首页
搭建一主两从集群
- 在/root/redis/conf文件夹下新建配置文件redisMaster.conf、redisSlave1.conf、redisSlave2.conf
# redisMaster.conf
port 6382
bind 0.0.0.0
replica-announce-ip 35.10.208.28 #对外暴漏ip,设置为本机ip即可。不设置的话,后期redistemplate链接报错
replica-announce-port 6382
# redisSlave1.conf
port 6383
slaveof 35.10.208.28 6382
replica-announce-ip 35.10.208.28
replica-announce-port 6383
#redisSlave2.conf
port 6384
slaveof 35.10.208.28 6382
replica-announce-ip 35.10.208.28
replica-announce-port 6384
- docker命令搭建集群
docker run -itd --privileged=true --name redis-master1 -v /root/redis/conf/:/usr/local/etc/redis -p 6382:6382 redis:latest redis-server /usr/local/etc/redis/redisMaster.conf
docker run -itd --privileged=true --name redis-slave11 -v /root/redis/conf/:/usr/local/etc/redis -p 6383:6383 redis:latest redis-server /usr/local/etc/redis/redisSlave1.conf
docker run -itd --privileged=true --name redis-slave22 -v /root/redis/conf/:/usr/local/etc/redis -p 6384:6384 redis:latest redis-server /usr/local/etc/redis/redisSlave2.conf
搭建哨兵
- 增加配置文件sentinel1.conf,sentinel2.conf,sentinel3.conf
# sentinel1.conf
port 16379
sentinel monitor master 35.10.208.28 6383 2
dir "/"
logfile "sentinel1.log"
#sentinel2.conf
port 16380
sentinel monitor master 35.10.208.28 6383 2
dir "/"
logfile "sentinel2.log"
#sentinel3.conf
port 16381
sentinel monitor master 35.10.208.28 6383 2
dir "/"
logfile "sentinel3.log"
- docker命令搭建集群
docker run -itd --privileged=true --name redis-sentinel1 -v /root/redis/conf/:/usr/local/etc/redis -p 16379:16379 redis:latest redis-sentinel /usr/local/etc/redis/sentinel1.conf
docker run -itd --privileged=true --name redis-sentinel2 -v /root/redis/conf/:/usr/local/etc/redis -p 16380:16380 redis:latest redis-sentinel /usr/local/etc/redis/sentinel2.conf
docker run -itd --privileged=true --name redis-sentinel3 -v /root/redis/conf/:/usr/local/etc/redis -p 16381:16381 redis:latest redis-sentinel /usr/local/etc/redis/sentinel3.conf
使用springboot连接集群
配置pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置文件
spring:
redis:
sentinel:
master: master
nodes:
- 35.10.208.28:16379
- 35.10.208.28:16380
- 35.10.208.28:16381
配置读写分离
@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer() {
return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}
使用redistemplate
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/get/{key}")
public String hi(@PathVariable String key) {
return redisTemplate.opsForValue().get(key);
}
@GetMapping("/set/{key}/{value}")
public String hi(@PathVariable String key, @PathVariable String value) {
redisTemplate.opsForValue().set(key, value);
return "success";
}
}