SpringBoot整合Redisson(集群版)

之前写了一篇关于SpringBoot整合Redisson的单机版,这篇是集群版。

关于如何在Linux搭建Redis集群,可以参考这篇文章:
redis集群搭建

一、导入Maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- redisson-springboot -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.11.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</exclusion>
</exclusions>
</dependency>

二、核心配置文件

1
2
3
4
5
6
7
8
9
10
11
12
redis:
cluster:
nodes: "192.168.52.1:7000,192.168.52.1:7001,192.168.52.1:7002,192.168.52.1:7003,192.168.52.1:7004,192.168.52.1:7005"
password: 123456
lettuce:
pool:
max-active: 1500
max-wait: 5000
max-idle: 500
min-idle: 100
shutdown-timeout: 1000
timeout: 60000

三、核心代码配置

RedisConfigProperties.java

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
30
31
32
33
34
35
36
37
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisConfigProperties {
private String password;
private cluster cluster;

public static class cluster {
private List<String> nodes;

public List<String> getNodes() {
return nodes;
}

public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public RedisConfigProperties.cluster getCluster() {
return cluster;
}

public void setCluster(RedisConfigProperties.cluster cluster) {
this.cluster = cluster;
}
}

RedissonConfig.java

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
import org.redisson.Redisson;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class RedissonConfig {
@Autowired
private RedisConfigProperties redisConfigProperties;

//添加redisson的bean
@Bean
public Redisson redisson() {
//redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加
List<String> clusterNodes = new ArrayList<>();
for (int i = 0; i < redisConfigProperties.getCluster().getNodes().size(); i++) {
clusterNodes.add("redis://" + redisConfigProperties.getCluster().getNodes().get(i));
}
Config config = new Config();
ClusterServersConfig clusterServersConfig = config.useClusterServers()
.addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
clusterServersConfig.setPassword(redisConfigProperties.getPassword());//设置密码
return (Redisson) Redisson.create(config);
}
}

四、启动项目,只要不报错就表示配置集群成功,同时启动过程中也会显示连接的各个redis

文章目录
  1. 一、导入Maven依赖
  2. 二、核心配置文件
  3. 三、核心代码配置
  4. 四、启动项目,只要不报错就表示配置集群成功,同时启动过程中也会显示连接的各个redis