SpringBoot+Redis哨兵模式的實現(xiàn)_第1頁
SpringBoot+Redis哨兵模式的實現(xiàn)_第2頁
SpringBoot+Redis哨兵模式的實現(xiàn)_第3頁
SpringBoot+Redis哨兵模式的實現(xiàn)_第4頁
SpringBoot+Redis哨兵模式的實現(xiàn)_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第SpringBoot+Redis哨兵模式的實現(xiàn)最近學(xué)習(xí)到了Redis的哨兵模式,光看視頻還不行,需要自己動手實現(xiàn)一遍才能加深映像,特此記錄。

由于沒有真實的服務(wù)器可以供我操作,所以在虛擬機上啟動了3個redis服務(wù),分別占用7001、7002、7003端口。

Redis下載安裝不多贅述,只在這里記錄一下配置。

首先在tmp目錄下創(chuàng)建3個文件夾:

cd/tmp

mkdir700170027003

然后將redis的配置文件redis.conf拷貝到剛剛創(chuàng)建的3個文件夾下

cpredis-6.2.6/redis.conf/tmp/7001

cpredis-6.2.6/redis.conf/tmp/7002

cpredis-6.2.6/redis.conf/tmp/7003

接著修改這3個配置文件

viredise.conf

找到端口,redis默認端口是6379,這里分別將端口改為7001、7002和7003

然后修改dir,redis持久化文件保存的路徑,分別改為對應(yīng)的路徑

接著注釋掉bind并且修改protected-mode為no

redis默認不允許遠程連接,修改這2項配置允許我們遠程連接

最后在配置文件第一行加上replica-announce-ip#{ip}

注意:這里#{ip}填自己的ip地址

由于是在虛擬機安裝的redis,會有多個ip,這里寫明ip防止找不到

3個配置文件都改完后,cd到對應(yīng)的目錄啟動redis

3個服務(wù)都啟動后,連接7002的redis

redis-cli-p7002

輸入命令,搭建主從集群,讓7002成為7001的從節(jié)點

REPLICAOF#{ip}7001

注意:這里#{ip}填自己的ip地址

同理,7003也這樣操作一遍,這樣就搭建好了以7001為主節(jié)點,7002和7003位從節(jié)點的主從集群模式。

需要注意的是,以命令形式搭建的主從集群,重啟后就失效了,想要持久保持可以在配置文件里配置,這里從簡就不貼了。

上述操作完成后,接著在tmp目錄下創(chuàng)建3個新文件夾

mkdirs1s2s3

cd到s1目錄,創(chuàng)建文件sentinel.conf,這個文件也可以從redis的目錄中拷貝。

編寫文件配置

#sentinel端口

port27001

#工作路徑

dir"/tmp/s1"

#哨兵監(jiān)控的master,主從配置一樣,在進行主從切換時7001會變成當前的master端口,最后的2為客觀判斷主節(jié)#點下線的節(jié)點個數(shù)

sentinelmonitormymaster#{ip}70012

#master或slave多長時間不能使用后標記為s_down狀態(tài)

sentineldown-after-millisecondsmymaster5000

#若sentinel在該配置值內(nèi)未能完成failover操作(即故障時master/slave自動切換),

#則認為本次failover失敗

sentinelfailover-timeoutmymaster60000

注意:這里#{ip}填自己的ip地址

然后將sentinel.conf文件cp到s2和s3路徑下,只用修改port和dir為各自的配置

然后分別在各自路徑下啟動3個哨兵

redis-sentinelsentinel.conf

由于之前測試了7001關(guān)閉服務(wù),哨兵自動切換主節(jié)點為7002了,若為第一次啟動,日志和截圖中的會稍有不同。

哨兵模式搭建好后,接著在Java端集成此模式

pom.xml引入最基本的依賴即可

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

exclusions!--去掉springboot默認配置--

exclusion

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-logging/artifactId

/exclusion

/exclusions

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-data-redis/artifactId

/dependency

dependency

groupIdcom.alibaba/groupId

artifactIdfastjson/artifactId

version1.2.73/version

/dependency

application.xml

spring:

redis:

sentinel:

master:mymaster

nodes:

-#{ip}:27001

-#{ip}:27002

-#{ip}:27003

注意:這里#{ip}填自己的ip地址

在一個配置類里注入一個bean,實現(xiàn)redis讀寫分離,配置從redis讀數(shù)據(jù)時優(yōu)先從從節(jié)點讀取

packagecom.wl.demo.config;

importcom.alibaba.fastjson.serializer.SerializerFeature;

importcom.alibaba.fastjson.support.config.FastJsonConfig;

importcom.alibaba.fastjson.support.spring.FastJsonRedisSerializer;

importio.lettuce.core.ReadFrom;

importorg.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.data.redis.connection.RedisConnectionFactory;

importorg.springframework.data.redis.core.RedisTemplate;

importorg.springframework.data.redis.serializer.StringRedisSerializer;

*@authorwl

*@date2025/3/28

@Configuration

publicclassRedisConfig{

@Bean

publicLettuceClientConfigurationBuilderCustomizerlettuceClientConfigurationBuilderCustomizer(){

returnbuilder-builder.readFrom(ReadFrom.REPLICA_PREFERRED);

@Bean

publicRedisTemplateString,ObjectredisTemplate(RedisConnectionFactoryconnectionFactory){

RedisTemplateString,ObjectredisTemplate=newRedisTemplate();

redisTemplate.setConnectionFactory(connectionFactory);

FastJsonRedisSerializerObjectfastJsonRedisSerializer=newFastJsonRedisSerializer(Object.class);

FastJsonConfigfastJsonConfig=fastJsonRedisSerializer.getFastJsonConfig();

SerializerFeature[]serializerFeatures=newSerializerFeature[]{SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteMapNullValue};

fastJsonConfig.setSerializerFeatures(serializerFeatures);

StringRedisSerializerstringRedisSerializer=newStringRedisSerializer();

redisTemplate.setKeySerializer(stringRedisSerializer);

redisTemplate.setHashKeySerializer(stringRedisSerializer);

redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);

redisTemplate.setValueSerializer(fastJsonRedisSerializer);

redisTemplate.setEnableTransactionSupport(true);

redisTemplate.afterPropertiesSet();

returnredisTemplate;

}

編寫一個測試接口

packagecom.wl.demo.controller;

importmon.result.HttpResult;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.data.redis.core.StringRedisTemplate;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

importorg.springframework.web.bind.annotation.RestController;

*@authorwl

*@date2025/4/14

@RestController

publicclassTestController{

privatefinalStringRedisTemplatestringRedisTemplate;

@Autowired

publicTestController(StringRedisTemplatestringRedisTemplate){

this.stringRedisTemplate=stringRedisTemplate;

@GetMapping("/set/{key}/{value}")

publicHttpResultsetValue(@PathVariable("key")Stringkey,@PathVariable("value")Stringvalue){

stringRedisTemplate.opsForValue().set(key,value);

returnHttpResult.success();

@GetMapping("/get/{key}")

publicHttpResultgetValue(@PathVariable("key")Stringkey){

returnHttpResult.success(stringRedisTemplate.opsForValue().get(key));

}

啟動springboot,調(diào)用set接口

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論