Lettuce形式集成redis示例
2019-03-17 本文已影响75人
叫我不矜持
前言
在 springboot 1.5.x版本的默认的Redis客户端是 Jedis实现的,springboot 2.x版本中默认客户端是用 lettuce实现的。
Lettuce 和 jedis 的都是连接 Redis Server的客户端,Jedis 在实现上是直连 redis server,多线程环境下非线程安全,除非使用连接池,为每个 redis实例增加 物理连接。
Lettuce 是 一种可伸缩,线程安全,完全非阻塞的Redis客户端,多个线程可以共享一个RedisConnection,它利用Netty NIO 框架来高效地管理多个连接,从而提供了异步和同步数据访问方式,用于构建非阻塞的反应性应用程序
本文以Lettuce形式集成Redis服务
一.导入依赖(pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二.application.propertis文件配置信息
# 主机地址
redis.cluster.host=192.168.247.13
# 最大重定向次数
redis.cluster.maxRedirects=3
# 连接池中的最大空闲连接 默认为8
generic.pool.config.maxIdle=10
# 连接池中的最小空闲连接 默认为0
generic.pool.config.minIdle=5
# 连接池中的总连接数
generic.pool.config.maxTotal=20
三.自定义 RedisTemplate
@Configuration
public class RedisConfig {
@Value("${spring.redisURI}")
private String redisURI;
@Value("${redis.cluster.host}")
private String host;
//连接池配置信息(如果不用连接池可以省略这一步)
@Bean
//prefix,表示从配置文件读取以前缀开头的信息,注入到GenericObjectPoolConfig中
@ConfigurationProperties(prefix="generic.pool.config")
public GenericObjectPoolConfig genericObjectPoolConfig() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
return poolConfig;
}
//lettuce客户端配置信息(如果不用连接池通过LettuceClientConfiguration来builder)
@Bean
public LettuceClientConfiguration lettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig){
//构造LettucePoolingClientConfiguration对象,同时加入连接池配置信息
LettucePoolingClientConfiguration build = LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig).build();
return build;
}
//Redis继续配置信息
@Bean
@ConfigurationProperties(prefix="redis.cluster")
public RedisClusterConfiguration redisClusterConfiguration(){
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
//配置集群信息(也可以从配置文件加载)
ArrayList<RedisNode> list = new ArrayList<>();
list.add(new RedisNode(host,7001));
list.add(new RedisNode(host,7002));
list.add(new RedisNode(host,7003));
list.add(new RedisNode(host,7004));
list.add(new RedisNode(host,7005));
list.add(new RedisNode(host,7006));
redisClusterConfiguration.setClusterNodes(list);
return redisClusterConfiguration;
}
//配置LettuceConnectionFactory连接工厂
@Bean
public LettuceConnectionFactory lettuceConnectionFactory(RedisClusterConfiguration redisClusterConfiguration,LettuceClientConfiguration lettuceClientConfiguration){
//通过配置RedisClusterConfiguration实例来创建工厂
//构造该对象是传入Lettuce客户端配置和redis集群配置
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisClusterConfiguration,lettuceClientConfiguration);
return lettuceConnectionFactory;
}
//配置RedisTemplate
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<String, Serializable>();
//设置key的存储方式为字符串
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置为value的存储方式为JDK二进制序列化方式,还有jackson序列化方式(Jackson2JsonRedisSerialize)
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
//注入Lettuce连接工厂
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}
四.测试
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class TestSpringBoot {
@Autowired
private RedisTemplate<String, Serializable> redisTemplate;
@Test
public void test(){
ValueOperations<String, Serializable> opsForValue = redisTemplate.opsForValue();
//在这里可以重新设置存储的格式(重新设置为json字符串形式存储)
//redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
User user = new User();
user.setIdcard("8888");
user.setUsername("小白");
//调用opsForValue存储key/value
opsForValue.set("user", user);
//查询
Serializable serializable = opsForValue.get("user");
System.out.println(serializable.toString());
//删除
Boolean delete = redisTemplate.delete("user");
//再次查询
Serializable res = (User)opsForValue.get("user");
System.out.println(delete+":"+res );
}
}