阿里云上面装redis集群基本被虐死,主要问题就是私有IP和公有IP。
下面分享成功搭建的步骤:
两台测试服务器,分别为:127.0.0.1,127.0.0.2.每分服务器有3个节点。
1.127.0.0.1 创建节点
在目录/opt/redis-4.0.2 mkdir cluster
mkdir 6370 6371 6372
2.修改redis.conf
bind 0.0.0.0改为#bind 0.0.0.0
dir ./ 改为dir /opt/redis-4.0.2/cluster/data
daemonize yes //redis后台运行
cluster-enabled yes //开启集群 把注释#去掉
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
protected-mode no //(需要不同服务器的节点连通,这个就要设置为 no)
#bind 127.0.0.1 此行一定要注释掉 不然用公有IP创建集群会失败
做集群的时候以下两行一定要注释掉
# unixsocket /tmp/redis.sock
# unixsocketperm 700
复制配置文件
cp /opt/redis-4.0.2/redis.conf ./redis_cluster/6370/
cp /opt/redis-4.0.2/redis.conf ./redis_cluster/6371/
cp /opt/redis-4.0.2/redis.conf ./redis_cluster/6372/
# 根据不同端口需要设置的地方
port 6370
logfile /opt/redis-4.0.2/logs/redis_6370.log
dbfilename dump_6370.rdb
appendfilename "appendonly_6370.aof"
cluster-config-file nodes_6370.conf //集群的配置
第二个就可以使用命令整体替换
:%s/6370/6371/g
3.启动
./redis-server /opt/redis-4.0.2/cluster/6370/redis.conf &
./redis-server /opt/redis-4.0.2/cluster/6371/redis.conf &
./redis-server /opt/redis-4.0.2/cluster/6372/redis.conf &
4.127.0.0.2和127.0.0.1创建除端口之外都相同
ps -ef | grep redis #查看是否启动成功
netstat -tnlp | grep redis #可以看到redis监听端口
5.创建集群
安装rubygems
yum install rubygems [root@insure src]# gem install redis -v 3.3.
Fetching: redis-3.3..gem (%)
Successfully installed redis-3.3.
Parsing documentation for redis-3.3.
Installing ri documentation for redis-3.3.
gem installed
----------集群创建------------------
创建集群 一定要用公有IP,或者jediscluster程序连接会报超时
执行./redis-trib.rb create --replicas 1 *.16.120.*:6370 *.16.120.*:6371 *.16.120.*:6372 *.16.120.*:6373 *.16.120.*:6374 *.16.120.*:6375
测试连接
可以在*.*.*.189 连接看一下:
./redis-cli -h *.*.*.190 -p 6373
关键点:
redis.conf 中requirepass 设置密码
find / -name client.rb
/usr/local/rvm/gems/ruby-2.4.1/gems/redis-4.0.3/lib/redis 修改client.rb password改成设置的密码
:host => "172.16.*.*", #改成公有IP
:port => 6373,
:path => nil,
:timeout => 5.0,
:password => '******',
主从如果连接不成功
masterauth 设置你的密码
集群
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。
java代码
1. 引用文件
<!-- 添加jedis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.</version>
</dependency>
2. 配置文件
spring.redis.cluster.nodes=.*.8.8:,.*.8.8:,.*.8.8:,.*.8.8:,.*.8.8:,.*.8.8:
spring.redis.cluster.max-redirects=
spring.redis.password=P11111111111
spring.redis.connection_timeout=
spring.redis.timeout=
spring.redis.jedis.pool.max-total=
spring.redis.jedis.pool.max-wait=-
spring.redis.jedis.pool.max-idle=
spring.redis.jedis.pool.min-idle=
3.代码
@Configuration
@EnableCaching
public class RedisClusterConfiguration extends CachingConfigurerSupport {
private static Logger logger = LoggerFactory.getLogger(RedisClusterConfiguration.class); @Value("${spring.redis.cluster.nodes}")
private String clusterNodes; @Value("${spring.redis.connection_timeout}")
private int connectionTimeout; @Value("${spring.redis.timeout}")
private int timeout; @Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle; @Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle; @Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis; @Value("${spring.redis.password}")
private String password; @Value("${spring.redis.jedis.pool.max-total}")
private int maxTotal; @Value("${redis.max_attempts:10}")
private int maxAttempts; @Bean
public JedisCluster redisPoolFactory() {
String [] serverArray = clusterNodes.split(","); Set<HostAndPort> nodes = new HashSet<HostAndPort>();
for (String ipPort:serverArray){
String [] ipPortPair=ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[].trim(),Integer.valueOf(ipPortPair[].trim()))); }
//创建config
GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
pool.setMaxTotal(maxTotal);
pool.setMinIdle(minIdle);
pool.setMaxIdle(maxIdle);
pool.setMaxWaitMillis(maxWaitMillis);
//通过config创建集群实例
JedisCluster jedisCluster =
new JedisCluster(nodes, connectionTimeout, timeout, maxAttempts, password, pool);
logger.info("JedisPool注入成功");
jedisCluster.set("sys","testredis");
String str = jedisCluster.get("sys");
logger.info("获取redis里面的值:" + str);
return jedisCluster;
}
}