一、使用spring-boot进行管理:
1、需要一个服务端:
服务端配置:
1)需要的依赖包:
<dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>2)启动类增加标签
@SpringBootApplication @EnableAdminServer public class SpringbtAdminServerApplication { public static void main(String[] args) { SpringApplication.run(SpringbtAdminServerApplication.class,args); } }3)配置文件application.properties:
server.port=19669 spring.application.name=Spring Boot Admin Web4)启动服务,访问地址可见:
2、客户端(被监控端配置)
1)web端需要添加对应的依赖,并修改对应的配置,使得对应的endpoint暴露出来。
需要依赖的包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.1.7.RELEASE</version> </dependency>2)application.properties添加对应的配置,需要配置springadmin 的地址并暴露需要的endpoints
#关于向springadmin监控注册的配置 start spring.boot.admin.client.url=http://172.xxx.xxx.96:19669 #关于向springadmin监控注册的配置 end #开放所有的web Endpoints management.endpoints.web.exposure.include= * management.endpoint.metrics.enabled=true management.endpoint.health.show-details=always #exposes management endpoints as JMX MBeans就可通过访问地址访问到已经暴露的endpoints
二、自定义endpoint
通过使用标签@Endpoint,并且注入该bean,默认自定义的endpoint是暴露的
@Configuration @Endpoint(id="endpoint-test") public class PlatformEndpoint { @ReadOperation public Map<String, Object> endpoint() { Map<String, Object> map = new HashMap<>(16); map.put("message", "this is my endpoint"); return map; } }自定义metric:
1)方法一:加标签
关于web应用,需要在方法名称上加上
@Timed(value = "testonexxx",description = "testxxx") @RequestMapping(path = "/test1") @ResponseBody public ResBaseDTO<String> test1(){ ResBaseDTO<String> res = new ResBaseDTO<>(); res.setSuccess(); return res; }2)方法二:实现MeterBinder,spring会自动注入该Metric
public class DemoMetrics implements MeterBinder { AtomicInteger count = new AtomicInteger(0); @Override public void bindTo(MeterRegistry meterRegistry) { Gauge.builder("demo.count", count, c -> c.incrementAndGet()) .tags("host", "localhost") .description("demo of custom meter binder") .register(meterRegistry); } }三、查看
访问:http://localhost:19667/actuator/metrics 可以查看系统注册了的所有metrics
{ "names": [ "jvm.memory.max", "tomcat.threads.busy", "jvm.threads.states", "jvm.gc.memory.promoted", "http.client.requests", "jvm.memory.used", "jvm.gc.max.data.size", "jvm.gc.pause", "jvm.memory.committed", "system.cpu.count", "logback.events", "okhttp.request", "testone", "jvm.buffer.memory.used", "tomcat.sessions.created", "jvm.threads.daemon", "system.cpu.usage", "jvm.gc.memory.allocated", "tomcat.global.sent", "dc-platform-metric", "tomcat.sessions.expired", "tomcat.global.request.max", "jvm.threads.live", "jvm.threads.peak", "tomcat.global.request", "process.uptime", "tomcat.sessions.rejected", "tomcat.global.received", "process.cpu.usage", "http.server.requests", "jvm.classes.loaded", "jvm.classes.unloaded", "tomcat.sessions.active.current", "tomcat.threads.config.max", "tomcat.sessions.alive.max", "jvm.gc.live.data.size", "tomcat.global.error", "jvm.buffer.count", "tomcat.threads.current", "jvm.buffer.total.capacity", "tomcat.sessions.active.max", "process.start.time" ] }访问对应的metric地址,查看具体metric信息:http://localhost:19667/actuator/metrics/dc-platform-metric
{ "name": "dc-platform-metric", "description": "Timer of http request", "baseUnit": "seconds", "measurements": [ { "statistic": "COUNT", "value": 2.0 }, { "statistic": "TOTAL_TIME", "value": 12.7515112 }, { "statistic": "MAX", "value": 12.747449 } ], "availableTags": [ { "tag": "method", "values": [ "POST" ] }, { "tag": "clientName", "values": [ "172.16.117.96" ] }, { "tag": "uri", "values": [ "/jingwei/dm/analyzeSql" ] }, { "tag": "status", "values": [ "200" ] } ] }