Redis搭建一主两从三哨兵

1、参考“Redis搭建一主两从”,完成一主两从搭建

2、复制一份sentinel.conf文件为sentinel-26379.conf

cp sentinel.conf sentinel-26379.conf

3、修改sentinel-26379.conf相关配置

port 26379
daemonize yes
pidfile /var/run/redis‐sentinel‐26379.pid
logfile 26379.log
dir /usr/local/redis‐6.2.4/data
# sentinel monitor <master‐name> <ip> <redis‐port> <quorum>
# quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
sentinel monitor mymaster 192.168.0.60 6379 2

4、复制一份sentinel-26379.conf文件为sentinel-26380.conf

5、修改sentinel-26380.conf相关配置

port 26380
pidfile /var/run/redis‐sentinel‐26380.pid
logfile 26380.log

6、复制一份sentinel-26380.conf文件为sentinel-26381.conf

7、修改sentinel-26381.conf相关配置

port 26381
pidfile /var/run/redis‐sentinel‐26381.pid
logfile 26381.log

8、启动sentinel哨兵实例

src/redis‐sentinel sentinel‐26379.conf
src/redis‐sentinel sentinel‐26380.conf
src/redis‐sentinel sentinel‐26381.conf

9、测试

依赖

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

代码

package com.example;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

/**
 * 一主两从三哨兵
 */
public class JedisSentinelTest {


    public static void main(String[] args) {

        JedisPoolConfig config = new JedisPoolConfig();

        String masterName = "mymaster";

        Set<String> sentinels = new HashSet<>();
        sentinels.add(new HostAndPort("192.168.229.128", 26379).toString());
        sentinels.add(new HostAndPort("192.168.229.128", 26380).toString());
        sentinels.add(new HostAndPort("192.168.229.128", 26381).toString());

        JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config, 3000, null);

        try (Jedis jedis = jedisSentinelPool.getResource()) {
            //nx:not exists, 只有key 不存在时才把key value set 到redis
            //xx:is exists ,只有 key 存在是,才把key value set 到redis
            //ex:seconds 秒
            //px:milliseconds 毫秒
            System.out.println(jedis.set("test", "222", "xx", "ex", 30));
            System.out.println(jedis.get("test"));
        } catch (Exception e) {
            System.out.println("捕获到Exception");
            e.printStackTrace();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值