未来每一个微服务在云上部署以后,我们都需要对其进行监控、追踪、审计、控制等。SpringBoot就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控、审计等功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
测试
http://localhost:8080/actuator/beans
http://localhost:8080/actuator/configprops
http://localhost:8080/actuator/metrics
http://localhost:8080/actuator/metrics/jvm.gc.pause
http://localhost:8080/actuator/endpointName/detailPath
。。。。。。
Health:监控状况
Metrics:运行时指标
Loggers:日志记录
健康检查端点,我们一般用于在云平台,平台会定时的检查应用的健康状况,我们就需要Health Endpoint可以为平台返回当前应用的一系列组件健康状况的集合。
开启health的显示详细endpoint.health.show-details=always
重要的几点:
提供详细的、层级的、空间指标信息,这些信息可以被pull(主动推送)或者push(被动获取)方式得到;
management:
endpoint:
beans:
enabled: true
management:
endpoints:
enabled-by-default: false
endpoint:
beans:
enabled: true
health:
enabled: true
支持的暴露方式
HTTP:默认只暴露health和info Endpoint
JMX:默认暴露所有Endpoint (例如Jconsole)
除过health和info,剩下的Endpoint都应该进行保护访问。如果引入SpringSecurity,则会默认配置安全访问规则
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
}
构建Health
Health build = Health.down()
.withDetail("msg", "error service")
.withDetail("code", "500")
.withException(new RuntimeException())
.build();
management:
health:
enabled: true
show-details: always #总是显示详细信息。可显示每个模块的状态信息
@Component
public class MyConHealthIndicator extends AbstractHealthIndicator {
//这里可以通过获取容器中某个我们要监测的组件,判断其健康情况,来决定是up还是down
/**
* 真实的检查方法
*
* @param builder
* @throws Exception
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
//mongodb。 获取连接进行测试
Map<String, Object> map = new HashMap<>();
// 检查完成
if (1 == 2) {
// builder.up(); //健康
builder.status(Status.UP);
map.put("count", 1);
map.put("ms", 100);
} else {
// builder.down();
builder.status(Status.OUT_OF_SERVICE);
map.put("err", "连接超时");
map.put("ms", 3000);
}
builder.withDetail("code", 100)
.withDetails(map);
}
}
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
show-details: always #总是显示详细信息。可显示每个模块的状态信息
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
show-details: always #总是显示详细信息。可显示每个模块的状态信息
info:
appName: boot-admin
version: 2.0.1
mavenProjectName: @project.artifactId@ #使用@@可以获取maven的pom文件值
mavenProjectVersion: @project.version@
@Component
public class ExampleInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("example",
Collections.singletonMap("key", "value"));
}
}
会输出以上方式返回的所有info信息,即配置文件和代码迭代的所有info信息
@RestController
public class TestController
{
Counter counter;
public TestController(MeterRegistry meterRegistry){
counter = meterRegistry.counter("myservice.method.running.counter");
}
@GetMapping("/test")
public String test()
{
//该请求每发一次,就会增加一次记录数,来记录当前请求被调用的次数
counter.increment();
return "hhhhh";
}
}
//也可以使用下面的方式
@Bean
MeterBinder queueSize(Queue queue) {
return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
}
@Component
@Endpoint(id = "container")
public class DockerEndpoint {
@ReadOperation
public Map getDockerInfo() {
//创建一个单实例的map对象
//端点的读操作
return Collections.singletonMap("info", "docker started...");
}
@WriteOperation
private void restartDocker() {
System.out.println("docker restarted....");
}
}
让当前项目数据被监控端的服务器收集,表明当前项目是被监控的客户端
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.1</version>
</dependency>
spring:
boot:
admin:
client:
url: http://localhost:8888 #我们这个客户端要把数据汇报给哪一个监控端的服务器
instance:
prefer-ip: true #使用ip注册进来
application:
name: 大忽悠 #当前应用的名字
表明当前项目作为检测端
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.1</version>
</dependency>
在主配置类上加上对应开启服务监控功能的注解
@EnableAdminServer //开启服务监控功能
@SpringBootApplication
public class HealthDemo1Application {
public static void main(String[] args) {
SpringApplication.run(HealthDemo1Application.class, args);
}
}
最好修改当前项目端口号,防止端口号冲突
server.port=8888
现在启动两个项目,访问http://localhost:8888/即可看到下面的界面: