如何在Spring Boot Actuator中添加依赖服务的健康状态信息
实现Spring Boot Actuator捕获依赖服务健康信息的方案
1. 引入Actuator依赖
首先在service-a的构建文件中添加Spring Boot Actuator依赖:
Maven(pom.xml)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Gradle(build.gradle)
implementation 'org.springframework.boot:spring-boot-starter-actuator'
2. 配置端点暴露与详情展示
在application.yml或application.properties中开启健康端点并配置详情显示:
application.yml
management: endpoints: web: exposure: include: health # 仅暴露health端点,也可使用*暴露所有端点 endpoint: health: show-details: always # 始终显示健康详情,生产环境可设为when-authorized
3. 自定义HealthIndicator检查依赖服务
为每个依赖服务(service-b、service-c)编写自定义HealthIndicator,通过调用其健康端点判断状态:
示例:ServiceBHealthIndicator
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class ServiceBHealthIndicator implements HealthIndicator { private final RestTemplate restTemplate; public ServiceBHealthIndicator(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @Override public Health health() { try { // 替换为service-b实际的健康端点地址 String healthUrl = "http://service-b/actuator/health"; restTemplate.getForObject(healthUrl, String.class); return Health.up().withDetail("service-b", "运行正常").build(); } catch (Exception e) { return Health.down() .withDetail("service-b", "服务不可达") .withDetail("错误信息", e.getMessage()) .build(); } } }
配置RestTemplate Bean
需要在service-a中注册RestTemplate的Bean:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
同理实现ServiceCHealthIndicator
复制上述代码,将service-b相关内容替换为service-c即可。
4. (可选)结合服务发现动态获取实例
如果依赖服务注册到Eureka、Nacos等服务发现组件,可通过DiscoveryClient动态获取实例地址,避免硬编码:
引入服务发现依赖(以Eureka为例)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
修改HealthIndicator
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.util.List; @Component public class ServiceBHealthIndicator implements HealthIndicator { private final RestTemplate restTemplate; private final DiscoveryClient discoveryClient; public ServiceBHealthIndicator(RestTemplate restTemplate, DiscoveryClient discoveryClient) { this.restTemplate = restTemplate; this.discoveryClient = discoveryClient; } @Override public Health health() { List<String> instanceUrls = discoveryClient.getInstances("service-b").stream() .map(instance -> instance.getUri() + "/actuator/health") .toList(); if (instanceUrls.isEmpty()) { return Health.down().withDetail("service-b", "无可用实例").build(); } try { // 检查第一个可用实例,也可扩展为检查多个实例 restTemplate.getForObject(instanceUrls.get(0), String.class); return Health.up() .withDetail("service-b", "运行正常") .withDetail("可用实例数", instanceUrls.size()) .build(); } catch (Exception e) { return Health.down() .withDetail("service-b", "实例不可用") .withDetail("错误信息", e.getMessage()) .build(); } } }
5. 验证效果
启动所有服务后,访问http://service-a/actuator/health,即可看到包含service-b、service-c健康状态的响应,示例如下:
{ "status": "UP", "components": { "service-b": { "status": "UP", "details": { "service-b": "运行正常" } }, "service-c": { "status": "UP", "details": { "service-c": "运行正常" } } } }
若某依赖服务宕机,对应组件状态会变为DOWN,默认情况下整体健康状态也会变为DOWN(可通过management.health.defaults.enabled=false自定义聚合规则)。
内容的提问来源于stack exchange,提问作者user352290
相关产品推荐
相关产品推荐

