SpringCloud(一)_健康自检
2020-12-13 03:27
标签:snap enc connect pen ble hand vax jar serve 目的:监测Eureka客户端和服务器是否正常 需要依赖的jar: pom.xml 红色部分: springboot的actuator主要用于系统监控 application.yml文件 配置 healthcheck之后,就会调用实现HealthCheckHandler的类,比如MyHealthCheckHandler MyHealthCheckHandler模拟数据库是否可用的监测 MyHealthIndicator监测数据数据库状态,是否可用 HealthController修改数据库状态,输入http://localhost:8080/db/true即可开启数据库,而http://localhost:8080/db/false是关闭数据库 处理流程 1. 执行main方法,启动server 2. 自动读取application.yml文件,看到有配置healthcheck的状态为true,找实现HealthCheckHandler接口的类和重写的方法getStatus 3. MyHealthCheckHandler实现了HealthCheckHandler这个接口,通过MyHealthIndicator去查看数据库状态 4. MyHealthIndicator获取到数据库状态 5. HealthController专门为测试改变数据库状态而存在的 6.application.yml文件配置了instanceInfoReplicationIntervalSeconds: 10,所以每隔10s,就会访问实现HealthCheckHandler接口的类和重写的方法getStatus。 SpringCloud(一)_健康自检 标签:snap enc connect pen ble hand vax jar serve 原文地址:https://www.cnblogs.com/zhougongjin/p/11076287.htmlspring:
application:
name: health-handler-provider
eureka:
instance:
hostname: localhost
client:
healthcheck:
enabled: true
instanceInfoReplicationIntervalSeconds: 10
serviceUrl:
defaultZone: http://localhost:8761/eureka/
return InstanceStatus.UP就会被Eureka服务器端监测到该client是down的状态
package org.crazyit.cloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
/**
* 健康检查处理器
* @author 杨恩雄
*
*/
@Component
public class MyHealthCheckHandler implements HealthCheckHandler {
@Autowired
private MyHealthIndicator indicator;
public InstanceStatus getStatus(InstanceStatus currentStatus) {
Status s = indicator.health().getStatus();
if(s.equals(Status.UP)) {
System.out.println("数据库正常连接");
return InstanceStatus.UP;
} else {
System.out.println("数据库无法连接");
return InstanceStatus.DOWN;
}
}
}
package org.crazyit.cloud;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
/**
* 鍋ュ悍鎸囩ず鍣?
* @author 鏉ㄦ仼闆?
*
*/
@Component
public class MyHealthIndicator implements HealthIndicator {
public Health health() {
if(HealthController.canVisitDb) {
// 鎴愬姛杩炴帴鏁版嵁搴擄紝杩斿洖UP
return new Health.Builder(Status.UP).build();
} else {
// 杩炴帴鏁版嵁搴撳け璐ワ紝杩斿洖 out of service
return new Health.Builder(Status.DOWN).build();
}
}
}
package org.crazyit.cloud;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HealthController {
// 标识当前数据库是否可以访问
static Boolean canVisitDb = false;
@RequestMapping(value = "/db/{canVisitDb}", method = RequestMethod.GET)
@ResponseBody
public String setConnectState(@PathVariable("canVisitDb") Boolean canVisitDb) {
this.canVisitDb = canVisitDb;
return "当前数据库是否正常: " + this.canVisitDb;
}
}