郑州网站建设搜q.479185700,怎么在百度上推广自己的公司信息,小型加工厂管理软件,山东省住房和城乡建设厅定额站子网站文章目录 一、前提条件二、SpringBoot访问Redis集群1. 引入依赖2. yaml配置3. 设置读写分离4. 简单的controller 三、运行四、测试1. 写2. 读3. 额外测试 环境
docker desktop for windows 4.23.0redis 7.2Idea
一、前提条件
先根据以下文章搭建一个Redis集群
Docker-Compo… 文章目录 一、前提条件二、SpringBoot访问Redis集群1. 引入依赖2. yaml配置3. 设置读写分离4. 简单的controller 三、运行四、测试1. 写2. 读3. 额外测试 环境
docker desktop for windows 4.23.0redis 7.2Idea
一、前提条件
先根据以下文章搭建一个Redis集群
Docker-Compose部署Redis(v7.2)主从模式Docker-Compose部署Redis(v7.2)哨兵模式
部署完后redis集群看起来大致如下图
二、SpringBoot访问Redis集群
1. 引入依赖
需要注意的是lettuce-core版本问题不能太旧否则不兼容新版的Redis。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdio.lettuce/groupIdartifactIdlettuce-core/artifactIdversion6.1.4.RELEASE/version !-- 或更高版本 --/dependency2. yaml配置
在application.yml加入以下配置。第一个password是用于sentinel节点验证第二个password用于数据节点验证。
spring:redis:sentinel:master: mymasternodes:- 172.30.1.11:26379- 172.30.1.12:26379- 172.30.1.13:26379password: 1009password: 1009这里关于sentinel的ip问题后面会讲解。
3. 设置读写分离
在任意配置类中写一个Bean本文简单起见直接写在SpringBoot启动类了。 Beanpublic LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){return clientConfigurationBuilder - clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);}这里的ReadFrom是配置Redis的读取策略是一个枚举包括下面选择
MASTER从主节点读取MASTER_PREFERRED:优先从master节点读取master不可用才读取replicaREPLICA:从slave (replica)节点读取REPLICA_PREFERRED优先从slave replica节点读取所有的slave都不可用才读取master
至于哪些节点支持读哪些支持写因为redis 7 默认给从节点设置为只读所以可以认为只有主节点有读写权限其余只有读权限。如果情况不一致就手动给每一个redis-server的配置文件都加上这一行。
replica-read-only yes4. 简单的controller
写一个简单的controller等会用于测试。
RestController
public class HelloController {Autowiredprivate 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;}
}三、运行
首先因为所有redis节点都在一个docker bridge网络中所以基于Idea编写的项目在宿主机(Windows)中运行spirngboot程序不好去和redis集群做完整的交互。
虽然说无论是sentinel还是redis-server都暴露了端口到宿主机我们可以通过映射的端口分别访问它们但是我们的程序只访问sentinelsentinel管理redis-serversentinel会返回redis-server的ip来让我们的程序来访问redis-server这里的ip是docker bridge网络里的ip所以即使我们的程序拿到ip也访问不了redis-server。
这个时候就需要将我们的项目放到一个docker容器中运行然后把这个容器放到和redis同一网络下就像下图。 具体如何快捷让Idea结合Docker去运行SpringBoot程序可以参考下面这篇文章。
Idea连接Docker在本地(Windows)开发SpringBoot
记得要暴露你的程序端口到宿主机这样才方便测试。
四、测试
1. 写
浏览器访问localhost:8080/set/num/7799 查看SpringBoot容器日志可以看到向主节点172.30.1.2:6379发送写请求。
01-06 07:23:59:848 DEBUG 1 --- [nio-8080-exec-6] io.lettuce.core.RedisChannelHandler : dispatching command AsyncCommand [typeSET, outputStatusOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:23:59:848 DEBUG 1 --- [nio-8080-exec-6] i.l.c.m.MasterReplicaConnectionProvider : getConnectionAsync(WRITE)
01-06 07:23:59:848 DEBUG 1 --- [nio-8080-exec-6] io.lettuce.core.RedisChannelHandler : dispatching command AsyncCommand [typeSET, outputStatusOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:23:59:848 DEBUG 1 --- [nio-8080-exec-6] i.lettuce.core.protocol.DefaultEndpoint : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379, epid0xf] write() writeAndFlush command AsyncCommand [typeSET, outputStatusOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:23:59:848 DEBUG 1 --- [nio-8080-exec-6] i.lettuce.core.protocol.DefaultEndpoint : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379, epid0xf] write() done
01-06 07:23:59:848 DEBUG 1 --- [oEventLoop-4-10] io.lettuce.core.protocol.CommandHandler : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379, epid0xf, chid0x16] write(ctx, AsyncCommand [typeSET, outputStatusOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command], promise)
01-06 07:23:59:849 DEBUG 1 --- [oEventLoop-4-10] io.lettuce.core.protocol.CommandEncoder : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379] writing command AsyncCommand [typeSET, outputStatusOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:23:59:851 DEBUG 1 --- [oEventLoop-4-10] io.lettuce.core.protocol.CommandHandler : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379, epid0xf, chid0x16] Received: 5 bytes, 1 commands in the stack
01-06 07:23:59:851 DEBUG 1 --- [oEventLoop-4-10] io.lettuce.core.protocol.CommandHandler : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379, epid0xf, chid0x16] Stack contains: 1 commands
01-06 07:23:59:851 DEBUG 1 --- [oEventLoop-4-10] i.l.core.protocol.RedisStateMachine : Decode done, empty stack: true
01-06 07:23:59:852 DEBUG 1 --- [oEventLoop-4-10] io.lettuce.core.protocol.CommandHandler : [channel0x9b4ebc85, /172.30.1.5:46700 - /172.30.1.2:6379, epid0xf, chid0x16] Completing command AsyncCommand [typeSET, outputStatusOutput [outputOK, errornull], commandTypeio.lettuce.core.protocol.Command]2. 读
浏览器访问localhost:8080/get/num 查看SpringBoot容器日志会向两个从节点之一发送读请求。
01-06 07:25:45:342 DEBUG 1 --- [io-8080-exec-10] io.lettuce.core.RedisChannelHandler : dispatching command AsyncCommand [typeGET, outputValueOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:25:45:342 DEBUG 1 --- [io-8080-exec-10] i.l.c.m.MasterReplicaConnectionProvider : getConnectionAsync(READ)
01-06 07:25:45:342 DEBUG 1 --- [io-8080-exec-10] io.lettuce.core.RedisChannelHandler : dispatching command AsyncCommand [typeGET, outputValueOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:25:45:342 DEBUG 1 --- [io-8080-exec-10] i.lettuce.core.protocol.DefaultEndpoint : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379, epid0x1c] write() writeAndFlush command AsyncCommand [typeGET, outputValueOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:25:45:342 DEBUG 1 --- [io-8080-exec-10] i.lettuce.core.protocol.DefaultEndpoint : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379, epid0x1c] write() done
01-06 07:25:45:342 DEBUG 1 --- [oEventLoop-4-11] io.lettuce.core.protocol.CommandHandler : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379, epid0x1c, chid0x23] write(ctx, AsyncCommand [typeGET, outputValueOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command], promise)
01-06 07:25:45:343 DEBUG 1 --- [oEventLoop-4-11] io.lettuce.core.protocol.CommandEncoder : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379] writing command AsyncCommand [typeGET, outputValueOutput [outputnull, errornull], commandTypeio.lettuce.core.protocol.Command]
01-06 07:25:45:346 DEBUG 1 --- [oEventLoop-4-11] io.lettuce.core.protocol.CommandHandler : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379, epid0x1c, chid0x23] Received: 10 bytes, 1 commands in the stack
01-06 07:25:45:346 DEBUG 1 --- [oEventLoop-4-11] io.lettuce.core.protocol.CommandHandler : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379, epid0x1c, chid0x23] Stack contains: 1 commands
01-06 07:25:45:346 DEBUG 1 --- [oEventLoop-4-11] i.l.core.protocol.RedisStateMachine : Decode done, empty stack: true
01-06 07:25:45:346 DEBUG 1 --- [oEventLoop-4-11] io.lettuce.core.protocol.CommandHandler : [channel0x96ae68cf, /172.30.1.5:38102 - /172.30.1.4:6379, epid0x1c, chid0x23] Completing command AsyncCommand [typeGET, outputValueOutput [output[B7427ef47, errornull], commandTypeio.lettuce.core.protocol.Command]3. 额外测试
以及还有一些额外的测试可以自行去尝试检验这里列举一些但具体不再赘述。
关闭两个从节点容器等待sentinel完成维护和通知后测试读数据和写数据会请求谁再次开启两个从节点等待sentinel完成操作后再关闭主节点等待sentinel完成操作后测试读数据和写数据会请求谁再次开启主节点等待sentinel完成操作后测试读数据和写数据会请求谁