Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式
说明
Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移。如果想了解更多集群模式的相关知识介绍,欢迎往上爬楼。
准备条件
pom.xml中引入相关jar
<!-- 集成Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Jedis 客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- lettuce客户端需要使用到 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
application.yml集群模式配置属性示例。
spring:
redis:
host: 192.168.8.121
port: 6379
password: enjoyitlife
timeout: 30000
jedis:
pool:
max-active: 256
max-wait: 30000
max-idle: 64
min-idle: 32
lettuce:
pool:
max-active: 256
max-idle: 64
max-wait: 30000
min-idle: 32
cluster:
nodes:
- 192.168.8.121:7000
- 192.168.8.121:7001
- 192.168.8.121:7002
- 192.168.8.121:7003
- 192.168.8.121:7004
- 192.168.8.121:7005
- 192.168.8.121:7006
- 192.168.8.121:7007
nodes节点读取。因为nodes是集合方式,所以spring中的@value$("xxx.xxx.xx")是无法读取的,本文提供了一种获取改节点属性的方式。
RedisClusterNodesCfg.java 获取nodes节点数据的代码示例。
package top.enjoyitlife.redis;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterNodesCfg {
private List<String> nodes;
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
}
集群模式下的整合教程
Jedis客户端整合
JedisClusterConfig.java 相关配置
package top.enjoyitlife.redis.jedis;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.enjoyitlife.redis.RedisClusterNodesCfg;
@Configuration
@Profile("JedisCluster")
public class JedisClusterConfig {
@Autowired
private RedisClusterNodesCfg redisClusterNodesCfg;
@Bean
public JedisConnectionFactory redisPoolFactory() throws Exception{
RedisClusterConfiguration rcc=new RedisClusterConfiguration();
List<String> nodesList=redisClusterNodesCfg.getNodes();
String host=null;
int port=0;
for(String node:nodesList) {
host=node.split(":")[0];
port=Integer.valueOf(node.split(":")[1]);
rcc.addClusterNode(new RedisNode(host,port));
}
return new JedisConnectionFactory(rcc);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
Lettuce客户端整合
package top.enjoyitlife.redis.lettuce;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.enjoyitlife.redis.RedisClusterNodesCfg;
@Configuration
@Profile("lettuceCluster")
public class LettuceClusterConfig {
@Autowired
private RedisClusterNodesCfg redisClusterNodesCfg;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisClusterConfiguration rcc=new RedisClusterConfiguration();
List<String> nodesList=redisClusterNodesCfg.getNodes();
String host=null;
int port=0;
for(String node:nodesList) {
host=node.split(":")[0];
port=Integer.valueOf(node.split(":")[1]);
rcc.addClusterNode(new RedisNode(host,port));
}
return new LettuceConnectionFactory(rcc);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
Springboot通过RedisClusterConfiguration来统一了连接集群的方式,区别Jedis客户端是通过JedisConnectionFactory进行初始化,而Lettuce客户端是通过LettuceConnectionFactory初始化。
单元测试
Jedis单元测试
package top.enjoyitlife.redis.jedis;
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.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest
@ActiveProfiles("JedisCluster")
class JedisClusterTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
String name=redisTemplate.opsForValue().get("name").toString();
redisTemplate.opsForValue().set("hahha", "enjoyitlife2020");
System.out.println(name);
}
}
Lettuce 单元测试
package top.enjoyitlife.redis.lettuce;
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.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest
@ActiveProfiles("lettuceCluster")
class LettuceClusterTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
String name = redisTemplate.opsForValue().get("name").toString();
System.out.println(name);
}
}
好了以上就是Springboot2.x集成Redis集群模式的代码示例,希望对你能有所帮助。谢谢阅读。