spring-data-2与其上一个版本1.8是一个分水岭,2.0用的是io.lettuce:lettuce-core,2.0之前的spring-data用的是biz.paluch.redis:lettuce
spring-data-redis版本2.0.9.RELEASE
io.lettuce:lettuce-core版本5.0.4.RELEASE
依赖:
io.lettuce
lettuce-core
5.0.4.RELEASE
org.springframework.data
spring-data-redis
2.0.9.RELEASE
org.apache.commons
commons-pool2
2.6.0
最终目标为:StringRedisTemplate或者RedisTemplate
需要 : RedisConnectionFactory
其实现为 : LettuceConnectionFactory
因实现sentinel,所以需要构造参数:
1、RedisSentinelConfiguration,其配置sentinel-master和ip:host
2、LettuceClientConfiguration,可其配置连接池以及ssl等相关参数(使用其子接口LettucePoolingClientConfiguration)
LettucePoolingClientConfiguration.可通过static方法使用builder模式创建:
LettucePoolingClientConfiguration.builder().poolConfig(pool).build();
pool类型GenericObjectPoolConfig为common-pool2线程池
实现代码:
bean配置(连接池,spring-template等)
/**
* @author wkCae
*/
@Configuration
@PropertySource(value = {"classpath:redis.properties"})
@ComponentScan(basePackages = "com")
public class RedisClientConfiguration {
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
//todo 定制化配置
return stringRedisTemplate;
}
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
//todo 定制化配置
return redisTemplate;
}
@Bean
public LettuceConnectionFactory lettuceConnectionFactory(RedisSentinelConfiguration redisSentinelConfiguration, LettuceClientConfiguration lettuceClientConfiguration) {
return new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration);
}
/**
* 配置哨兵集群信息 master和host:ip
* @param sentinelProperties 集群Properties
* @return redisSentinelConfiguration
*/
@Bean
public RedisSentinelConfiguration redisSentinelConfiguration(SentinelProperties sentinelProperties) {
return new RedisSentinelConfiguration(sentinelProperties.getMaster(), sentinelProperties.getHosts());
}
/**
* 配置LettuceClientConfiguration 包括线程池配置和安全项配置
* @param genericObjectPoolConfig common-pool2线程池
* @return lettuceClientConfiguration
*/
@Bean
public LettuceClientConfiguration lettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) {
return LettucePoolingClientConfiguration.builder()
.poolConfig(genericObjectPoolConfig)
.build();
}
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig(CommonPool2Properties commonPool2Properties) {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(commonPool2Properties.getMaxIdle());
poolConfig.setMinIdle(commonPool2Properties.getMinIdle());
poolConfig.setMaxTotal(commonPool2Properties.getMaxTotal());
//todo 其他配置
return poolConfig;
}
}
哨兵信息配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* @author wkCae
*/
@Component
public class SentinelProperties {
@Value("${lettuce.sentinel.master}")
private String master;
@Value("${lettuce.sentinel.nodes}")
private String nodes;
private Set hosts;
@PostConstruct
public void hosts() {
hosts = new HashSet<>();
hosts.addAll(Arrays.asList(nodes.split(",")));
}
public String getMaster() {
return master;
}
public void setMaster(String master) {
this.master = master;
}
public String getNodes() {
return nodes;
}
public void setNodes(String nodes) {
this.nodes = nodes;
}
public Set getHosts() {
return hosts;
}
public void setHosts(Set hosts) {
this.hosts = hosts;
}
}
连接池信息配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
public class CommonPool2Properties {
@Value("${lettuce.pool.maxTotal}")
private Integer maxTotal;
@Value("${lettuce.pool.maxIdle}")
private Integer maxIdle;
@Value("${lettuce.pool.minIdle}")
private Integer minIdle;
//TODO 其他属性
public Integer getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(Integer maxTotal) {
this.maxTotal = maxTotal;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
}
redis.properties
lettuce.sentinel.master=mymaster
lettuce.sentinel.nodes=127.0.0.1:63791,127.0.0.1:63792,127.0.0.1:63793
lettuce.sentinel.node1=127.0.0.1:63791
lettuce.sentinel.node2=127.0.0.1:63792
lettuce.sentinel.node3=127.0.0.1:63793
lettuce.pool.maxTotal=5
lettuce.pool.maxIdle=2
lettuce.pool.minIdle=2
# todo 其他线程池配置
spring-data-redis版本1.8.7.RELEASE
io.lettuce:lettuce-core版本4.2.2.RELEASE
依赖:
biz.paluch.redis
lettuce
4.2.2.Final
org.springframework.data
spring-data-redis
1.8.7.RELEASE
org.apache.commons
commons-pool2
2.6.0
最终目标为:StringRedisTemplate或者RedisTemplate
需要 : RedisConnectionFactory
其实现为 : LettuceConnectionFactory
需要构造参数:
LettucePool接口,其默认实现为DefaultLettucePool。
DefaultLettucePool可接收RedisSentinelConfiguration类型构造参数,然后可以为其设置GenericObjectPoolConfig用于配置common-pool2的连接池。
spring-data-2.0.x与之前版本还有一个区别就是ssl等配置放到了LettuceConnectionFactory中。
直接xml实现完整配置
${lettuce.sentinel.node1}
${lettuce.sentinel.node2}
${lettuce.sentinel.node3}