【问题标题】:Spring boot actuator MySQL database health checkSpring Boot 执行器 MySQL 数据库健康检查
【发布时间】:2017-01-30 13:51:58
【问题描述】:

我设计了一个使用 MySQL 数据库的演示 spring boot 应用程序 CRUD 操作。我的 application.properties 文件如下。

spring.boot.admin.url=http://localhost:8081
spring.datasource.url= jdbc:mysql://localhost:3306/springbootdb
spring.datasource.username=root
spring.datasource.password=admin
endpoints.health.sensitive=false
management.health.db.enabled=true
management.health.defaults.enabled=true
management.health.diskspace.enabled=true
spring.jpa.hibernate.ddl-auto=create-drop

弹簧执行器的POM.xml如下。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>1.3.4</version>
</dependency>

当我尝试点击网址“http://localhost:8080/health”时,我收到 {"status":"UP"} 作为响应。我想用弹簧启动执行器监控我的数据库(MySQL)。我想查看我的数据库状态。

有人可以帮忙吗?

【问题讨论】:

  • 你需要数据库的状态吗?你能解释一下吗
  • 是的,我需要数据库状态,但我不太清楚使用 Spring Boot 执行器可以获取哪些数据库的最大细节,因为我是 Spring Boot 新手。

标签: java mysql spring spring-boot spring-boot-actuator


【解决方案1】:

我会检查the documentation - 匿名公开全部详细信息需要禁用执行器的安全性。

如果这不是您想要的,您可以完全控制安全性并使用 Spring Security 编写自己的规则。

【讨论】:

    【解决方案2】:

    将此配置添加到您的 application.properties 文件中

    management.endpoint.health.show-details=always 
    

    【讨论】:

      【解决方案3】:

      我使用自己的方式检查数据库连接。以下解决方案非常简单,您可以根据需要进行修改。我知道这不是特定于执行器的,尽管当您不想使用执行器时,它是一种相同的解决方法。

      @RestController
      @RequestMapping("/api/db/")
      public class DbHealthCheckController {
      @Autowired
      JdbcTemplate template;
      
      private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
      
      @GetMapping("/health")
      public ResponseEntity<?> dbHealthCheck() {
          LOGGER.info("checking db health");
          try {
              int errorCode = check(); // perform some specific health check
              if (errorCode != 1)
                  return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse(false, "down"));
              return ResponseEntity.ok(new ApiResponse(true, "Up"));
          } catch (Exception ex) {
              ex.printStackTrace();
              LOGGER.error("Error Occured" + ex.getMessage());
              return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ApiResponse(false, "Down"));
          }
      }
      
         public int check() {
           List<Object> results = template.query("select 1 from dual", new 
                 SingleColumnRowMapper<>());
                return results.size();
           }
      }
      

      ApiResponse 是一个简单的 POJO 类,具有两个属性 Boolean success and String message

      【讨论】:

        【解决方案4】:

        在春季版本 2.2.x 中是:

        management.endpoint.health.show-details=always
        

        默认情况下,这个道具没有价值。

        【讨论】:

          【解决方案5】:

          如果您使用的是 spring security,则默认情况下为 actuator 启用安全性。

          将此添加到您的属性文件中 -

          management.security.enabled= false
          

          在属性文件中添加用户名和密码 -

          security.user.name= username
          security.user.password = password
          

          并使用这些凭据访问执行器端点。

          【讨论】:

            【解决方案6】:

            调用http://localhost:8080/actuator端点时,必须有如下端点。

            "health-component": {
              "href": "http://localhost:8080/actuator/health/{component}",
              "templated": true
            }
            

            在我的情况下,我在我的应用程序中使用 mongodb。我调用 http://localhost:8080/actuator/health/mongo 端点它返回 mongodb 健康。

            {
              "status": "UP",
              "details": {
                 "version": "2.6.0"
              }
            }
            

            【讨论】:

            • 组件名称由什么决定?例如,如果我想检查 JPA 或数据源连接?
            • 这个document显示了响应结构,构成健康的组件。,但是我没有找到组件名称列表
            猜你喜欢
            • 1970-01-01
            • 2015-07-04
            • 1970-01-01
            • 2015-08-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-03
            • 1970-01-01
            相关资源
            最近更新 更多