redis 4.14哨兵模式搭建及Spring boot配置

环境

  • redis:4.0.14
  • linux
  • 1主2从

安装

下载redis

redis-4.0.14.tar.gz

配置redis

解压 tar -xzvf redis-4.0.14.tar.gz

编译
cd redis-4.0.14/

执行make

结束后,在redis-4.0.14新建config文件夹
mkdir config

拷贝redis配置文件

cp redis.conf ../config/redis-6301.conf
cp redis.conf ../config/redis-6302.conf
cp redis.conf ../config/redis-6303.conf

配置三个redis.conf

vim redis-6301.conf

主节点

//服务器ip
#bind 192.168.18.128
protected-mode no
port 6301
<!--后台启动-->
daemonize yes
supervised no
pidfile "/var/run/redis_6301.pid"
dbfilename "6301dump.rdb"
masterauth "redis"

从节点添加

//主节点ip port
slaveof 192.168.18.128 6301

另外修改对应端口

启动

./redis-server ../config/redis-6301.conf
./redis-server ../config/redis-6302.conf 
./redis-server ../config/redis-6303.conf 

查看

./redis-cli -h 192.168.18.128 -p 6301

<!--验证-->
auth redis
<!--显示信息-->
info

显示主从信息

# Replication
role:master  //主节点
connected_slaves:2
slave0:ip=192.168.18.128,port=6301,state=online,offset=6359422,lag=1
slave1:ip=192.168.18.128,port=6302,state=online,offset=6359279,lag=1
master_replid:82dd642ad60846cd858cfa20e86cda7c4a043881
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6359565
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5310990
repl_backlog_histlen:1048576

哨兵模式配置

配置项

port 26301
sentinel monitor mymaster 192.168.18.128 6301 2
sentinel auth-pass mymaster redis
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 80000
bind 192.168.18.128
protected-mode no
daemonize yes //允许后台启动

向config文件夹中拷贝三个配置文件

cp sentinel.conf ../config/sentinel-6301.conf
cp sentinel.conf ../config/sentinel-6302.conf
cp sentinel.conf ../config/sentinel-6303.conf

从节点修改端口26302,26303

启动

./redis-sentinel ../config/sentinel-26301.conf
./redis-sentinel ../config/sentinel-26302.conf
./redis-sentinel ../config/sentinel-26303.conf

结果验证

查看进程
ps -ef|grep redis

验证下主从同步

kill -9 13028
查看主节点变更情况

spring boot集成

配置文件application.yml

spring:
    redis:
    #    url: 127.0.0.1   // 单机使用
    #    host: localhost
    #    port: 6379
        timeout: 3600
        password: redis
        sentinel:
              nodes: 192.168.18.128:26301,192.168.18.128:26302,192.168.18.128:26303
              master: mymaster

WebMvcConfigurer配置

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("#{'${spring.redis.sentinel.nodes}'.split(',')}")
    private List<String> nodes;
    @Value("${spring.redis.password}")
    private String redisPassword;
    @Value("${spring.redis.sentinel.master}")
    private String redisMaster;
    @Autowired
    private FastJsonHttpMessageConverter fastJsonHttpMessageConverter;


    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
        RestTemplateBuilder builder = new RestTemplateBuilder();
        return builder.messageConverters(fastJsonHttpMessageConverter);
    }

    @Bean
    public RestTemplate restTemplate() {
        return restTemplateBuilder().build();
    }


    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory(sentinelConfiguration());
    }
    @Bean
    public RedisSentinelConfiguration sentinelConfiguration(){
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        //配置matser的名称
        redisSentinelConfiguration.master(redisMaster);
        //配置redis的哨兵sentinel
        Set<RedisNode> redisNodeSet = new HashSet<>();
        nodes.forEach(x->{
            redisNodeSet.add(new RedisNode(x.split(":")[0],Integer.parseInt(x.split(":")[1])));
        });
        log.info("\nredisNodeSet -->{}",redisNodeSet);
        redisSentinelConfiguration.setSentinels(redisNodeSet);
        redisSentinelConfiguration.setPassword(RedisPassword.of(redisPassword));
        return redisSentinelConfiguration;
    }


    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

启动,连接成功会显示具体信息

redis.clients.jedis.JedisSentinelPool    : Trying to find master from available Sentinels...
redis.clients.jedis.JedisSentinelPool    : Redis master running at 192.168.18.128:6303, starting Sentinel listeners...
redis.clients.jedis.JedisSentinelPool    : Created JedisPool to master at 192.168.18.128:6303

连接异常情况

2019-04-04 09:55:44.961  INFO 24056 --- [           main] redis.clients.jedis.JedisSentinelPool    : Trying to find master from available Sentinels...
2019-04-04 09:55:44.973  WARN 24056 --- [           main] redis.clients.jedis.JedisSentinelPool    : Cannot get master address from sentinel running @ 192.168.18.1281:26302. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: 192.168.18.1281. Trying next one.
2019-04-04 09:55:44.973  WARN 24056 --- [           main] redis.clients.jedis.JedisSentinelPool    : Cannot get master address from sentinel running @ 192.168.18.1281:26301. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: 192.168.18.1281. Trying next one.
2019-04-04 09:55:44.973  WARN 24056 --- [           main] redis.clients.jedis.JedisSentinelPool    : Cannot get master address from sentinel running @ 192.168.18.1281:26303. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: 192.168.18.1281. Trying next one.

注意事项

防火墙端口开启

个人博客 > 欢迎来访

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏叶新城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值