创建shards目录
mkdir shards
复制redis.conf到shards目录下
cp redis.conf shards/6379.conf
cp redis.conf shards/6380.conf
cp redis.conf shards/6381.conf
按照各自的文件修改对应的端口号信息
启动多台redis
redis-server 6379.conf
redis-server 6380.conf
redis-server 6381.com
编辑redis.properties配置文件
redis.shards=192.168.52.134:6379,192.168.52.134:6380,192.168.52.134:6381
编辑配置类
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.shards}")
private String redisShards;//node,node,node
//配置redis分片机制
@Scope("prototype") //对象的多例,使用连接池
@Bean //标识实例化对象的类型
public ShardedJedis shardedJedis() {
String[] nodeArray = redisShards.split(",");
List<JedisShardInfo> shards = new ArrayList<>();
for (String node : nodeArray) { //node=IP:PROT
String[] nodeArr = node.split(":");
String host = nodeArr[0];
int port = Integer.parseInt(nodeArr[1]);
//每循环一次,添加一个node节点对象到list集合中
shards.add(new JedisShardInfo(host, port));
}
return new ShardedJedis(shards);
}
}
CacheAop配置
@Autowired
private ShardedJedis jedis;