• 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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    静态方法和类方法
    DEL: Restore Boxes after VirtualBox Upgrade
    DEL: IE "Your current security settings put your computer at risk. Click h
    EV: Using GitHub Repository
    EV: Windows Commands 命令
    EV: Notepad++ Regular Express syntax
    html页面的三个width: document, window, screen
    DEL: View web content zone in IE9
    EV: 关于min-width样式的使用
    EV: Linux Shell Commands
  • 原文地址:https://www.cnblogs.com/telwanggs/p/10826887.html
Copyright © 2020-2023  润新知