【笔记】SpringBoot项目整合Redis分片

前言

SpringBoot项目整合Redis分片

创建配置文件

  • 创建redis.properties配置文件
1
redis.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381

创建配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {

@Value("${redis.nodes}")
private String nodes;

@Bean
public ShardedJedis shardedJedis(){
List<JedisShardInfo> shards = new ArrayList<>();
String[] nodeArray = nodes.split(",");
for (String node : nodeArray){ //node=ip:port
String host = node.split(":")[0];
int port = Integer.parseInt(node.split(":")[1]);
shards.add(new JedisShardInfo(host, port));
}
return new ShardedJedis(shards);
}

}

完成