您的位置:首页 > 数据库 > Redis

java鬼混笔记:redis 5、redis哨兵模式配置

2017-12-16 14:47 561 查看
记录简单的配置:

先在redis安装文件夹下添加3个文件(我开了3个redis):sentinel.conf、sentinel.6380.conf、sentinel.6381.conf

然后内容分别如下:

sentinel.conf:

# 访问的端口,不是redis的端口号
port 63791
# 关闭  protected-mode
protected-mode no
# master 主机的IP地址和端口 后面的1是指 主机挂了,有1个投票就选举出一个主服务器,masterName 是自定义的名字
sentinel monitor masterName 192.168.1.103 6381 1
#日志
logfile "sentinel6379.txt"
# 如果master在多少秒内无反应哨兵会开始进行master-slave间的切换
sentinel down-after-milliseconds masterName 3000
# 设置2个从机可以切换主机
sentinel parallel-syncs masterName 2


sentinel.6380.conf:(端口号,日志不同而已)

# 访问的端口,不是redis的端口号
port 63801
# 关闭  protected-mode
protected-mode no
# master 主机的IP地址和端口 后面的1是指 主机挂了,有1个投票就选举出一个主服务器,masterName 是自定义的名字
sentinel monitor masterName 192.168.1.103 6381 1
#日志
logfile "sentinel6380.txt"
# 如果master在多少秒内无反应哨兵会开始进行master-slave间的切换
sentinel down-after-milliseconds masterName 3000
# 设置2个从机可以切换主机
sentinel parallel-syncs masterName 2


sentinel.6381.conf(也是端口,日志号不同)

# 访问的端口,不是redis的端口号
port 63811
# 关闭  protected-mode
protected-mode no
# master 主机的IP地址和端口 后面的1是指 主机挂了,有1个投票就选举出一个主服务器,masterName 是自定义的名字
sentinel monitor masterName 192.168.1.103 6381 1
#日志
logfile "sentinel6381.txt"
# 如果master在多少秒内无反应哨兵会开始进行master-slave间的切换
sentinel down-after-milliseconds masterName 3000
# 设置2个从机可以切换主机
sentinel parallel-syncs masterName 2


保存完毕。

接着按顺序 分别 启动:主机、从机、哨兵服务。

提示,我用的是windows redis,所以哨兵模式启动的方式是在redis目录下cmd,分别执行

redis-server.exe sentinel.conf --sentinel

redis-server.exe sentinel.6380.conf --sentinel

redis-server.exe sentinel.6381.conf --sentinel

OK。

测试:先在主机上set name ywj, 从机可以获取,然后关闭 主机 服务, 通过客户端 命令 info replication可看见 6381的从机变成了主机了。。。。

java测试代码:

package com.example.demo.utils;

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

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

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

public class T3 {

public static void main(String[] args) {

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10);
jedisPoolConfig.setMaxIdle(5);
jedisPoolConfig.setMinIdle(5);

Set<String> sentinels = new HashSet<String>();
sentinels.add("192.168.1.103:63791");
sentinels.add("192.168.1.103:63801");
sentinels.add("192.168.1.103:63811");

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(10);
poolConfig.setMaxIdle(5);
poolConfig.setMinIdle(5);

JedisSentinelPool pool = new JedisSentinelPool("master", sentinels, jedisPoolConfig);

String key1 = "key1";
try (Jedis jedis = pool.getResource()) {
jedis.set(key1, "222");
System.out.println(jedis.get(key1));
} catch (Exception e) {
e.printStackTrace();
}
}

}


OK。。。简单配置完毕。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: