zoukankan      html  css  js  c++  java
  • springboot2.x 整合redis集群的几种方式

    一、不指定redis连接池

    #系统默认连接池

    yml配置文件:

    spring:
      redis:
        cluster:
          nodes:
            - 192.168.1.236:7001
            - 192.168.1.236:7002
            - 192.168.1.236:7003
            - 192.168.1.244:7004
            - 192.168.1.244:7005
            - 192.168.1.244:7006
          max-redirects: 3  # 获取失败 最大重定向次数
        pool:
          max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
          max-idle: 10    # 连接池中的最大空闲连接
          max-wait: -1   # 连接池最大阻塞等待时间(使用负值表示没有限制)
          min-idle:  5     # 连接池中的最小空闲连接
        timeout: 6000  # 连接超时时长(毫秒)

    这种方式 redisTemplate 可直接使用默认,

    在使用的地方直接注入即可

     @Autowired
     private RedisTemplate<String, Object> redisTemplate;
    

      

    二、使用jedis连接池

    # 使用jedis连接池

    yml配置文件:

    spring:
      redis:
        password:    # 密码(默认为空)
        timeout: 6000ms  # 连接超时时长(毫秒)
        cluster:
          nodes:
            - 192.168.1.236:7001
            - 192.168.1.236:7002
            - 192.168.1.236:7003
            - 192.168.1.244:7004
            - 192.168.1.244:7005
            - 192.168.1.244:7006 
        jedis:
          pool:
            max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)
            max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)
            max-idle: 10      # 连接池中的最大空闲连接
            min-idle: 5       # 连接池中的最小空闲连接

    //连接池注入配置信息

    @Configuration
    public class RedisConfig {
       @Autowired
       private RedisConnectionFactory factory;
    
       @Bean
        public RedisTemplate<String, Object> redisTemplate() {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
            redisTemplate.setConnectionFactory(factory);
            return redisTemplate;
        }
    }
    

      

    在使用的地方直接注入即可

     @Autowired
     private RedisTemplate<String, Object> redisTemplate;
    

      

    三、使用lettuce连接池(推荐)

    # 使用lettuce连接池

    yml配置文件:

    spring:
      redis:
        timeout: 6000ms
        password: 
        cluster:
          max-redirects: 3  # 获取失败 最大重定向次数 
          nodes:
            - 192.168.1.236:7001
            - 192.168.1.236:7002
            - 192.168.1.236:7003
            - 192.168.1.244:7004
            - 192.168.1.244:7005
            - 192.168.1.244:7006 
        lettuce:
          pool:
            max-active: 1000  #连接池最大连接数(使用负值表示没有限制)
            max-idle: 10 # 连接池中的最大空闲连接
            min-idle: 5 # 连接池中的最小空闲连接
            max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)

    //连接池注入配置信息

    @Configuration
    @AutoConfigureAfter(RedisAutoConfiguration.class)
    public class RedisConfig {    
        @Bean
        public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
    }
    

      

    在使用的地方直接注入即可

     @Autowired
     private RedisTemplate<String, Object> redisTemplate;
    

      


    ---------------------
    作者:qq_31256487
    来源:CSDN
    原文:https://blog.csdn.net/qq_31256487/article/details/83144088
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    rabbitmq-高级(死信队列)
    rabbitmq-高级(TTL过期时间)
    springboot整合rabbitmq(topic主题模式)
    springboot整合rabbitmq(direct路由模式)
    glide图片加载库
    自己封装的OKhttp请求
    手机上搭建微型服务器
    listview实现点击条目上的箭头展开隐藏菜单。
    recycleview + checkbox 实现单选
    recycleview中使用checkbox导致的重复选中问题
  • 原文地址:https://www.cnblogs.com/telwanggs/p/10826887.html
Copyright © 2011-2022 走看看