Spring Boot执行器:为什么Redis群集的运行状况不正确?

时间:2016-08-04 02:19:17

标签: spring-boot redis spring-data-redis

我有一个使用Jedis进行redis群集配置的spring boot项目。配置文件如下:

application.yml文件:

enter image description here

RedisClusterConfig.java文件:

@Configuration

公共类RedisClusterConfig {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Value("${redis.cluster.host1}") private String HOST1;
@Value("${redis.cluster.port1}") private Integer PORT1;

@Value("${redis.cluster.host2}") private String HOST2;
@Value("${redis.cluster.port2}") private Integer PORT2;

@Value("${redis.cluster.host3}") private String HOST3;
@Value("${redis.cluster.port3}") private Integer PORT3;

@Value("${redis.cluster.host4}") private String HOST4;
@Value("${redis.cluster.port4}") private Integer PORT4;

@Value("${redis.cluster.host5}") private String HOST5;
@Value("${redis.cluster.port5}") private Integer PORT5;

@Value("${redis.cluster.host6}") private String HOST6;
@Value("${redis.cluster.port6}") private Integer PORT6;

@Bean
public JedisCluster jedisCluster() {
    Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
    //Jedis Cluster will attempt to discover cluster nodes automatically
    jedisClusterNodes.add(new HostAndPort(HOST1, PORT1));
    jedisClusterNodes.add(new HostAndPort(HOST2, PORT2));
    jedisClusterNodes.add(new HostAndPort(HOST3, PORT3));

    if(StringUtils.isNotBlank(HOST4)){
        jedisClusterNodes.add(new HostAndPort(HOST4, PORT4));
    }else{
        logger.warn("jedis cluster HOST4 not configured.");
    }

    if(StringUtils.isNotBlank(HOST5)){
        jedisClusterNodes.add(new HostAndPort(HOST5, PORT5));
    }else{
        logger.warn("jedis cluster HOST5 not configured.");
    }

    if(StringUtils.isNotBlank(HOST6)){
        jedisClusterNodes.add(new HostAndPort(HOST6, PORT6));
    }else{
        logger.warn("jedis cluster HOST6 not configured.");
    }
    JedisCluster jc = new JedisCluster(jedisClusterNodes, new JedisPoolConfig());
    return jc;
}

}

使用此配置,一切正常。我可以在redis集群中写入/读取数据。但是当我访问http://localhost:64001/health以获得执行器运行状况时,它会返回如下结果:

{&#34;状态&#34;:&#34; DOWN&#34;&#34;磁盘空间&#34; {&#34;状态&#34;:&#34; UP&#34 ;, &#34;总&#34;:120031539200,&#34;自由&#34;:33381117952,&#34;阈值&#34;:10485760}&#34; redis的&#34; {&#34;状态&# 34;:&#34; DOWN&#34;,&#34;错误&#34;:&#34; org.springframework.data.redis.RedisConnectionFailureException:无法获得Jedis连接;嵌套异常是redis.clients.jedis.exceptions.JedisConnectionException:无法从池中获取资源&#34;},&#34; mongo&#34;:{&#34; status&#34;:&#34; UP& #34;&#34;版本&#34;:&#34; 3.2.3&#34;}&#34;分贝&#34; {&#34;状态&#34;:&#34; UP&# 34;,&#34; dataSource1&#34; {&#34;状态&#34;:&#34; UP&#34;&#34;数据库&#34;:&#34; MySQL的&#34;,& #34;你好&#34;:1},&#34; dataSource2&#34; {&#34;状态&#34;:&#34; UP&#34;&#34;数据库&#34;:&# 34; MySQL的&#34;&#34;你好&#34;:1},&#34; dataSource3&#34; {&#34;状态&#34;:&#34; UP&#34;&#34 ;数据库&#34;:&#34; MySQL的&#34;&#34;你好&#34;:1}}}

我们可以看到有关Jedis的错误提示。

BTW,pom.xml中的相关依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

有什么我可能会错过的/健康状态回到&#34; UP&#34;?谢谢!

更多信息: 我跟踪了启动,发现RedisHealthIndicator通过了一个错误的工厂,其中host = localhost和port = 6379,它应该是已配置的集群主机和端口?

public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    this.redisConnectionFactory = connectionFactory;
}

1 个答案:

答案 0 :(得分:2)

最后,我通过重新配置redis来解决问题,如下所示:

spring: redis: cluster: nodes: - 192.168.0.17:6390 - 192.168.0.17:6391 - 192.168.0.17:6392 - 192.168.0.9:6390 - 192.168.0.9:6391 - 192.168.0.9:6392