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

本文主要是介绍Ubuntu 22最新dockers部署redis哨兵模式,并整合spring boot和配置redisson详细记录(含spring boot项目包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

  • 环境说明
  • 相关学习博客
  • 一、在docker中安装redis
    • 1、下载dockers镜像包和redis配置文件(主从一样)
    • 2、编辑配置文件
    • 3、启动redis(主从一样)
    • 4、进入容器测试(主从一样)
  • 二、配置主从复制
    • 1、在从机上设置master的IP的端口,并切换主机
    • 2、查询两台服务器的主从配置
    • 3、主从测试
  • 三、配置哨兵模式(主从一样)
    • 1、可有可无的工具
    • 2、配置sentinel.conf
    • 3、启动哨兵模式
    • 4、查看启动情况
  • 四、整合到spring boot中
    • 1、创建一个新的spring boot项目
    • 2、添加依赖
    • 3、添加application.yaml的配置
    • 4、创建redis的配置类
    • 5、自定义RedisTemplate
    • 6、遇到的问题redisson

环境说明

系统:Ubuntu 22
容器:docker
redis服务器:
master:192.168.81.128
slave:192.168.81.129

相关学习博客

docker的详细学习教程

redis的详细学习教程

一、在docker中安装redis

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

# 下载最新的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、编辑配置文件

主机配置

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

从机配置

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(主从一样)

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、进入容器测试(主从一样)

# 进入容器
$ 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的端口,并切换主机

# 设置主机
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

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

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、可有可无的工具

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

2、配置sentinel.conf

root@4266ebe78410:/# vim sentinel.conf

port 26379
daemonize yes
sentinel monitor mymaster 192.168.81.128 6379 2
sentinel auth-pass mymaster yourPassword

3、启动哨兵模式

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、查看启动情况

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项目

在这里插入图片描述

  • 如果遇到可选择的Java版式过高,可将idea默认“服务器URL”改为:https://start.aliyun.com/

2、添加依赖

        <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的配置

server:port: 8088spring:redis:# 数据库(默认为0号库)database: 2# 密码(默认空),操作redis需要使用的密码password: yourPassword# 端口号#    port: 6379#连接超时时间(毫秒)timeout: 10000mssentinel:master: mymasternodes:- 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的配置类

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 {@BeanRedissonClient 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);}@Beanpublic 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的方式序列化Valuetemplate.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

这篇关于Ubuntu 22最新dockers部署redis哨兵模式,并整合spring boot和配置redisson详细记录(含spring boot项目包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/925644

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示