@Bean public RestTemplateBuilder restTemplateBuilder() { RestTemplateBuilder builder = new RestTemplateBuilder(); return builder.messageConverters(fastJsonHttpMessageConverter); }
@Bean public RestTemplate restTemplate() { return restTemplateBuilder().build(); }
@Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(sentinelConfiguration()); } @Bean public RedisSentinelConfiguration sentinelConfiguration(){ RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(); //配置matser的名称 redisSentinelConfiguration.master(redisMaster); //配置redis的哨兵sentinel Set<RedisNode> redisNodeSet = new HashSet<>(); nodes.forEach(x->{ redisNodeSet.add(new RedisNode(x.split(":")[0],Integer.parseInt(x.split(":")[1]))); }); log.info("\nredisNodeSet -->{}",redisNodeSet); redisSentinelConfiguration.setSentinels(redisNodeSet); redisSentinelConfiguration.setPassword(RedisPassword.of(redisPassword)); return redisSentinelConfiguration; }
@Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); return template; } }
启动,连接成功会显示具体信息
1 2 3
redis.clients.jedis.JedisSentinelPool : Trying to find master from available Sentinels... redis.clients.jedis.JedisSentinelPool : Redis master running at 192.168.18.128:6303, starting Sentinel listeners... redis.clients.jedis.JedisSentinelPool : Created JedisPool to master at 192.168.18.128:6303
连接异常情况
1 2 3 4 5
2019-04-04 09:55:44.961 INFO 24056 --- [ main] redis.clients.jedis.JedisSentinelPool : Trying to find master from available Sentinels... 2019-04-04 09:55:44.973 WARN 24056 --- [ main] redis.clients.jedis.JedisSentinelPool : Cannot get master address from sentinel running @ 192.168.18.1281:26302. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: 192.168.18.1281. Trying next one. 2019-04-04 09:55:44.973 WARN 24056 --- [ main] redis.clients.jedis.JedisSentinelPool : Cannot get master address from sentinel running @ 192.168.18.1281:26301. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: 192.168.18.1281. Trying next one. 2019-04-04 09:55:44.973 WARN 24056 --- [ main] redis.clients.jedis.JedisSentinelPool : Cannot get master address from sentinel running @ 192.168.18.1281:26303. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: 192.168.18.1281. Trying next one.