1.效果
redis版本
1.1 主備效果
2.搭建
2.1 搭建主備
2.1.1 搭建單機版
https://my.oschina.net/springMVCAndspring/blog/1922742
2.1.2 配置主備
# bind 127.0.0.1 # 自我保護 運行就設置no port 6379 # 主節點的密碼 一般所有的節點都一樣
|
cd /usr/local/develop/service/redis/bin
./redis-server redis.conf
./redis-cli -h localhost -p 6379 -a "123456"
info replication
2.2 搭建 sentinel
port 26379 sentinel deny-scripts-reconfig yes protected-mode no # 主節點 # 主節點的密碼 主備整體都一個祕密 |
./redis-sentinel sentinel.conf
info
3. springboot整合redis哨兵模式
3.1 引入redisTemplate
<!--13.springRedis操作jar--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.6.6</version> </dependency>
package cn.ma.utils; 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; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @Configuration public class RestTemplateConfig { @Resource private RedisTemplate redisTemplate; @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory) { //序列化問題 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000);// ms factory.setConnectTimeout(15000);// ms return factory; } }
# redis-single #spring.redis.host=localhost #spring.redis.port=6379 # spring.redis.password= ## redis-sentinel 哨兵模式 spring.redis.password=123456 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=192.168.179.129:26379,192.168.179.130:26379,192.168.179.131:26379 # 集羣配置 https://my.oschina.net/springMVCAndspring/blog/2413598
應用
4. 附件
鏈接:https://pan.baidu.com/s/1BKVj9MkbeSNIvMwcIVgTVw
提取碼:duuf