1.# yum install ruby -y
1.1 后面需要用到ruby脚本
2.# yum install rubygems -y
1.1 安装ruby包管理器
3.# gem install redis-3.0.0.gem
3.1 脚本需要ruby其他包,所以安装这个redis.gem
4.# mkdir reids-cluster
4.1 在/usr/local中新建redis-cluster文件夹
5.# cp -r bin ../redis-cluster/redis01
5.1 把之前安装好的redis/bin复制到redis-cluster中并起名为redis01
6.# rm -rf dump.rdb
6.1 删除掉redis01 中dump.rdb数据库文件
7.# vi redis.conf
7.1 修改redis01中端口号为7001, 找到port 后面修改为7001
7.2 去掉cluster-enabled yes前面的注释
7.3 如果之前设置过密码,注释掉密码.如果没有设置过过略7.3这步骤
8.# cp -r redis01 redis02
cp -r redis01 redis03
cp -r redis01 redis04
cp -r redis01 redis05
cp -r redis01 redis06
8.1 把redis01文件夹在复制5份,分别起名为redis02,redis03,redis04,redis05,redis06
9# vi redis02/redis.conf
9.1 此命令需要在redis-cluster下执行
9.2 把其他5个文件夹中redis.conf中port修改成不同的值,分别为7002,7003,7004,7005,7006
9.2 设置cluster-config-file nodes-700*.conf用于节点之间的连接
9.3 设置 appendonly yes
10.# cp *.rb /usr/local/redis-cluster/
10.1 去redis解压目录中src下执行此命令
10.2 把redis-trib.rb复制到reids-cluster中.
11.# vi startall.sh
11.1 创建一个批量启动文件
11.2 把下面内容粘贴到文件中
cd redis01 ./redis-server redis.conf cd .. cd redis02 ./redis-server redis.conf cd .. cd redis03 ./redis-server redis.conf cd .. cd redis04 ./redis-server redis.conf cd .. cd redis05 ./redis-server redis.conf cd .. cd redis06 ./redis-server redis.conf cd .. |
12.# chmod +x startall.sh
12.1 给脚本设置一个可启动权限
13.# ./startall.sh
13.1 执行脚本,启动所有redis服务
14.# ps aux|grep redis
14.1 查看所有服务是否启动成功
15.# ./src/redis-cli --cluster create 192.168.48.136:7001 192.168.48.136:7002 192.168.48.136:7003 192.168.48.136:7004 192.168.48.136:7005 192.168.48.136:7006 --cluster-replicas 1
15.1 创建集群
15.2 在执行时按照提示输入’yes’
16.# ./redis01/redis-cli -h 192.168.10.128 -p 7001 -c(-h表示ip,-p表示端口,-c表示集群模式连接)
16.1 进入任意节点测试
17.# redis01/redis-cli -p 7001 shutdown
17.1 关闭其中一个redis
18.# vi shutdown.sh
18.1 在redis-cluster中创建文件,并添加下面内容
./redis01/redis-cli -p 7001 shutdown ./redis02/redis-cli -p 7002 shutdown ./redis03/redis-cli -p 7003 shutdown ./redis04/redis-cli -p 7004 shutdown ./redis05/redis-cli -p 7005 shutdown ./redis06/redis-cli -p 7006 shutdown |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis-cluster.properties" />
<context:component-scan base-package="com">
</context:component-scan>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
</bean> <bean id="hostport1" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host1}" />
<constructor-arg name="port" value="${redis.port1}" />
</bean>
<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host2}" />
<constructor-arg name="port" value="${redis.port2}" />
</bean>
<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host3}" />
<constructor-arg name="port" value="${redis.port3}" />
</bean>
<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host4}" />
<constructor-arg name="port" value="${redis.port4}" />
</bean>
<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host5}" />
<constructor-arg name="port" value="${redis.port5}" />
</bean>
<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host6}" />
<constructor-arg name="port" value="${redis.port6}" />
</bean> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<ref bean="hostport1" />
<ref bean="hostport2" />
<ref bean="hostport3" />
<ref bean="hostport4" />
<ref bean="hostport5" />
<ref bean="hostport6" />
</set>
</constructor-arg>
<constructor-arg name="timeout" value="6000" />
<constructor-arg name="poolConfig">
<ref bean="jedisPoolConfig" />
</constructor-arg>
</bean>
</beans>
@Test
public void testcluster() { // ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/applicationContext-redis-cluster.xml");
// JedisCluster = (JedisCluster) ac.getBean("redisCluster");
// ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); for (int i=0; i<100; i++) {
jedisCluster.set("name"+i+i,"value"+i);
} System.out.println(jedisCluster.get("name4"));
}