最近學習到了Redis的哨兵模式,光看視頻還不行,需要自己動手實現一遍才能加深映像,特此記錄。
由于沒有真實的服務器可以供我操作,所以在虛擬機上啟動了3個redis服務,分別占用7001、7002、7003端口。
Redis下載安裝不多贅述,只在這里記錄一下配置。
首先在tmp目錄下創建3個文件夾:
cd /tmp
mkdir 7001 7002 7003
然后將redis的配置文件redis.conf拷貝到剛剛創建的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的從節點
REPLICAOF #{ip} 7001
注意:這里#{ip}填自己的ip地址
同理,7003也這樣操作一遍,這樣就搭建好了以7001為主節點,7002和7003位從節點的主從集群模式。
需要注意的是,以命令形式搭建的主從集群,重啟后就失效了,想要持久保持可以在配置文件里配置,這里從簡就不貼了。
上述操作完成后,接著在tmp目錄下創建3個新文件夾
mkdir s1 s2 s3
cd到s1目錄,創建文件sentinel.conf,這個文件也可以從redis的目錄中拷貝。
編寫文件配置
# sentinel端口
port 27001
#工作路徑
dir "/tmp/s1"
# 哨兵監控的master,主從配置一樣,在進行主從切換時7001會變成當前的master端口,最后的2為客觀判斷主節# 點下線的節點個數
sentinel monitor mymaster #{ip} 7001 2
# master或slave多長時間不能使用后標記為s_down狀態
sentinel down-after-milliseconds mymaster 5000
#若sentinel在該配置值內未能完成failover操作(即故障時master/slave自動切換),
#則認為本次failover失敗
sentinel failover-timeout mymaster 60000
注意:這里#{ip}填自己的ip地址
然后將sentinel.conf文件cp到s2和s3路徑下,只用修改port和dir為各自的配置
然后分別在各自路徑下啟動3個哨兵
redis-sentinel sentinel.conf
由于之前測試了7001關閉服務,哨兵自動切換主節點為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,實現redis讀寫分離,配置從redis讀數據時優先從從節點讀取
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,調用set接口
查看redis
7002主節點有值了,并且它的從節點也同步到了數據
然后調用get接口
數據也成功獲取到了
最后測試一下哨兵自動切換主從節點,這里關閉7002的redis
接著查看27002哨兵打印的日志
從日志中可以看到關閉7002的redis后,哨兵自動將主節點切換到了7001的redis
現在啟動7002的redis
查看哨兵27001的日志
可以發現由將7002加入到了自己的從節點中
自此,Redis哨兵模式的簡單搭建就完成了
到此這篇關于SpringBoot+Redis哨兵模式的實現示例的文章就介紹到這了,更多相關SpringBoot Redis哨兵模式內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!