SpringBoot 整合 Redis 集群
创建 Redis 集群
打开 Redis 远程访问
添加依赖
在 pom 文件中添加 Redis 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置 Redis 集群连接
在 application.properties 文件中添加 Redis 连接参数,多节点之间用逗号分隔开
#Redis
spring.redis.cluster.nodes=192.168.2.2:6000,192.168.2.2:6001,192.168.2.2:6002,192.168.2.2:6003,192.168.2.2:6004,192.168.2.2:6005
spring.redis.password=123456
测试连接
编写测试类
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("redis-cluster-state","success");
System.out.println(redisTemplate.opsForValue().get("redis-cluster-state"));
}
运行结果成功读出 success,连接成功