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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM