老师,我用jedis直连master没问题,但是连接sentinel就报错

来源:8-7 java客户端

闪光棒

2018-08-21


java代码

package com.mmall.common;

import com.google.common.collect.Sets;
import com.mmall.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.Set;

/**
* @author Luyue
* @date 2018/8/11 14:29
**/
public class SentinelJedisPool {
   private static JedisSentinelPool pool;

   //最大连接数
   private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getKey("redis.max.total", "20"));
   //最大空闲连接数
   private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getKey("redis.max.idle", "10"));
   //最小空闲连接数
   private static Integer minIdle = Integer.parseInt(PropertiesUtil.getKey("redis.min.idle", "2"));

   //通过连接池拿去jedis连接时,校验并返回可用连接
   private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getKey("redis.test.borrow", "true"));
   //通过连接池返还jedis连接时,校验该连接
   private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getKey("redis.test.return", "true"));

   /**
    * 初始化连接池
    */
   private static void initPool() {
       JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

       jedisPoolConfig.setMaxTotal(maxTotal);
       jedisPoolConfig.setMaxIdle(maxIdle);
       jedisPoolConfig.setMinIdle(minIdle);
       jedisPoolConfig.setTestOnBorrow(testOnBorrow);
       jedisPoolConfig.setTestOnReturn(testOnReturn);

       //当连接池无空闲连接时,是否阻塞
       jedisPoolConfig.setBlockWhenExhausted(true);

       Set<String> sentinels = Sets.newHashSet("106.12.20.136:26379", "106.12.20.136:26380", "106.12.20.136:26381");

       pool = new JedisSentinelPool("mymaster", sentinels, jedisPoolConfig, 2 * 1000);
   }

   static {
       initPool();
   }

   /**
    * 获取一个连接
    * @return
    */
   public static Jedis getJedis() {
       return pool.getResource();
   }

   /**
    * 返还错误的连接
    * @param jedis
    */
   public static void returnBrokenJedis(Jedis jedis) {
       pool.returnBrokenResource(jedis);
   }

   /**
    * 返还连接
    * @param jedis
    */
   public static void returnJedis(Jedis jedis) {
       pool.returnResource(jedis);
   }
}


//img.mukewang.com/szimg/5b7c0db40001cf2d17190766.jpg

信息: Trying to find master from available Sentinels...

Exception in thread "main" java.lang.ExceptionInInitializerError

at com.mmall.util.SentinelJedisPoolUtil.main(SentinelJedisPoolUtil.java:129)

Caused by: redis.clients.jedis.exceptions.JedisDataException: 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.

at redis.clients.jedis.Protocol.processError(Protocol.java:115)

at redis.clients.jedis.Protocol.process(Protocol.java:133)

at redis.clients.jedis.Protocol.read(Protocol.java:202)

at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:285)

at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:230)

at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:236)

at redis.clients.jedis.Jedis.sentinelGetMasterAddrByName(Jedis.java:3051)

at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:133)

at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:74)

at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:55)

at com.mmall.common.SentinelJedisPool.initPool(SentinelJedisPool.java:52)

at com.mmall.common.SentinelJedisPool.<clinit>(SentinelJedisPool.java:56)

... 1 more


redis-7000.conf(master):

port 7000

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile /var/run/redis-7000.pid

loglevel notice

logfile "7000.log"

databases 16

always-show-logo yes

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump-7000.rdb

dir /opt/redis/data/sentinel

slave-serve-stale-data yes

slave-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

lazyfree-lazy-eviction no

lazyfree-lazy-expire no

lazyfree-lazy-server-del no

slave-lazy-flush no

appendonly no

appendfilename "appendonly.aof"

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

aof-use-rdb-preamble no

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-size -2

list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes


redis-7001.conf, redis-7002.conf 和上面一样(除了端口)


redis-sentinel-26379.conf

port 26379

daemonize yes

dir "/opt/redis/data/sentinel"

logfile "26379.log"

sentinel myid ce9ef8b638afa838b47c86eca89b8d4ac954e2ab

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 192.168.0.4 7000 2

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 0

# Generated by CONFIG REWRITE

sentinel known-slave mymaster 106.12.20.136 7002

sentinel known-slave mymaster 192.168.0.4 7002

sentinel known-slave mymaster 106.12.20.136 7001

sentinel known-slave mymaster 192.168.0.4 7001

sentinel known-sentinel mymaster 192.168.0.4 26381 3fffe6041f0ea1486bcf560443f98d7d7f8f906e

sentinel known-sentinel mymaster 192.168.0.4 26380 ecb59d025f114d538100c7b172b58b33f03bbcb0

sentinel current-epoch 0


redis-sentinel-26380.conf redis-sentinel-26381.conf和上述一样(除了端口)

我把protected-model no注释掉了

写回答

1回答

闪光棒

提问者

2018-08-21

老师,解决了

0
0

一站式学习Redis 从入门到高可用分布式实践

Redis课程升级!系统梳理Redis知识体系,掌握redis必备!

2277 学习 · 261 问题

查看课程