【问题标题】:Actuator dependency doesn't display the health of some application执行器依赖项不显示某些应用程序的运行状况
【发布时间】:2018-01-30 19:31:15
【问题描述】:

我开发了一个应用程序 spring-boot,我使用 spring-boot-starter-actuator 来获取有关我的应用程序的信息。问题是请求 GET: /health 不显示 redis 的状态。这是我的 application.yml 的快照

spring:
  datasource:
    url: jdbc:mysql://********
    username: ****
    password: *******
    driver-class-name: com.mysql.jdbc.Driver
  ldap:
    urls: ldap://*****
    username: ****
    password: *******
  redis:
    url: redis://******

这是发出 GET 请求的结果:/health

{
    "status": "UP",
    "diskSpace": {
        "status": "UP"
    },
    "ldap": {
        "status": "UP",
        "version": "3"
    },
    "db": {
        "status": "UP",
        "database": "MySQL",
        "hello": 1
    }
}

【问题讨论】:

    标签: maven spring-boot gradle spring-boot-actuator


    【解决方案1】:

    似乎 Spring Actuator 无法识别 Redis 已启用。你能把这个配置添加到你的 yml 文件中吗

    management:
      health:
        redis:
          enabled: true
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢@Kenny Tai Huynh 我试过你的建议,但它不起作用
    • 还是同样的问题?!您可以仔细检查 yml 文件中的配置吗?!有空位吗?!或者尝试全部启用:management.health.defaults.enabled = true。根据这个“自动配置的健康指标”:docs.spring.io/spring-boot/docs/current/reference/html/…。我认为它应该有效。
    【解决方案2】:

    我遇到了完全相同的问题,我找到了解决方案。

    如果你看一下 spring-boot-actuator 的默认类 RedisHealthIndicator 你会注意到它使用了

    private final RedisConnectionFactory redisConnectionFactory;
    

    实际连接到 Redis 服务器并检查状态。不过这个类是从import org.springframework.data.redis.connection.RedisConnectionFactory;导入的

    只要您不在项目中使用spring-data-redis,开箱即用的运行状况指示器就不起作用。出于好奇,因为我真的不想导入 spring-data-redis 我将它作为依赖项添加到我的项目中,并且如果在 application.properties 中设置了运行状况指示器,则会出现:

     management.health.redis.enabled = true
    

    取出依赖,健康端点消失。

    总之,要在 spring boot 执行器健康中使用 Redis 健康指示器,您需要使用 spring-data-redis 并启用默认的 Redis 健康指示器,或者通过简单地扩展 AbstractHealthIndicator 并检查状态来创建自己的健康指示器你自己。我想我会尝试第二种选择——它似乎不太难。

    编辑 - 我做到了,它就像一个魅力。我刚刚创建了一个扩展 AbstractHealthIndicator 的类,在 doHealthCheck 方法中我只是这样做:

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        var pong = healthConnection.sync().ping();
        if (pong.equals("PONG")) {
            builder.up();
        }
    }
    

    我使用 io.lettuce:lettuce-core 进行 Redis 连接。连接在构造函数中设置并在@PreDestroy 方法上关闭。

    现在在健康端点我得到:{"status":"UP","details":{"redis":{"status":"UP"},"db":{"status":"UP","details":{"database":"MySQL","hello":1}}}}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2012-04-15
      • 2019-06-06
      • 2016-06-12
      相关资源
      最近更新 更多