Java如何使用Lettuce客户端在Redis主从模式下执行命令
1 redis主从复制的概念
在多机环境下,一个redis服务接收写命令,并在自身数据和状态发生变化时将其复制到一个或多个redis。这种模式称为主从复制。通过命令slaveof,在Redis中可以让一个Redis服务器复制另一个Redis服务器的数据和状态。我们将主服务器称为master,从服务器称为slave。
主从复制保证了网络异常正常时,网络断开重的情况下将数据复制。网络正常时master会通过发送命令保持对slave更新,更新包括客户端的写入,key的过期或被逐出等网络异常,master与slave连接断开一段时间,slave重连上master后会尝试部分重同步,重新获取连接断开期间丢失的命令。当无法进行部分重同步,则会执行全量重同步。
2 为什么需要主从复制
为了保证数据不丢失,有时会用到持久化功能。但这样会增加磁盘IO操作。使用主从复制技术可以取代持久化,减少IO操作,从而降低延迟并提高性能。
主从模式下,master负责处理写,slave负责读。尽管主从同步可能会导致数据不一致,但它可以提高读操作的吞吐量。主从模式避免了redis单点风险。通过副本提高系统可用性。如果主节点挂掉,通过从节点选举新的节点作为主节点以确保系统可用。
3 主从复制配置及原理
主从复制可以分为三个阶段:初始化、同步、命令传播。
当服务器执行完slaveof命令后,从服务器与主服务器建立套接字连接,完成初始化。如果主服务器正常,建立连接后会通过ping命令进行心跳检测并返回响应。当发生故障并收不到响应时,从节点将会重新尝试与主节点进行连接。如果master设置了认证信息,则会再检查认证数据是否正确。如果认证失败,则会报错。
在初始化完成之后,当master接收到slave的数据同步指令时,需要根据情况来确定是执行全量同步还是部分同步。
在同步完成后, 主服务器和从服务器通过心跳检测确认彼此的在线状态,以进行命令传输。slave同时向master发送自己复制缓冲区的偏移量。根据这些请求,master会判断是否需要将新产生的命令同步到slave。slave收到同步的命令后执行,最终与master保持同步。
4 使用Lettuce在主从模式下执行命令
Jedis、Redission和Lettuce是常见的Java Redis客户端。这里将通过Lettuce来演示主从模式下的读写分离命令执行。
<dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.1.8.RELEASE</version> </dependency>
下面通过
package redis; import io.lettuce.core.ReadFrom; import io.lettuce.core.RedisClient; import io.lettuce.core.RedisURI; import io.lettuce.core.api.sync.RedisCommands; import io.lettuce.core.codec.Utf8StringCodec; import io.lettuce.core.masterslave.MasterSlave; import io.lettuce.core.masterslave.StatefulRedisMasterSlaveConnection; import org.assertj.core.util.Lists; class MainLettuce { public static void main(String[] args) { List<RedisURI> nodes = Lists.newArrayList( RedisURI.create("redis://localhost:7000"), RedisURI.create("redis://localhost:7001") ); RedisClient redisClient = RedisClient.create(); StatefulRedisMasterSlaveConnection<String, String> connection = MasterSlave.connect( redisClient, new Utf8StringCodec(), nodes); connection.setReadFrom(ReadFrom.SLAVE); RedisCommands<String, String> redisCommand = connection.sync(); redisCommand.set("master","master write test2"); String value = redisCommand.get("master"); System.out.println(value); connection.close(); redisClient.shutdown(); } }
补充:Redis 客户端之Lettuce配置使用(基于Spring Boot 2.x)
开发环境:使用Intellij IDEA + Maven + Spring Boot 2.x + JDK 8
Spring Boot 从 2.0版本开始,将默认的Redis客户端Jedis替换问Lettuce,下面描述Lettuce的配置使用。
1.在项目的pom.xml文件下,引入Redis在Spring Boot 下的相关Jar包依赖
<properties> <redisson.version>3.8.2</redisson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> </dependencies>
2.在项目的resources目录下,在application.yml文件里添加lettuce的配置参数
#Redis配置 spring: redis: database: 6 #Redis索引0~15,默认为0 host: 127.0.0.1 port: 6379 password: #密码(默认为空) lettuce: # 这里标明使用lettuce配置 pool: max-active: 8 #连接池最大连接数(使用负值表示没有限制) max-wait: -1ms #连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 5 #连接池中的最大空闲连接 min-idle: 0 #连接池中的最小空闲连接 timeout: 10000ms #连接超时时间(毫秒)
3.添加Redisson的配置参数读取类RedisConfig
package com.dbfor.redis.config; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { /** * RedisTemplate配置 * @param connectionFactory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) { // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer());//key序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } }
4.构建Spring Boot的启动类RedisApplication
package com.dbfor.redis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class); } }
5.编写测试类RedisTest
package com.dbfor.redis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) @Component public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void set() { redisTemplate.opsForValue().set("test:set1", "testValue1"); redisTemplate.opsForSet().add("test:set2", "asdf"); redisTemplate.opsForHash().put("hash2", "name1", "lms1"); redisTemplate.opsForHash().put("hash2", "name2", "lms2"); redisTemplate.opsForHash().put("hash2", "name3", "lms3"); System.out.println(redisTemplate.opsForValue().get("test:set")); System.out.println(redisTemplate.opsForHash().get("hash2", "name1")); } }
6.在Redis上查看运行结果
以上是Java如何使用Lettuce客户端在Redis主从模式下执行命令的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Redis集群模式通过分片将Redis实例部署到多个服务器,提高可扩展性和可用性。搭建步骤如下:创建奇数个Redis实例,端口不同;创建3个sentinel实例,监控Redis实例并进行故障转移;配置sentinel配置文件,添加监控Redis实例信息和故障转移设置;配置Redis实例配置文件,启用集群模式并指定集群信息文件路径;创建nodes.conf文件,包含各Redis实例的信息;启动集群,执行create命令创建集群并指定副本数量;登录集群执行CLUSTER INFO命令验证集群状态;使

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

Redis 使用哈希表存储数据,支持字符串、列表、哈希表、集合和有序集合等数据结构。Redis 通过快照 (RDB) 和追加只写 (AOF) 机制持久化数据。Redis 使用主从复制来提高数据可用性。Redis 使用单线程事件循环处理连接和命令,保证数据原子性和一致性。Redis 为键设置过期时间,并使用 lazy 删除机制删除过期键。

要查看 Redis 中的所有键,共有三种方法:使用 KEYS 命令返回所有匹配指定模式的键;使用 SCAN 命令迭代键并返回一组键;使用 INFO 命令获取键的总数。

解决redis-server找不到问题的步骤:检查安装,确保已正确安装Redis;设置环境变量REDIS_HOST和REDIS_PORT;启动Redis服务器redis-server;检查服务器是否运行redis-cli ping。

使用 Redis 指令需要以下步骤:打开 Redis 客户端。输入指令(动词 键 值)。提供所需参数(因指令而异)。按 Enter 执行指令。Redis 返回响应,指示操作结果(通常为 OK 或 -ERR)。

H5开发需要掌握的工具和框架包括Vue.js、React和Webpack。1.Vue.js适用于构建用户界面,支持组件化开发。2.React通过虚拟DOM优化页面渲染,适合复杂应用。3.Webpack用于模块打包,优化资源加载。

Redis 有序集合(ZSet)用于存储有序元素集合,并按关联分数进行排序。ZSet 的用法步骤包括:1. 创建 ZSet;2. 添加成员;3. 获取成员分数;4. 获取排名;5. 获取排名范围的成员;6. 删除成员;7. 获取元素个数;8. 获取分数范围内的成员个数。
