系统环境: Centos7 vmware
redis版本:redis3.2.5
redis3.2.5需要高版本的ruby和gem,我这边ruby用的最新版本2.3.3,低版本的ruby创建集群的时候会出错
安装步骤:
一、所需工具初始化
1、安装开发包
#yum install openssl* openssl-devel zlib-devel gcc gcc-c++ make autoconf readline-devel curl-devel expat-devel gettext-devel
openssl、zlib、gcc在安装ruby和gem的时候需要,后面的可以不需要
2、安装ruby
# wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.gz
# tar zxvf ruby-2.3.3.tar.gz
# cd ruby-2.3.3
# ./configure
# make && make install
# ruby -v
3、安装redis-gem
# wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
# gem install redis-3.2.1.gem
二、rediscluster构建
1、redis下载安装
# wget http://download.redis.io/releases/redis-3.2.5.tar.gz
# tar -zxf redis-3.2.5.tar.gz
# cd redis-3.2.5
# make && make install
2、集群配置
创建6个redis.conf文件(cluster最少需要6个节点)
# mkdir conf
# cd conf
# vi redis-6379.conf
port 6379
daemonize yes
dir /data/redis/redis-6379
logfile /data/redis/redis-6379/redis-6379.log
pidfile /var/run/redis-6379.pid
dbfilename dump-6379.rdb
appendonly yes
appendfilename "appendonly-6379.aof"
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
protected-mode no#关闭保护模式
maxmemory 1073741824#最大内存
maxmemory-policy volatile-lru#内存不足时,数据清除策略
# cp redis-6379.conf redis-6380.conf && sed -i "s/6379/6380/g" redis-6380.conf
# cp redis-6379.conf redis-6381.conf && sed -i "s/6379/6381/g" redis-6381.conf
# cp redis-6379.conf redis-6382.conf && sed -i "s/6379/6382/g" redis-6382.conf
# cp redis-6379.conf redis-6383.conf && sed -i "s/6379/6383/g" redis-6383.conf
# cp redis-6379.conf redis-6384.conf && sed -i "s/6379/6384/g" redis-6384.conf
说明:protected-mode 功能参考如下链接
3、启动服务*
# ./src/redis-server conf/redis-6379.conf
# ./src/redis-server conf/redis-6380.conf
# ./src/redis-server conf/redis-6381.conf
# ./src/redis-server conf/redis-6382.conf
# ./src/redis-server conf/redis-6383.conf
# ./src/redis-server conf/redis-6384.conf
4、创建集群
使用第二个(IP)创建,用127.0.0.1创建的jediscluster外部网络连接不上;
–replicas 1的意思是每个master有1个slave
# ./src/redis-trib.rb create --replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
# ./src/redis-trib.rb create --replicas 1 172.21.20.25:6379 172.21.20.25:6380 172.21.20.25:6381 172.21.20.25:6382 172.21.20.25:6383 172.21.20.25:6384
5、集群检测
#./src/redis-trib.rb check 172.21.20.10:6379
三、客户端链接
客户端采用的jediscluster,采用的spring boot
1、maven jar
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version><!--$NO-MVN-MAN-VER$-->
</dependency>
2、bean注入
package com.hive.data.redis.config;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Configuration
@EnableConfigurationProperties(RedisSettings.class)
public class JedisClusterConfig {
@Autowired
private RedisSettings redisSettings;
@Bean
public JedisCluster jedisCluster() {
Set<HostAndPort> set = new HashSet<HostAndPort>();
List<String> hostPorts = redisSettings.getNodes();
if (null != hostPorts && hostPorts.size() > 0) {
String[] ipPortPair;
for (String hostPort : hostPorts) {
ipPortPair = hostPort.split(":");
set.add(new HostAndPort(ipPortPair[0], Integer.parseInt(ipPortPair[1])));
}
}
return new JedisCluster(set,300000);
}
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig(){
GenericObjectPoolConfig config=new GenericObjectPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000*100);
config.setTestOnBorrow(true);
return config;
}
}
package com.hive.data.redis.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@ConfigurationProperties(locations = "classpath:redis.properties",prefix = "redis.cluster")
public class RedisSettings {
private List<String> nodes;
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
}
redis.properties
redis.cluster.nodes[0] =172.21.20.25:6379
redis.cluster.nodes[1] =172.21.20.25:6380
redis.cluster.nodes[2] =172.21.20.25:6381
redis.cluster.nodes[3] =172.21.20.25:6382
redis.cluster.nodes[4] =172.21.20.25:6383
redis.cluster.nodes[5] =172.21.20.25:6384
四、防火墙关闭
我的虚拟机是centos7的默认防火墙,7以下默认是iptables的。
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
iptables设置方法
重启后永久性生效:
开启:chkconfig iptables on
关闭:chkconfig iptables off
即时生效,重启后失效:
开启:service iptables start
关闭:
service iptables stop
重启:service iptables restart
保存配置:service iptables save
或者/etc/rc.d/init.d/iptables save
设置防火墙开机启动
systemctl enable iptables.service
禁止防火墙在系统启动时启动
/sbin/chkconfig –level 2345 iptables off
参考文献:
cluster密码设置> http://www.07net01.com/2016/10/1691935.html