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 可直接使用默认,

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

1
2
@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       # 连接池中的最小空闲连接
复制代码

 

//连接池注入配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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;
    }
}

  

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

1
2
@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 # 连接池最大阻塞等待时间(使用负值表示没有限制)
复制代码

 

//连接池注入配置信息

1
2
3
4
5
6
7
8
9
10
11
12
@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;
    }
}

  

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

1
2
@Autowired
private RedisTemplate<String, Object> redisTemplate;
posted @ 2020-09-11 20:25  jack-jin  阅读(510)  评论(0)    收藏  举报