个人博客


  • SpringBoot2.1.4
  • 操作Redis集群。

1、Spring配置Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Bean
public JedisCluster jedisCluster(JedisPoolConfig jedisPoolConfig) {
Set<HostAndPort> nodeSet = new HashSet<>();
nodeSet.add(new HostAndPort("172.16.122.101", 6379));
nodeSet.add(new HostAndPort("172.16.122.101", 6380));
nodeSet.add(new HostAndPort("172.16.122.102", 6379));
nodeSet.add(new HostAndPort("172.16.122.102", 6380));
nodeSet.add(new HostAndPort("172.16.122.103", 6379));
nodeSet.add(new HostAndPort("172.16.122.103", 6380));
JedisCluster jedisCluster = new JedisCluster(nodeSet, 2000, jedisPoolConfig);

log.info("=====创建JedisCluster=====");

return jedisCluster;
}

2、代码测试示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Autowired
private JedisCluster jedisCluster; // 集群操作

@Test
public void TestJedisCluster() {
// jedisCluster
jedisCluster.set("name", "zxb");
jedisCluster.set("age", "28");

// get
log.info(jedisCluster.get("name"));

// exists
log.info(jedisCluster.exists("age") + "");

// expire
jedisCluster.expire("name", 1);

// ttl
log.info(jedisCluster.ttl("name") + "");

// ++ --
log.info(jedisCluster.incr("age") + "");
log.info(jedisCluster.decr("age") + "");

// del
jedisCluster.del("age");

}

参考链接

代码地址