Redis集群服务

Categories:

Redis单机版能够起到一定的减压效果,必要情况下也需要配置Redis-Cluster。利用Redis集群,将对Redis的访问分配到各个结点,以减小单个结点的压力。Redis集群节点分为主节点和从节点,一个主节点可以有多个从节点,当主节点宕机时,从节点可以暂时作为主节点来工作。


环境

集群服务需要ruby运行环境

yum install ruby

将ruby运行脚本上传后 redis-3.2.2.gem

gem install redis-3.2.2.gem

安装

安装方法同单服务一样,但是安装集群需要每个节点都是空的,所以不能复制之前的redis使用

images
配置

依次修改每个redis下bin目录中的redis.conf

注释:

images
修改:

images修改:

images修改:

images

去掉注释:

images
修改:

images

设置密码

依次修改每个redis/bin下的redis.conf,在结尾处追加

masterauth  123456

requirepass  123456

开启

在redis-cluster目录下创建集群启动脚本:start-cluster.sh

cd redis01/bin
./redis-server redis.conf
cd ../../redis02/bin
./redis-server redis.conf
cd ../../redis03/bin
./redis-server redis.conf
cd ../../redis04/bin
./redis-server redis.conf
cd ../../redis05/bin
./redis-server redis.conf
cd ../../redis06/bin
./redis-server redis.conf
cd ../../

授权文件可执行

chmod u+x start-cluster.sh

 

执行./start-cluster.sh开启所有redis

创建集群

将ruby脚本redis-trib.rb复制到redis-cluster文件夹下

cp /usr/local/software/redis-3.2.11/src/redis-trib.rb /usr/local/software/redis-cluster

开启所有redis.conf中配置的端口,并将端口+10000号同时开启,例redis.conf中为7000端口,则需开启7000/17000端口

安装集群

./redis-trib.rb create --replicas 1 ip:port ip:port ......

注:ip不能使用127.0.0.1,否侧远程无法连接集群,且不支持域名

连接

连接集群redis需要在任意redis/bin目录下执行

./redis-cli -p port -c

-p:端口号  -c:集群  -a:密码

打印集群信息

cluster info

查看集群所有节点(node)

cluster nodes

关闭

在redis-cluster目录下创建集群关闭脚本:stop-cluster.sh

redis01/bin/redis-cli -p 6381 shutdown
redis01/bin/redis-cli -p 6382 shutdown
redis01/bin/redis-cli -p 6383 shutdown
redis01/bin/redis-cli -p 6384 shutdown
redis01/bin/redis-cli -p 6385 shutdown
redis01/bin/redis-cli -p 6386 shutdown

授权文件可执行

chmod u+x stop-cluster.sh

 

java使用集群

测试代码

/**
 * 集群redis无密码,原生测试连接
 * @throws Exception
 */
@Test
public void testJedisCluster() throws Exception {
	// 第一步:需要一个Set<HostAndPort>参数,存放Redis节点的列表。
	Set<HostAndPort> nodes = new HashSet<>();
	nodes.add(new HostAndPort("10.0.107.157", 6381));
	nodes.add(new HostAndPort("10.0.107.157", 6382));
	nodes.add(new HostAndPort("10.0.107.157", 6383));
	nodes.add(new HostAndPort("10.0.107.157", 6384));
	nodes.add(new HostAndPort("10.0.107.157", 6385));
	nodes.add(new HostAndPort("10.0.107.157", 6386));
	JedisCluster jedisCluster = new JedisCluster(nodes);
	// 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
	System.out.println(jedisCluster.exists("hello"));
	// 第三步:系统关闭前,关闭JedisCluster对象。
	jedisCluster.close();
}

/**
 * 集群redis有密码,原生测试连接
 * @throws Exception
 */
@Test
public void testJedisClusterWithPassword() throws Exception {
	// 第一步:需要一个Set<HostAndPort>参数,存放Redis节点的列表。
	Set<HostAndPort> nodes = new HashSet<>();
	nodes.add(new HostAndPort("10.0.107.157", 6381));
	nodes.add(new HostAndPort("10.0.107.157", 6382));
	nodes.add(new HostAndPort("10.0.107.157", 6383));
	nodes.add(new HostAndPort("10.0.107.157", 6384));
	nodes.add(new HostAndPort("10.0.107.157", 6385));
	nodes.add(new HostAndPort("10.0.107.157", 6386));
	JedisPoolConfig config = new JedisPoolConfig();
	JedisCluster jedisCluster = new JedisCluster(nodes, 3000, 1000, 5, "123456", config);

	// 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。
	System.out.println(jedisCluster.exists("hello"));
	// 第三步:系统关闭前,关闭JedisCluster对象。
	jedisCluster.close();
}

/**
 * 集群redis无密码,spring测试连接
 * @throws Exception
 */
@Test
public void testSpringJedisCluster() throws Exception{
	ApplicationContext ac = new ClassPathXmlApplicationContext("spring-redis.xml");
	JedisCluster jedisCluster = (JedisCluster)ac.getBean("jedisCluster");
	System.out.println(jedisCluster.exists("hello"));
	jedisCluster.close();
}

/**
 * 集群redis有密码,spring测试连接
 * @throws Exception
 */
@Test
public void testSpringJedisClusterWithPassword() throws Exception{
	ApplicationContext ac = new ClassPathXmlApplicationContext("spring-redis.xml");
	JedisCluster jedisCluster = (JedisCluster)ac.getBean("jedisClusterPwd");
	System.out.println(jedisCluster.exists("hello"));
	jedisCluster.close();
}

 

spring-redis.xml 注:两个JedisPool不能同时开启

<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
	<constructor-arg>
		<set>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6381"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6382"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6383"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6384"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6385"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6386"></constructor-arg>
			</bean>
		</set>
	</constructor-arg>
</bean>

<bean id="jedisClusterPwd" class="redis.clients.jedis.JedisCluster">
	<constructor-arg name="jedisClusterNode">
		<set>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6381"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6382"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6383"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6384"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6385"></constructor-arg>
			</bean>
			<bean class="redis.clients.jedis.HostAndPort">
				<constructor-arg name="host" value="10.0.107.157"></constructor-arg>
				<constructor-arg name="port" value="6386"></constructor-arg>
			</bean>
		</set>
	</constructor-arg>
	<constructor-arg name="password" value="123456"/>
	<constructor-arg name="connectionTimeout" value="3000"/>
	<constructor-arg name="soTimeout" value="1000"/>
	<constructor-arg name="maxAttempts" value="5"/>
	<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>

 

pom.xml

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<!--Jedis的jar-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<!--Spring基础jar包-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.11.RELEASE</version>
</dependency>