Redis6搭建集群并在SpringBoot中使用RedisTemplate的實(shí)現(xiàn)
原理
Redis Cluster 一般由多個(gè)節(jié)點(diǎn)組成,節(jié)點(diǎn)數(shù)量至少為 6 個(gè)才能保證組成完整高可用的集群,其中三個(gè)為主節(jié)點(diǎn),三個(gè)為從節(jié)點(diǎn)。三個(gè)主節(jié)點(diǎn)會(huì)分配槽,處理客戶端的命令請(qǐng)求,而從節(jié)點(diǎn)可用在主節(jié)點(diǎn)故障后,頂替主節(jié)點(diǎn)。

如上圖所示,該集群中包含6個(gè)Redis節(jié)點(diǎn)3個(gè)主服務(wù)器3個(gè)從服務(wù)器,分別為M1,M2,M3,S1,S2,S3。除了主從 Redis 節(jié)點(diǎn)之間進(jìn)行數(shù)據(jù)復(fù)制外,所有 Redis 節(jié)點(diǎn)之間采用 Gossip 協(xié)議進(jìn)行通信,交換維護(hù)節(jié)點(diǎn)元數(shù)據(jù)信息。一般來(lái)說(shuō),主Redis節(jié)點(diǎn)會(huì)處理Clients的讀寫(xiě)操作,而從節(jié)點(diǎn)只處理讀操作。
搭建集群
1.進(jìn)入redis目錄中,打開(kāi)redis.conf文件,注釋掉bind 127.0.0.1,bind自己服務(wù)器的IP。并將protected -mode yes改為no。daemonize 默認(rèn)是no,需要改成yes,以守護(hù)進(jìn)程運(yùn)行。


2.分別創(chuàng)建6個(gè)conf文件對(duì)應(yīng)不同的端口號(hào),正常每個(gè)節(jié)點(diǎn)對(duì)應(yīng)一臺(tái)服務(wù)器,為了方便測(cè)試本次使用一臺(tái)服務(wù)來(lái)搭建集群模式。
redis6379.conf
include redis.conf pidfile /var/run/redis_6379.pid port 6379 dbfilename dump6379.rdb cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6379.conf
redis6380.conf
include redis.conf pidfile /var/run/redis_6380.pid port 6380 dbfilename dump6380.rdb cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6380.conf
redis6381.conf
include redis.conf pidfile /var/run/redis_6381.pid port 6381 dbfilename dump6381.rdb cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6381.conf
redis6389.conf
include redis.conf pidfile /var/run/redis_6389.pid port 6389 dbfilename dump6389.rdb cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6389.conf
redis6390.conf
include redis.conf pidfile /var/run/redis_6390.pid port 6390 dbfilename dump6390.rdb cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6390.conf
redis6391.conf
include redis.conf pidfile /var/run/redis_6391.pid port 6391 dbfilename dump6391.rdb cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6391.conf
3.進(jìn)入到redis安裝目錄啟動(dòng)redis服務(wù)(如之前有nodes、rdb、aof文件請(qǐng)先刪除,非則可能會(huì)報(bào)錯(cuò))。


4.進(jìn)入src目錄中執(zhí)行如下命令搭建集群。
redis-cli --cluster create --cluster-replicas 1 IP:6379 IP:6380 IP:6381 IP:6389 IP:6390 IP:6391
5.進(jìn)入其中任意一個(gè)節(jié)點(diǎn),執(zhí)行命令查看當(dāng)前節(jié)點(diǎn)狀態(tài)
redis-cli - h ip -p 端口info replication

SpringBoot中使用RedisTemplate
1.pom文件中加入依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>2.創(chuàng)建redis配置類.
package com.wzy.demo.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired
private RedisTemplate redisTemplate;
/**
* 解決redis插入中文亂碼
* @return
*/
@Bean
public RedisTemplate redisTemplateInit() {
//設(shè)置序列化Key的實(shí)例化對(duì)象
redisTemplate.setKeySerializer(new StringRedisSerializer());
//設(shè)置序列化Value的實(shí)例化對(duì)象
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}3.創(chuàng)建RedisUtil工具類
package com.wzy.demo.util;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
//=============================common============================
/**
* 指定緩存失效時(shí)間
*
* @param key 鍵
* @param time 時(shí)間(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據(jù)key 獲取過(guò)期時(shí)間
*
* @param key 鍵 不能為null
* @return 時(shí)間(秒) 返回0代表為永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判斷key是否存在
*
* @param key 鍵
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除緩存
*
* @param key 可以傳一個(gè)值 或多個(gè)
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
//============================String=============================
/**
* 普通緩存獲取
*
* @param key 鍵
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通緩存放入
*
* @param key 鍵
* @param value 值
* @return true成功 false失敗
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通緩存放入并設(shè)置時(shí)間
*
* @param key 鍵
* @param value 值
* @param time 時(shí)間(秒) time要大于0 如果time小于等于0 將設(shè)置無(wú)限期
* @return true成功 false 失敗
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 遞增
*
* @param key 鍵
* @param delta 要增加幾(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞增因子必須大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 遞減
*
* @param key 鍵
* @param delta 要減少幾(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("遞減因子必須大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
//================================Map=================================
/**
* HashGet
*
* @param key 鍵 不能為null
* @param item 項(xiàng) 不能為null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 獲取hashKey對(duì)應(yīng)的所有鍵值
*
* @param key 鍵
* @return 對(duì)應(yīng)的多個(gè)鍵值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 鍵
* @param map 對(duì)應(yīng)多個(gè)鍵值
* @return true 成功 false 失敗
*/
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并設(shè)置時(shí)間
*
* @param key 鍵
* @param map 對(duì)應(yīng)多個(gè)鍵值
* @param time 時(shí)間(秒)
* @return true成功 false失敗
*/
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
*
* @param key 鍵
* @param item 項(xiàng)
* @param value 值
* @return true 成功 false失敗
*/
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
*
* @param key 鍵
* @param item 項(xiàng)
* @param value 值
* @param time 時(shí)間(秒) 注意:如果已存在的hash表有時(shí)間,這里將會(huì)替換原有的時(shí)間
* @return true 成功 false失敗
*/
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除hash表中的值
*
* @param key 鍵 不能為null
* @param item 項(xiàng) 可以使多個(gè) 不能為null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判斷hash表中是否有該項(xiàng)的值
*
* @param key 鍵 不能為null
* @param item 項(xiàng) 不能為null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash遞增 如果不存在,就會(huì)創(chuàng)建一個(gè) 并把新增后的值返回
*
* @param key 鍵
* @param item 項(xiàng)
* @param by 要增加幾(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash遞減
*
* @param key 鍵
* @param item 項(xiàng)
* @param by 要減少記(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
//============================set=============================
/**
* 根據(jù)key獲取Set中的所有值
*
* @param key 鍵
* @return
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根據(jù)value從一個(gè)set中查詢,是否存在
*
* @param key 鍵
* @param value 值
* @return true 存在 false不存在
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將數(shù)據(jù)放入set緩存
*
* @param key 鍵
* @param values 值 可以是多個(gè)
* @return 成功個(gè)數(shù)
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 將set數(shù)據(jù)放入緩存
*
* @param key 鍵
* @param time 時(shí)間(秒)
* @param values 值 可以是多個(gè)
* @return 成功個(gè)數(shù)
*/
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) expire(key, time);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 獲取set緩存的長(zhǎng)度
*
* @param key 鍵
* @return
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除值為value的
*
* @param key 鍵
* @param values 值 可以是多個(gè)
* @return 移除的個(gè)數(shù)
*/
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//===============================list=================================
/**
* 獲取list緩存的內(nèi)容
*
* @param key 鍵
* @param start 開(kāi)始
* @param end 結(jié)束 0 到 -1代表所有值
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 獲取list緩存的長(zhǎng)度
*
* @param key 鍵
* @return
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 通過(guò)索引 獲取list中的值
*
* @param key 鍵
* @param index 索引 index>=0時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推
* @return
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @return
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @param time 時(shí)間(秒)
* @return
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @return
*/
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 將list放入緩存
*
* @param key 鍵
* @param value 值
* @param time 時(shí)間(秒)
* @return
*/
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根據(jù)索引修改list中的某條數(shù)據(jù)
*
* @param key 鍵
* @param index 索引
* @param value 值
* @return
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 移除N個(gè)值為value
*
* @param key 鍵
* @param count 移除多少個(gè)
* @param value 值
* @return 移除的個(gè)數(shù)
*/
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}4.在controller中測(cè)試redis。
@RequestMapping("/testRedis")
public String testRedis(Model model) throws Exception{
redisUtil.set("myid","123456");
Object object = redisUtil.get("myid");
model.addAttribute("redisKey",String.valueOf(object));
return "redis.html";
}5.頁(yè)面中顯示redis的返回結(jié)果。

