springboot2.x采用lettuce整合redis集群
程序员文章站
2022-04-30 19:59:54
...
文章目录
1 springboot2.x采用lettuce整合redis集群
1 概念说明
springboot1.x系列中使用的是jedis, springboot2.x使用的是Lettuce。
关于jedis跟lettuce的区别:
- Lettuce 和 Jedis 都是Redis的client,都可以连接redis server。
- Jedis在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接。
- Lettuce的连接基于Netty,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,是线程安全的,这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
2 参考网址
- https://51dev.com/java/42905
- https://www.cnblogs.com/enjoyitlife/p/12057404.html
- https://www.cnblogs.com/stupidMartian/p/12092578.html
- https://blog.csdn.net/u010199866/article/details/80705797
- https://blog.csdn.net/weixin_40623736/article/details/98097708
- http://www.manongjc.com/article/3453.html
1.1 pom依赖
<!-- 集成redis采用lettuce方式需添加commons-pool依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
1.2 配置文件(properties或yml)
properties配置
spring.redis.timeout=30000
spring.redis.lettuce.pool.max-active=256
spring.redis.lettuce.pool.max-idle=64
spring.redis.lettuce.pool.max-wait=30000
spring.redis.lettuce.pool.min-idle=32
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384
yml配置
spring:
redis:
timeout: 30000
lettuce:
pool:
max-active: 256
max-idle: 64
max-wait: 30000
min-idle: 32
cluster:
max-redirects: 3 # 获取失败 最大重定向次数
nodes:
- 127.0.0.1:6379
- 127.0.0.1:6380
- 127.0.0.1:6381
- 127.0.0.1:6382
- 127.0.0.1:6383
- 127.0.0.1:6384
1.3 代码类
1.3.1 配置类
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
1.3.2 测试类
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.util.Set;
@SpringBootTest
class LettuceClusterTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("name", "hello world");
String name = redisTemplate.opsForValue().get("name").toString();
System.out.println(name);
System.out.println("》》》》》》》》》》》获取配置信息");
RedisSerializer<?> keySerializer = redisTemplate.getKeySerializer();
System.out.println(keySerializer);
LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
Set<RedisNode> clusterNodes = connectionFactory.getClusterConfiguration().getClusterNodes();
System.out.println(clusterNodes);
GenericObjectPoolConfig poolConfig = ((LettucePoolingClientConfiguration) connectionFactory.getClientConfiguration()).getPoolConfig();
System.out.println(poolConfig);
}
}
上一篇: SringBoot集成Mongodb
下一篇: Linux后台运行jar包
推荐阅读
-
Redis集群整合到springboot框架
-
Spring Boot(十三):整合Redis哨兵,集群模式实践
-
springboot2.x整合redis
-
SpringBoot2.x整合Redis进行数据缓存
-
springboot2.x整合redis实现缓存
-
springboot2.x简单整合redis
-
SpringBoot2.X整合Redis教程
-
springboot2.x集成redis(lettuce)
-
SpringBoot2.x系列二:整合第三方组件Mybatis、JPA、Redis、Elasticsearch、ActiveMQ、Kafka、Logback
-
SpringBoot2.x系列教程50--NoSQL之SpringBoot整合Redis