前提:下载好redis-4.0.6.tar.gz
1、解压tar zxf redis-4.0.6.tar.gz
cd redis-4.0.6
make & make install PREFIX=/usr/local/redis-cluster
在redis-cluster下修改bin文件夹为Redis01,复制redis.conf配置文件,修改以下配置:
daemonize yes #后台启动
port 7001 #修改端口号,从7001到7006
cluster-enabled yes #开启cluster,去掉注释
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
复制六份,修改对应端口号
2、安装redis-trib所需的ruby脚本
复制redis解压文件下src下的redis-trib.rb到redis-cluster目录
安装ruby环境:yum install ruby、yum install rubygems
安装redis-trib-rb运行依赖的redis-3.2.2.gem:gem install redis-3.2.2.gem
启动所有的redis节点
使用redis-trib.rb创建集群
./redis-trib.rb create --replicas 1 127.0.0.1:7001 之后是其他五个节点,空格分割
3、spring和redis集群的集成
maven中依赖的jar
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
配置文件:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="1000"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="1"/>
<property name="maxWaitMillis" value="30000"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<!-- <property name="testWhileIdle" value="true"/> -->
</bean>
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.229.131"/>
<constructor-arg name="port" value="7000"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.229.131"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.229.131"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.229.132"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.229.132"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.229.132"/>
<constructor-arg name="port" value="7005"/>
</bean>
</set>
</constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>
代码中使用:
@Autowired
private JedisCluster jedisCluster;