一、使用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 Web
4)啓動服務,訪問地址可見:
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"
]
}
]
}