redis哨兵集群搭建及java应用

环境:windows

1 redis集群搭建

redis哨兵集群:一主(6380)两从(6381、6382)三哨兵(26379、26349、26359)

1.1 redis应用包

下载redis windows版应用包,如redis3.2;

删除多余文件

删除redis-server.pdb, redis-cli.pdb, redis-check-aof.pdb, redis-benchmark.pdb

删除Windows Service Documentation.docx, Redis on Windows.docx, Redis on Windows Release Notes.docx

删除redis.windows-service.conf

更名redis.windows.conf为redis.conf

创建sentinel.conf

1.2 redis一主两从配置

1.2.1 master6380

#端口号
port 6380
#开放外网访问权限
bind 0.0.0.0
#关闭protected-mode模式,此时外部网络可以直接访问;开启protected-mode保护模式,需配置bind ip或者设置访问密码
protected-mode yes
#访问密码
requirepass 123456
#访问主库密码
masterauth 123456

1.2.2 slave6381

#端口号
port 6381
#开放外网访问权限
bind 0.0.0.0
protected-mode yes
#主库ip及端口
slaveof 192.168.1.1 6380
requirepass 123456
masterauth 123456

其中slaveof 对应的为主库IP及端口

与6381一样,配置6382服务器,仅需要修改其中的port参数即可

1.3 redis哨兵配置

在redis一主两从三个服务文件夹内各创建一个sentinel.conf文件,配置内容如下

#端口号
port 26379
#绑定ip
protected-mode yes
#对外网开放
bind 0.0.0.0
#监听master主库地址及端口,并且配置认为主库宕机的阈值,mymaster主服务器名称
sentinel monitor mymaster 192.168.194.94 6380 1
#心跳阈值3秒
sentinel down-after-milliseconds mymaster 3000
#过期时间
sentinel failover-timeout mymaster 10000
#访问主库密码
sentinel auth-pass mymaster 123456
#打印日志地址,如果注释掉则打印在控制台
logfile "G:/rediscluster/6380/sentinel.log"

1.4 测试

(1)shutdown主服务器

(2)使用info replication查看另外两台服务器的主从状态

2 启动脚本

#启动redis服务
redis-server redis.conf
#启动对应sentinel哨兵服务
redis-server sentinel.conf --sentinel

3 spring应用哨兵集群

3.1 spring配置文件(RedisSentinel.xml)

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

	<description>JedisSentinelPool</description>

    <bean id="jedisPool" class="com.util.redis.JedisSentinelPoolUtil" init-method="init" destroy-method="destroy">
        <constructor-arg type="java.lang.String" value="mymaster" index="0"/>
        <constructor-arg type="java.lang.String" value="123456" index="1"/>
        <constructor-arg>
        	<list>
        		<value>192.168.1.1:26379</value>
        		<value>192.168.1.1:26479</value>
        		<value>192.168.1.1:26579</value>
        	</list>
        </constructor-arg>
    </bean>

    <bean id="redisClient" class="com.util.redis.RedisClient">
        <constructor-arg name="jedisPool" ref="jedisPool"/>
        <property name="expire" value="3000"/>
    </bean>
</beans>

在ApplicationContext.xml配置文件中注入以上配置文件

<import resource="classpath*:spring/RedisSentinel.xml"/>

3.2 JedisSentinelPoolUtil的实现

package com.util.redis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

/**
 * 测试Redis哨兵模式
 * 
 * @author liu
 */
public class JedisSentinelPoolUtil {

	// jdeis工具接口
	public JedisSentinelPool pool = null;
	private Integer maxActive = 10000;
	private Integer maxIdle = 100;
	private Integer maxWait = 100;
	private Integer timeBetweenEvictionRunsMillis = 30000;
	private Integer numTestsPerEvictionRun = -1;
	private boolean testOnBorrow = true;
	private String masterName = "mymaster";
	private String password = "123456";
	private List<String> sentinelIPS = new ArrayList<String>();

	public JedisSentinelPoolUtil() {
		super();
	}

	public JedisSentinelPoolUtil(String masterName, String password,
			List<String> sentinelIPS) {
		super();
		this.masterName = masterName;
		this.password = password;
		this.sentinelIPS = sentinelIPS;
	}

	public void init() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(maxActive);
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMinIdle(maxWait);
		jedisPoolConfig.setTestOnBorrow(testOnBorrow);
		jedisPoolConfig
				.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
		// 哨兵信息
		Set<String> sentinels = new HashSet<>(sentinelIPS);
		if (pool == null) {
			// 创建连接池
			pool = new JedisSentinelPool(masterName, sentinels,
					jedisPoolConfig, password);
		}
	}

	public void destroy() {
		if (pool != null) {
			pool.destroy();
		}
		if (pool != null) {
			pool.close();
		}
	}
}

3.3 应用的使用RedisClient

public class RedisClient extends RedisManager {
	
    // 线程池
    private JedisSentinelPool jedisPool = null;
	
	public RedisClient(JedisSentinelPoolUtil jedisPool) {
		this.jedisPool = jedisPool.pool;
	}

	public void init() {
		super.init();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值