到此這篇關(guān)于Redis6搭建集群并在SpringBoot中使用RedisTemplate的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot使用RedisTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate
- springboot使用redisTemplate操作lua腳本
- SpringBoot整合Redis使用@Cacheable和RedisTemplate
- springboot使用redisRepository和redistemplate操作redis的過(guò)程解析
- 詳解SpringBoot使用RedisTemplate操作Redis的5種數(shù)據(jù)類型
- SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法
- Spring Boot單元測(cè)試中使用mockito框架mock掉整個(gè)RedisTemplate的示例
- Spring Boot中RedisTemplate的使用示例詳解
相關(guān)文章
SpringBoot項(xiàng)目報(bào)錯(cuò):"Error?starting?ApplicationContext....
這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目報(bào)錯(cuò):“Error?starting?ApplicationContext.?To?display?the?conditions?report?re-run?...”的解決辦法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue3實(shí)現(xiàn)一個(gè)todo-list
這篇文章主要為大家詳細(xì)介紹了基于vuejs實(shí)現(xiàn)一個(gè)todolist項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-08-08
SpringMVC通過(guò)RESTful結(jié)構(gòu)實(shí)現(xiàn)頁(yè)面數(shù)據(jù)交互
RESTFUL是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計(jì)風(fēng)格和開(kāi)發(fā)方式,基于HTTP,可以使用XML格式定義或JSON格式定義。RESTFUL適用于移動(dòng)互聯(lián)網(wǎng)廠商作為業(yè)務(wù)接口的場(chǎng)景,實(shí)現(xiàn)第三方OTT調(diào)用移動(dòng)網(wǎng)絡(luò)資源的功能,動(dòng)作類型為新增、變更、刪除所調(diào)用資源2022-08-08
SpringMVC的處理器攔截器HandlerInterceptor詳解
這篇文章主要介紹了SpringMVC的處理器攔截器HandlerInterceptor詳解,SpringWebMVC的處理器攔截器,類似于Servlet開(kāi)發(fā)中的過(guò)濾器Filter,用于處理器進(jìn)行預(yù)處理和后處理,需要的朋友可以參考下2024-01-01
詳解Java 序列化與反序列化(Serialization)
這篇文章主要介紹了Java 序列化與反序列化(Serialization),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí) 吧2019-03-03
Java循環(huán)隊(duì)列與非循環(huán)隊(duì)列的區(qū)別總結(jié)
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí)總結(jié),文章圍繞著Java循環(huán)隊(duì)列與非循環(huán)隊(duì)列的區(qū)別展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
通過(guò)入門demo簡(jiǎn)單了解netty使用方法
這篇文章主要介紹了通過(guò)入門demo簡(jiǎn)單了解netty使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Java項(xiàng)目開(kāi)發(fā)命名規(guī)范(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
定義這個(gè)規(guī)范的目的是讓項(xiàng)目中所有的文檔都看起來(lái)像一個(gè)人寫(xiě)的,增加可讀性,減少項(xiàng)目組中因?yàn)閾Q人而帶來(lái)的損失。下面給大家分享java開(kāi)發(fā)命名規(guī)范,一起看看吧2017-03-03

