用外网搭建redis集群总结
一、在配置文件redis-cluster.conf配置内网
二、在云主机的防火规则开放端口:8000、8001、8002、8003、8004、8005和18000、18001、18002、18003、18004、18005
- 三、用外网ip搭建集群节点的三主三从脚本
for port in {8000..8005};do redis-cli -c -p $port shutdown;done;
rm -rf dump*;
rm -rf appendonly*;
rm -rf nodes*;
for port in {8000..8005};do redis-server $port/redis-cluster.conf;done;
echo yes|./src/redis-trib.rb create --replicas 1 116.75.179.72:8000 116.75.179.72:8001 116.75.179.72:8002 116.75.179.72:8003 116.75.179.72:8004 116.75.179.72:8005;
四、用外网ip连接集群测试
package cn.tedu;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
public class JedisClusterTest {
@Test
public void test01(){
//收集集群的节点信息,至少要提供一个可连接的节点
Set<HostAndPort> set=new HashSet<HostAndPort>();
set.add(new HostAndPort("116.75.179.72",8001));
//内部封装连接池,可以提供一个连接池配置对象
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(8);
config.setMinIdle(3);
config.setMaxTotal(200);
//使用上述2个参数构造一个集群连接对象jedisCluster
JedisCluster cluster=new JedisCluster(set,config);
//调用cluster方法 底层发送redis命令 set get hset hget lpush..
cluster.set("name9","王老师9");
System.out.println(cluster.get("name9"));
}
}
五、项目的userservice系统配置用外网ip连接:
server.port=10003
server.context-path=/
#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///easydb
spring.datasource.username=root
spring.datasource.password=root
#sqlSession classpath:==roottarget/classes src/main/resources/mapper
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.jt.common.pojo
#mybatis single config
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.cache-enabled=false
#eureka config
spring.application.name=userservice
eureka.client.service-url.defaultZone=http://localhost:8888/eureka
#redis config
redis.nodes=116.75.179.73:6379,116.75.179.73:6380,116.75.179.72:6381
redis.maxTotal=200
redis.maxIdle=8
redis.minIdle=5
#cluster config
cluster.nodes=116.75.179.72:8000,116.75.179.72:8001,116.75.179.72:8002
cluster.maxTotal=200
cluster.maxIdle=8
cluster.minIdle=5