Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作

网友投稿 380 2022-10-29


Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作

1 redis主从复制的概念

多机环境下,一个redis服务接收写命令,当自身数据与状态发生变化,将其复制到一个或多个redis。这种模式称为主从复制。在redis中通过命令salveof命令让执行该命令的redis复制另一个redis数据与状态。我们将主服务器称为master,从服务器称为slave。

主从复制保证了网络异常正常时,网络断开重的情况下将数据复制。网络正常时master会通过发送命令保持对slIwIfNEjlbave更新,更新包括客户端的写入,key的过期或被逐出等网络异常,master与slave连接断开一段时间,slave重连上master后会尝试部分重同步,重新获取连接断开期间丢失的命令。当无法进行部分重同步,则会执行全量重同步。

2 为什么需要主从复制

为了保证数据不丢失,有时会用到持久化功能。但这样会增加磁盘IO操作。通过使用主从复制,可以替代持久化并减少IO操作,降低延迟提高性能。

主从模式下,master负责处理写,slave负责读。虽然主从同步会导致在数据存在不一致窗口,但可以增加读操作的吞吐量。主从模式避免了redis单点风险。通过副本提高系统可用性。当master挂掉,从slave中选举新的机器作为master保证系统可用。

3 主从复制配置及原理

主从复制可以分为三个阶段:初始化、同步、命令传播。

初始化:从服务器执行完 slaveof 命令后,slave与master建立socket连接。连接建立完毕后通过ping进行心跳检测,若master正常,则返回响应。如果出现故障收不到响应,那么slave会重新尝试连接master。如果master设置了认证信息,则会再检查认证数据是否正确。如果认证失败,则会报错。

同步:当初始化完毕,master收到slave的数据同步命令后,需要判断是否执行全量同步还是部分同步。

命令传播:同步完成后,master与slave通过心跳检测判断对方是否在线。slave同时向master发送自己复制缓冲区的偏移量。master根据这些请求,判断是否向slave同步新产生的命令。slave收到同步的命令后执行,最终与master保持同步。

4 使用Lettuce在主从模式下执行命令

常用的java Redis客户端有Jedis、Redission、Lettuce。这里将通过Lettuce来演示主从模式下的读写分离命令执行。

io.lettuce

lettuce-core

5.1.8.RELEASE

下面通过

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 nodes = Lists.newArrayList(

RedisURI.create("redis://localhost:7000"),

RedisURI.create("redis://localhost:7001")

);

RedisClient redisClient = RedisClient.create();

StatefulRedisMasterSlaveConnection connection = MasterSlave.connect(

redisClient,

new Utf8StringCodec(), nodes);

connection.setReadFrom(ReadFrom.SLAVE);

RedisCommands 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>

3.8.2

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-data-redis

org.apache.commons

commons-pool2

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.RedIwIfNEjlbisTemplate;

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 redisTemplate(LettuceConnectionFactory connectionFactory) {

// 配置redisTemplate

RedisTemplate 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("hash1", "name1", "lms1");

redisTemplate.opsForHash().put("hash1", "name2", "lms2");

redisTemplate.opsForHash().put("hash1", "name3", "lms3");

System.out.println(redisTemplate.opsForValue().get("test:set"));

System.out.println(redisTemplate.opsForHash().get("hash1", "name1"));

}

}

6.在Redis上查看运行结果

从上图可以看到,Lettuce配置操作数据库成功!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:模拟接口(接入网的用户网络接口只有模拟接口)
下一篇:三种网络协议的连接方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~