這篇文章主要介紹“Java如何使用Lettuce客戶端在Redis主從模式下執行命令”,在日常操作中,相信很多人在Java如何使用Lettuce客戶端在Redis主從模式下執行命令問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java如何使用Lettuce客戶端在Redis主從模式下執行命令”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
多機環境下,一個redis服務接收寫命令,當自身數據與狀態發生變化,將其復制到一個或多個redis。這種模式稱為主從復制。在redis中通過命令salveof命令讓執行該命令的redis復制另一個redis數據與狀態。我們將主服務器稱為master,從服務器稱為slave。
主從復制保證了網絡異常正常時,網絡斷開重的情況下將數據復制。網絡正常時master會通過發送命令保持對slave更新,更新包括客戶端的寫入,key的過期或被逐出等網絡異常,master與slave連接斷開一段時間,slave重連上master后會嘗試部分重同步,重新獲取連接斷開期間丟失的命令。當無法進行部分重同步,則會執行全量重同步。
為了保證數據不丟失,有時會用到持久化功能。但這樣會增加磁盤IO操作。通過使用主從復制,可以替代持久化并減少IO操作,降低延遲提高性能。
主從模式下,master負責處理寫,slave負責讀。雖然主從同步會導致在數據存在不一致窗口,但可以增加讀操作的吞吐量。主從模式避免了redis單點風險。通過副本提高系統可用性。當master掛掉,從slave中選舉新的機器作為master保證系統可用。
主從復制可以分為三個階段:初始化、同步、命令傳播。
初始化:從服務器執行完 slaveof 命令后,slave與master建立socket連接。連接建立完畢后通過ping進行心跳檢測,若master正常,則返回響應。如果出現故障收不到響應,那么slave會重新嘗試連接master。如果master設置了認證信息,則會再檢查認證數據是否正確。如果認證失敗,則會報錯。
同步:當初始化完畢,master收到slave的數據同步命令后,需要判斷是否執行全量同步還是部分同步。
命令傳播:同步完成后,master與slave通過心跳檢測判斷對方是否在線。slave同時向master發送自己復制緩沖區的偏移量。master根據這些請求,判斷是否向slave同步新產生的命令。slave收到同步的命令后執行,最終與master保持同步。
常用的Java Redis客戶端有Jedis、Redission、Lettuce。這里將通過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的配置使用。
<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>
#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 #連接超時時間(毫秒)
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;
}
}
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);
}
}
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"));
}
}
到此,關于“Java如何使用Lettuce客戶端在Redis主從模式下執行命令”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。