用外網搭建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