1. 程式人生 > >java 集成Redis 一主多從

java 集成Redis 一主多從

scl ring ext solution 保護模式 ssi conn .net accep

1、測試代碼如下:

public static void main(String[] args) { 
         Set<String> sentinels = new HashSet<String>();
         sentinels.add("118.25.7.111:26379");
         sentinels.add("118.25.7.111:26380");
         sentinels.add("118.25.7.111:26381");
         String clusterName = "mymaster";
         
//此處添加密碼 String password = "foo" ; // 建立連接池配置參數 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(50); //設置最小空閑數 config.setMinIdle(8); config.setMaxWaitMillis(10000); config.setTestOnBorrow(true); config.setTestOnReturn(
true); //Idle時進行連接掃描 config.setTestWhileIdle(true); //表示idle object evitor兩次掃描之間要sleep的毫秒數 config.setTimeBetweenEvictionRunsMillis(30000); //表示idle object evitor每次掃描的最多的對象數 config.setNumTestsPerEvictionRun(10); //表示一個對象至少停留在idle狀態的最短時間,然後才能被idle object evitor掃描並驅逐;這一項只有在timeBetweenEvictionRunsMillis大於0時才有意義
config.setMinEvictableIdleTimeMillis(60000); JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName,sentinels,config,password); Jedis jedis = null; try { jedis = redisSentinelJedisPool.getResource(); jedis.set("key", "aaa"); System.out.println(jedis.get("key")); System.out.println(jedis.get("bbb")); } catch (Exception e) { e.printStackTrace(); } finally { //redisSentinelJedisPool.returnResource(jedis); jedis.close(); } redisSentinelJedisPool.close(); }

問題:

1、-DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no‘ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no‘, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘--protected-mode no‘ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

解決:此處是因為sentinel的保護模式開啟(默認)導致的,在sentinel對應的配置文件中將其關閉即可,即

#關閉保護模式
protected-mode no

2、redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisSentinelPool.getResource(JedisSentinelPool.java:209)
at com.ww.wwta.config.client.RedisClusterClient.main(RedisClusterClient.java:92)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 2 more

解決:未將對應的主服務的端口加入防火墻,將設置端口加入防火墻即可,即

firewall-cmd --permanent --zone=public --add-port=26380/tcp
firewall-cmd --reload

查看是否已加入:

firewall-cmd --permanent --zone=public --list-ports

3、連接127.0.0.1:6780連接拒絕

需將sentinel配置sentinel monitor mymaster 127.0.0.1 6380 2 -> sentinel monitor mymaster 118.25.7.111 6380 2

java 集成Redis 一主多從