簡體   English   中英

Spring Boot 執行器 MySQL 數據庫健康檢查

[英]Spring boot actuator MySQL database health check

我設計了一個使用 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>

當我嘗試點擊 url " http://localhost:8080/health " 時,我得到 {"status":"UP"} 作為響應。 我想用 Spring Boot 執行器監控我的數據庫 (MySQL)。 我想查看我的數據庫狀態。

任何人都可以幫忙嗎?

我會檢查文檔- 匿名公開完整細節需要禁用執行器的安全性。

如果這不是您想要的,您可以完全控制安全性並使用 Spring Security 編寫自己的規則。

"management.endpoint.health.show-details=always" 添加您的 application.properties

我用我自己的方式檢查數據庫連接。 以下解決方案非常簡單,您可以根據需要進行修改。 我知道這不是執行器特有的,盡管當您不想使用執行器時,這是一種解決方法。

@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 類,具有 2 個屬性Boolean success and String message

在 spring 版本 2.2.x 中是:

management.endpoint.health.show-details=always

默認情況下,此道具的值從不。

如果您使用的是 spring 安全性,那么執行器默認啟用安全性。

將此添加到您的屬性文件中 -

management.security.enabled= false

在屬性文件中添加用戶名和密碼 -

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

並使用這些憑據訪問執行器端點。

當調用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"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM