一、指标监控
引入jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
以web方式开启:
#开启全部的
management.endpoints.enabled-by-default=true
#web 方式暴露
management.endpoints.web.exposure.include=*
?
二、常用的监控端点
看这个:传送门
?最常用的:
- health:健康状况,查看应用是否可用
- metrics:运行时指标,JVM、线程等相关内容(重要)
- loggers:日志记录
?三、定制EndPoint
?定制组件健康信息,比较简单,同时也可以实现接口方式:
package com.example.demo;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* @author Administrator
*/
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {
/**
* 真实的检查方法
* @param builder
* @throws Exception
*/
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Map<String, Object> map = new HashMap<>();
if(1==1){
builder.up();
map.put("count", 1);
map.put("msg", "健康");
}else{
builder.down();
map.put("msg", "超时");
}
builder.withDetail("code", 100)
.withDetails(map);
}
}
?
?INFO Endpoint 的定义:
1、配置文件直接定义:
info.mavenProjectName = @project.artifactId@
info.mavenProjectVersion=@project.version@
2、写代码:
package com.example.demo;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
@Component
public class AppInfo implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("msg", "真他吗帅!");
}
}
metrics定制endpoint,直接使用MeterRegistry。
自定义Endpoint,监控端点:
package com.example.demo;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Map;
@Component
@Endpoint(id = "myEndPoint")
public class EndPoint {
@ReadOperation
public Map<String, Object> read(){
return Collections.singletonMap("MG", "MG GOGO");
}
@WriteOperation
public void write(){
System.out.println("累");
}
}
访问自定义的指标的时候,访问的就是read方法
四、spring boot admin(可以使用)
准备一个 server,会定时去获取各个服务的相关内容。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
客户端注册:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
配置属性文件:
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
interface:#使用IP注册
prefer-ip: ture
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
|