Ubuntu 22最新dockers部署redis哨兵模式,并整合spring boot和配置redisson详细记录(含spring boot项目包)

dockers部署redis哨兵模式,并整合spring boot

环境说明

系统:Ubuntu 22

容器:docker

redis服务器:

master:192.168.81.128

slave:192.168.81.129

相关学习博客

docker的详细学习教程

redis的详细学习教程

一、在docker中安装redis

1、下载dockers镜像包和redis配置文件(主从一样)

bash 复制代码
# 下载最新的redis镜像包
docker pull redis
# 查询镜像
docker images
REPOSITORY       TAG       IMAGE ID       CREATED       SIZE
redis            latest    7fc37b47acde   12 days ago   116MB
delron/fastdfs   latest    8487e86fc6ee   5 years ago   464MB
# 创建docker挂载目录
mkdir -p /usr/local/redis
cd /usr/local/redis/
# 下载redis配置文件
wget -c http://download.redis.io/redis-stable/redis.conf

2、编辑配置文件

主机配置

bash 复制代码
vim redis.conf
bind 0.0.0.0
port 6379
daemonize yes
logfile "/data/logs/redis.log"
masterauth yourPassword
requirepass yourPassword
appendonly yes
cluster-enabled yes

从机配置

bash 复制代码
vim redis.conf
bind 0.0.0.0
port 6379
daemonize yes
logfile "/data/logs/redis.log"
masterauth yourPassword
replicaof 192.168.81.128 6379
requirepass yourPassword
appendonly yes
cluster-enabled yes

3、启动redis(主从一样)

bash 复制代码
docker run -p 6379:6379 -p 26379:26379  --privileged=true --name redis -v $PWD/redis.conf:/etc/redis/redis.conf -v $PWD/data:/data -d --restart=always redis

4、进入容器测试(主从一样)

bash 复制代码
# 进入容器
$ docker exec -it redis  bash
# 使用客户端命令进入reids并输入密码
root@93e142d4b040:/data# redis-cli -a "yourPassword"
# 设置一个key为test,value为redis的键值对做简单的测试
127.0.0.1:6379> set test redis
OK
# 取值
127.0.0.1:6379> get test
"redis"
127.0.0.1:6379> info

二、配置主从复制

1、在从机上设置master的IP的端口,并切换主机

bash 复制代码
# 设置主机
127.0.0.1:6379> SLAVEOF  192.168.81.128 6379
# 切换master(当自动切换无效时,可以使用此命令手动切换)
127.0.0.1:6379> replicaof  192.168.81.128 6379

配置文件的密码可能会不生效,则需要手动配置

127.0.0.1:6379> config set masterauth yourPassword

127.0.0.1:6379> config set requirepass yourPassword

2、查询两台服务器的主从配置

Master

bash 复制代码
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.81.128,port=6379,state=online,offset=3327799,lag=0
master_failover_state:no-failover
master_replid:0e2af2cc670451aa4ec703c336f3014a35a67016
master_replid2:2dbd8f657c563221b2bfd8d45aead1f25e7e9922
master_repl_offset:3327799
second_repl_offset:2395307
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2265043
repl_backlog_histlen:1062757

Slave

bash 复制代码
127.0.0.1:6379> info  replication
# Replication
role:slave
master_host:192.168.81.129
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:3362051
slave_repl_offset:3362051
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:0e2af2cc670451aa4ec703c336f3014a35a67016
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3362051
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2408022
repl_backlog_histlen:954030
127.0.0.1:6379>

3、主从测试

自行在master上设置一个值,然后看从机上

三、配置哨兵模式(主从一样)

1、可有可无的工具

bash 复制代码
# 推荐在容器里面安装一下两个依赖
apt-get update
# 方便编辑文件
apt-get install -y vim
# ps用于查询进程
apt-get install procps

2、配置sentinel.conf

root@4266ebe78410:/# vim sentinel.conf

bash 复制代码
port 26379
daemonize yes
sentinel monitor mymaster 192.168.81.128 6379 2
sentinel auth-pass mymaster yourPassword

3、启动哨兵模式

bash 复制代码
redis-sentinel sentinel.conf
# 使用ps查询进程
root@4266ebe78410:/# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
redis          1       0  1 09:31 ?        00:01:20 redis-server *:6379
root          21       0  0 09:31 pts/0    00:00:00 bash
root          74       1  0 10:45 ?        00:00:32 redis-sentinel *:26379 [sentinel]
root          84       0  0 11:28 pts/1    00:00:00 bash
root          96      84 99 11:41 pts/1    00:00:00 ps -ef

4、查看启动情况

bash 复制代码
redis-cli -a "yourPassword" -p 26379
127.0.0.1:26379> info sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.22.1.68:6379,slaves=2,sentinels=4

总结

未解决的问题:(如果找到原因欢迎在评论区沟通)

  • redis.conf中配置的密码,没有生效,每次重启需要手动配置密码,然后再启动哨兵

四、整合到spring boot中

1、创建一个新的spring boot项目

2、添加依赖

xml 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.12.4</version>
            <version>3.13.6</version>
        </dependency>

3、添加application.yaml的配置

yaml 复制代码
server:
  port: 8088

spring:
  redis:
    # 数据库(默认为0号库)
    database: 2
    # 密码(默认空),操作redis需要使用的密码
    password: yourPassword
    # 端口号
    #    port: 6379
    #连接超时时间(毫秒)
    timeout: 10000ms
    sentinel:
      master: mymaster
      nodes:
        - 192.168.81.128:26379
        - 192.168.81.129:26379
      # 操作sentinel时需要提供的密码
      password: yourPassword
    # 使用lettuce配置
    lettuce:
      pool:
        # 连接池最大连接数(使用负值表示没有限制)
        max-active: 200
        # 连接池中的最大空闲连接
        max-idle: 20
        # 连接池中的最小空闲连接
        min-idle: 5
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms

4、创建redis的配置类

java 复制代码
package com.cdjs.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {

    @Bean
    RedissonClient redissonSentinel() {
        Config config = new Config();
        config.useSentinelServers()
                .setMasterName("mymaster")
                .addSentinelAddress("redis://192.168.81.128:26379")
                .addSentinelAddress("redis://192.168.81.129:26379")
                .setMasterConnectionPoolSize(250)
                .setSlaveConnectionPoolSize(250)
                .setCheckSentinelsList(false)
                .setPassword("yourPassword")
                .setMasterConnectionMinimumIdleSize(10)
                .setSlaveConnectionMinimumIdleSize(10);
        return Redisson.create(config);
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        // 创建RedisTemplate对象
        RedisTemplate<String,Object> template = new RedisTemplate();

        // 设置连接工厂
        template.setConnectionFactory(connectionFactory);

        // 创建Json序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();


        // 设置Key的序列化
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());

        // 设置Value的序列化
        template.setValueSerializer(jsonRedisSerializer);
        // 设置Hash采用String的方式序列化Value
        template.setHashValueSerializer(stringRedisSerializer);
        return template;
    }
}
  • RedissonClient redissonSentinel() redisson是分布式锁配置,不需要的可以不配

5、自定义RedisTemplate

见笔记

6、遇到的问题redisson

redisson是分布式配置。

启动spring boot时报错:Can't execute SENTINEL commands on /192.168.81.128:26379

解决办法:

降低redisson版本

org.redisson

redisson

3.12.4

相关推荐
范什么特西1 小时前
Spring boot细节
java·spring boot·后端
java1234_小锋2 小时前
请描述 Spring Boot 的启动流程,包括 SpringApplication 的初始化和 run 方法的核心步骤。
java·数据库·spring boot
炘爚2 小时前
Linux——Redis
数据库·redis·缓存
csjane10793 小时前
Redisson 限流原理
java·redis
Flittly3 小时前
【AgentScope Java新手村系列】(9)SpringBoot集成
java·spring boot·spring
ThanksGive3 小时前
Go 服务里的 Redis 锁惊群问题:一次本地合流优化实践
redis
杨运交3 小时前
[033][缓存模块]基于 Redisson 的租户隔离 Redis Key 前缀设计
spring boot
小挪号底迪滴3 小时前
Redis 和 MySQL 数据不一致怎么办?缓存更新策略实战
redis·mysql·缓存