zoukankan      html  css  js  c++  java
  • springboot整合redisCluster集群(springboot整合redisCluster集群)

    springboot整合redisCluster集群:(前提redis集群已经搭建好。windows上搭建redis集群参考:https://www.cnblogs.com/super-chao/p/9329018.html。linux上搭建redis集群步骤和windows上搭建redis集群一致)

    1.引入springboot和redis的相关jar包:

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>spring-aop</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>spring-aop</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

    2.创建application.yml配置文件并配置redis集群:

    #server:
    #  port: 8762
    spring:
      application:
        name: redis
      #redis配置
      redis:
    #    host: 192.168.1.100
    #    port: 6385
        pool:
          max-idle: 100
          min-idle: 1
          max-active: 1000
          max-wait: -1
        database: 0
        timeout: 100000
        cluster:
          nodes:
            #地址要和redis配置中bind地址一致
            - 127.0.0.1:6379
            - 127.0.0.1:6380
            - 127.0.0.1:6381
            - 127.0.0.1:6382
            - 127.0.0.1:6383
            - 127.0.0.1:6384

    3.redis工具类,RedisClusterService:

    package com.springbootrediscluster.redis;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.concurrent.TimeUnit;
    
    @Service
    public class RedisClusterService {
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        public void setStr(String key, String value) {
            setStr(key, value, null);
        }
    
        public void setStr(String key, String value, Long time) {
            /*stringRedisTemplate.opsForValue().set(key, value);
            if (time != null)
                stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);*/
            stringRedisTemplate.opsForValue().set(key,value);
            if(time != null){
                stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            /*if(time != null){
                jedisCluster.expire(key,(int) (time/1000));
            }*/
        }
    
        public Object getKey(String key) {
            /*return redisTemplate.opsForValue().get(key);*/
            return stringRedisTemplate.opsForValue().get(key);
        }
    
        public void delKey(String key) {
            /*stringRedisTemplate.delete(key);*/
            stringRedisTemplate.delete(key);
        }
    }

    4.controller类,IndexController:

    package com.springbootrediscluster.controller;
    
    import com.springbootrediscluster.redis.RedisClusterService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * Created by Administrator on 2021/8/15.
     */
    @Controller
    public class IndexController {
        @Autowired
        private RedisClusterService redisClusterService;
    
        @RequestMapping("/setRedis")
        @ResponseBody
        public String setRedis(String key, String value) {
            redisClusterService.setStr(key, value);
            return "success";
        }
    
        @RequestMapping("/getKey")
        @ResponseBody
        public Object getKey(String key){
            Object result = redisClusterService.getKey(key);
            return result == null ? "缓存中没有该数据" : result;
        }
    
        @RequestMapping("/delStringKey")
        @ResponseBody
        public String reStrRedis(String key){
            redisClusterService.delKey(key);
            return "success";
        }
    }

    5.springboot的app启动类App:

    package com.itmayiedu.app;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    
    @ComponentScan("com.itmayiedu.**")
    @EnableAutoConfiguration
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }

    6.测试:

     

     也可在dos命令窗口查看值,或者通过RedisClient可视化工具查看redis的值。

    至此,springboot整合redisCluster集群完成。项目中可直接使用工具类RedisClusterService(可自行重构)对redisCluster集群操作。

  • 相关阅读:
    《Django By Example》第十二章(终章) 中文 翻译 (个人学习,渣翻)
    《Django By Example》第十一章 中文 翻译 (个人学习,渣翻)
    《Django By Example》第十章 中文 翻译 (个人学习,渣翻)
    《Django By Example》第九章 中文 翻译 (个人学习,渣翻)
    《Django By Example》第八章 中文 翻译 (个人学习,渣翻)
    《Django By Example》第五章 中文 翻译 (个人学习,渣翻)
    我的superui开源后台bootstrap开发框架
    LayoutInflater 总结
    Android屏幕分辨率概念(dp、dip、dpi、sp、px)
    android studio安装问题
  • 原文地址:https://www.cnblogs.com/super-chao/p/15143411.html
Copyright © 2011-2022 走看看