使用DockerCompose部署Redis高可用哨兵版——整合SpringBoot

50 篇文章 10 订阅
22 篇文章 1 订阅

        今天来记录一下使用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上了,具体演示结果就不做了 ,大家自行下载使用吧

https://gitee.com/840312696/redis-demo.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值