123. redis搭建 哨兵模式 及整合springboot

 

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
protected-mode no

port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
# 默認值:no;是否開啓守護進行啓動redis
daemonize yes

supervised no
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/develop/service/redis/bin"

# 主節點的密碼 一般所有的節點都一樣
masterauth "123456"
# 從節點配置主節點
 slaveof 192.168.179.129 6379

 
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
#密碼
requirepass "123456"


lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
 

 

 

 

 

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
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile ""
dir "/tmp"

sentinel deny-scripts-reconfig yes

protected-mode no

# 主節點
sentinel monitor mymaster 192.168.179.129 6379 1

# 主節點的密碼  主備整體都一個祕密
sentinel auth-pass mymaster 123456

 

 

./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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章