欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+Redis哨兵模式的實現(xiàn)

 更新時間:2022年05月18日 11:43:51   作者:wl_Honest  
本文主要介紹了SpringBoot+Redis哨兵模式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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

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

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

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

cd /tmp
mkdir 7001 7002 7003

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

cp redis-6.2.6/redis.conf /tmp/7001
cp redis-6.2.6/redis.conf /tmp/7002
cp redis-6.2.6/redis.conf /tmp/7003

接著修改這3個配置文件

vi redise.conf

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

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

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

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

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

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

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

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

 3個服務都啟動后,連接7002的redis

redis-cli -p 7002

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

REPLICAOF #{ip} 7001

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

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

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

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

mkdir s1 s2 s3

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

編寫文件配置

# sentinel端口
port 27001
#工作路徑
dir "/tmp/s1"
# 哨兵監(jiān)控的master,主從配置一樣,在進行主從切換時7001會變成當前的master端口,最后的2為客觀判斷主節(jié)# 點下線的節(jié)點個數(shù)
sentinel monitor mymaster #{ip} 7001 2
# master或slave多長時間不能使用后標記為s_down狀態(tài)
sentinel down-after-milliseconds mymaster 5000
#若sentinel在該配置值內(nèi)未能完成failover操作(即故障時master/slave自動切換),
#則認為本次failover失敗
sentinel failover-timeout mymaster 60000

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

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

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

redis-sentinel sentinel.conf

 

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

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

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

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <exclusions><!-- 去掉springboot默認配置 -->
         <exclusion>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-logging</artifactId>
         </exclusion>
     </exclusions>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.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é)點讀取

package com.wl.demo.config;
 
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import io.lettuce.core.ReadFrom;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
/**
 * @author wl
 * @date 2022/3/28
 */
@Configuration
public class RedisConfig {
 
    @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer() {
        return builder -> builder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }
 
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
 
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        FastJsonConfig fastJsonConfig = fastJsonRedisSerializer.getFastJsonConfig();
        SerializerFeature[] serializerFeatures = new SerializerFeature[] {SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue};
        fastJsonConfig.setSerializerFeatures(serializerFeatures);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
 
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
        redisTemplate.setValueSerializer(fastJsonRedisSerializer);
        redisTemplate.setEnableTransactionSupport(true);
 
        redisTemplate.afterPropertiesSet();
 
        return redisTemplate;
    }
}

編寫一個測試接口

package com.wl.demo.controller;
 
import com.wl.demo.common.result.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author wl
 * @date 2022/4/14
 */
@RestController
public class TestController {
 
    private final StringRedisTemplate stringRedisTemplate;
 
    @Autowired
    public TestController(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
 
    @GetMapping("/set/{key}/{value}")
    public HttpResult setValue(@PathVariable("key") String key, @PathVariable("value") String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return HttpResult.success();
    }
 
    @GetMapping("/get/{key}")
    public HttpResult getValue(@PathVariable("key") String key) {
        return HttpResult.success(stringRedisTemplate.opsForValue().get(key));
    }
}

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

 查看redis

7002主節(jié)點有值了,并且它的從節(jié)點也同步到了數(shù)據(jù)

 然后調(diào)用get接口

 數(shù)據(jù)也成功獲取到了

最后測試一下哨兵自動切換主從節(jié)點,這里關(guān)閉7002的redis

 接著查看27002哨兵打印的日志

 從日志中可以看到關(guān)閉7002的redis后,哨兵自動將主節(jié)點切換到了7001的redis

現(xiàn)在啟動7002的redis

 查看哨兵27001的日志

 可以發(fā)現(xiàn)由將7002加入到了自己的從節(jié)點中

自此,Redis哨兵模式的簡單搭建就完成了

到此這篇關(guān)于SpringBoot+Redis哨兵模式的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot Redis哨兵模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java lambda 循環(huán)累加求和代碼

    Java lambda 循環(huán)累加求和代碼

    這篇文章主要介紹了Java lambda 循環(huán)累加求和代碼,具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java中數(shù)組在內(nèi)存中存放原理的講解

    Java中數(shù)組在內(nèi)存中存放原理的講解

    今天小編就為大家分享一篇關(guān)于Java中數(shù)組在內(nèi)存中存放原理的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Spring MVC集成springfox-swagger2構(gòu)建restful API的方法詳解

    Spring MVC集成springfox-swagger2構(gòu)建restful API的方法詳解

    這篇文章主要給大家介紹了關(guān)于Spring MVC集成springfox-swagger2構(gòu)建restful API的相關(guān)資料,文中介紹介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • springBoot整合rabbitMQ的方法詳解

    springBoot整合rabbitMQ的方法詳解

    這篇文章主要介紹了springBoot整合rabbitMQ的方法詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Java內(nèi)存模型原子性原理及實例解析

    Java內(nèi)存模型原子性原理及實例解析

    這篇文章主要介紹了Java內(nèi)存模型原子性原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • JavaWeb開發(fā)之模仿知乎首頁完整代碼

    JavaWeb開發(fā)之模仿知乎首頁完整代碼

    這篇文章主要介紹了JavaWeb開發(fā)之模仿知乎首頁完整代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • 解決JavaEE開發(fā)中字符編碼出現(xiàn)亂碼的問題

    解決JavaEE開發(fā)中字符編碼出現(xiàn)亂碼的問題

    下面小編就為大家?guī)硪黄鉀QJavaEE開發(fā)中字符編碼出現(xiàn)亂碼的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • SpringBoot集成screw實現(xiàn)數(shù)據(jù)庫文檔生成的代碼示例

    SpringBoot集成screw實現(xiàn)數(shù)據(jù)庫文檔生成的代碼示例

    數(shù)據(jù)庫設(shè)計文檔是項目技術(shù)文檔的重要組成部分,Screw 是一款開源的數(shù)據(jù)庫文檔生成工具,它支持多種數(shù)據(jù)庫類型,并能生成豐富格式的文檔,本文將通過一個實際的例子,展示如何使用 Spring Boot 集成 Screw 生成數(shù)據(jù)庫設(shè)計文檔
    2024-07-07
  • ElasticSearch?深度分頁示例解析

    ElasticSearch?深度分頁示例解析

    這篇文章主要為大家介紹了ElasticSearch?深度分頁示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案

    Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案

    本文主要介紹了Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論