Setup redis cluster
Please refer to https://blog.csdn.net/yaominhua/article/details/82229502
Add dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.15</version>
</dependency>
Add redis cluster configure
src/main/resources/application.properties
redis.cluster.servers=127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005
redis.cluster.commandTimeout=5000
@Configuration
public class JedisClusterConfig {
@Autowried
private Environment env;
@Bean
public JedisCluster jedisCluster() {
String[] serverArrary = env.getProperty("redis.cluster.servers").split(",");
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), ipPortPair[1].trim())));
}
return new JedisCluster(nodes, Integer.valueOf(env.getProperty("redis.cluster.commandTimeout")));
}
}
Reference
《Java微服务实战》- 赵计刚