今天来记录一下使用DockerCompose来部署Redis高可用哨兵版,并整合springBoot代码实现。
前面两篇博客(redis单机版和redis集群版)的实操还是挺顺利的 ,到这里了出现了一些状况,解决这些问题花费了一些功夫 ,最终整理出来了一个比较直接的部署过程。
首先这一篇博客是建立在redis集群版的基础上去增加的DockerCompose中的内容,加入了哨兵的部分,这里不再多说了直接上干货。
DockerCompose文件内容如下:
services:
redisServer1:
image: redis:6.2.4
container_name: redis_server1
volumes:
- /home/redis/redis1/data:/data
- /home/redis/redis1/logs:/logs
command:
# 服务启动
/bin/bash -c "redis-server --port 7001"
#ports:
# - 7001:7001
network_mode: host
redisServer2:
image: redis:6.2.4
container_name: redis_server2
volumes:
- /home/redis/redis2/data:/data
- /home/redis/redis2/logs:/logs
command:
# 服务启动
/bin/bash -c "redis-server --port 7002 --replicaof 192.168.21.69 7001"
#ports:
# - 7002:7002
network_mode: host
#依赖服务
depends_on:
- redisServer1
redisServer3:
image: redis:6.2.4
container_name: redis_server3
volumes:
- /home/redis/redis3/data:/data
- /home/redis/redis3/logs:/logs
command:
# 服务启动
/bin/bash -c "redis-server --port 7003 --replicaof 192.168.21.69 7001"
#ports:
# - 7003:7003
network_mode: host
#依赖服务
depends_on:
- redisServer1
sentinelServer1:
image: redis:6.2.4
container_name: sentinel_server1
volumes:
- /home/redis/sentinel.conf:/home/sentinel.conf
command:
# 服务启动
/bin/bash -c "redis-sentinel /home/sentinel.conf --port 27001"
#ports:
# - 27001:27001
network_mode: host
#依赖服务
depends_on:
- redisServer1
sentinelServer2:
image: redis:6.2.4
container_name: sentinel_server2
volumes:
- /home/redis/sentinel.conf:/home/sentinel.conf
command:
# 服务启动
/bin/bash -c "redis-sentinel /home/sentinel.conf --port 27002"
#ports:
# - 27002:27002
network_mode: host
#依赖服务
depends_on:
- redisServer1
sentinelServer3:
image: redis:6.2.4
container_name: sentinel_server3
volumes:
- /home/redis/sentinel.conf:/home/sentinel.conf
command:
# 服务启动
/bin/bash -c "redis-sentinel /home/sentinel.conf --port 27003"
#ports:
# - 27003:27003
network_mode: host
#依赖服务
depends_on:
- redisServer1
network_mode: host
这里需要解释的一点是,上面这一行是让容器使用了宿主机的网络,这样会避免一些不同容器之间访问不到的问题。
上面还有是sentinel.conf配置文件,这里是用来配置哨兵的配置文件,内容如下:
port 27001
sentinel announce-ip 192.168.21.69
sentinel monitor mymaster 192.168.21.69 7001 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
dir "/home"
这两个文件把ip换成服务器的ip就可以了。
这个时候redis哨兵版的配置已经搞定了!
docker-compose up
下面我们来介绍一下使用springboot来对redis进行操作
用到的pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.itcast</groupId>
<artifactId>redis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
yml配置文件如下:
logging:
level:
io.lettuce.core: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
spring:
redis:
sentinel:
master: mymaster
nodes:
- 192.168.21.69:27001
- 192.168.21.69:27002
- 192.168.21.69:27003
启动类
@SpringBootApplication
public class RedisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RedisDemoApplication.class, args);
}
@Bean
public LettuceClientConfigurationBuilderCustomizer configurationBuilderCustomizer(){
return configBuilder -> configBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}
}
controller文件
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";
}
}
代码也上传到gitee上了,具体演示结果就不做了 ,大家自行下载使用吧