SpringBoot服務(wù)監(jiān)控機制,總算整明白了!
這里是碼農(nóng)充電第一站,回復(fù)“666”,獲取一份專屬大禮包 真愛,請設(shè)置“星標(biāo)”或點個“在看”

文章來源:http://u6.gg/kqmhz
SpringBoot 監(jiān)控
| HTTP Endpoints 監(jiān)控
<dependencies>
????<dependency>
????????<groupId>org.springframework.bootgroupId>
????????<artifactId>spring-boot-starter-actuatorartifactId>
????dependency>
dependencies>

內(nèi)置端點

management:
??endpoints:
????web:
??????exposure:
????????include:?[health,info,mappings]?//或者直接配置?"*"
management.endpoint.<id>.enabled=true
health 端點
management:
??endpoint:
????health:
??????show-details:?always

loggers 端點



metrics 端點


自定義監(jiān)控端點
@Endpoint:定義一個監(jiān)控端點,同時支持 HTTP 和 JMX 兩種方式。
@WebEndpoint:定義一個監(jiān)控端點,只支持 HTTP 方式。
@JmxEndpoint:定義一個監(jiān)控端點,只支持 JMX 方式。
@ReadOperation:作用在方法上,可用來返回端點展示的信息(通過 Get 方法請求)。
@WriteOperation:作用在方法上,可用來修改端點展示的信息(通過 Post 方法請求)。
@DeleteOperation:作用在方法上,可用來刪除對應(yīng)端點信息(通過 Delete 方法請求)。
@Selector:作用在參數(shù)上,用來定位一個端點的具體指標(biāo)路由。
來,一起寫一個自己的監(jiān)控端點,定義一個類,并使用?@Endpoint 注解標(biāo)注標(biāo)識,同時定義幾個方法用?@ReadOperation 和?@WriteOperation 注解來標(biāo)注:
@Endpoint(id="myEndpoint")
@Component
public?class?MyEndpoint?{
????private?String?STATUS?=?"up";
????private?String?DETAIL?=?"一切正常";
//????@ReadOperation
//????public?String?test1(){
//????????return?"wolf";
//????}
//????@ReadOperation
//????public?Map?test2(){
//????????Map?map?=?new?HashMap();
//????????map.put("status","up");
//????????return?map;
//????}
????@ReadOperation
????public?JSONObject?test3(){
????????JSONObject?jsonObject=?new?JSONObject();
????????jsonObject.put("status",STATUS);
????????jsonObject.put("detail",DETAIL);
????????return?jsonObject;
????}
????@ReadOperation
????public?JSONObject?test3_1(@Selector?String?name){
????????JSONObject?jsonObject=?new?JSONObject();
????????if?("status".equals(name)){
????????????jsonObject.put("status",STATUS);
????????}else?if?("detail".equals(name)){
????????????jsonObject.put("detail",DETAIL);
????????}
????????return?jsonObject;
????}
????@WriteOperation//動態(tài)修改指標(biāo)
????public?void?test4(@Selector?String?name,@Nullable?String?value){
????????if?(!StringUtils.isEmpty(value)){
????????????if?("status".equals(name)){
????????????????STATUS?=?value;
????????????}else?if?("detail".equals(name)){
????????????????DETAIL?=?value;
????????????}
????????}
????}
}



| JMX 監(jiān)控


如何手動注冊一個 JMX MBean?
定義一個接口 SystemInfoMBean(注意名字必須要用 MBean 結(jié)尾):
public?interface?SystemInfoMBean?{
????int?getCpuCore();
????long?getTotalMemory();
????void?shutdown();
}
再定義一個類實現(xiàn) SystemInfoMBean 接口,實現(xiàn)類的明明方式為接口名去掉 MBean:
public?class?SystemInfo?implements?SystemInfoMBean?{
????@Override
????public?int?getCpuCore()?{
????????return?Runtime.getRuntime().availableProcessors();
????}
????@Override
????public?long?getTotalMemory()?{
????????return?Runtime.getRuntime().totalMemory();
????}
????@Override
????public?void?shutdown()?{
????????System.exit(0);
????}
}
最后就是需要將該實現(xiàn)類進行注冊:
public?class?JmxRegisterMain?{
????public?static?void?main(String[]?args)?throws?NotCompliantMBeanException,?InstanceAlreadyExistsException,?MBeanRegistrationException,?MalformedObjectNameException,?IOException?{
????????MBeanServer?mBeanServer=?ManagementFactory.getPlatformMBeanServer();
????????ObjectName?objectName=new?ObjectName("com.lonely.wolf.note.springboot.actuator.jmx:type=SystemInfo");
????????SystemInfo?SystemInfo?=new?SystemInfo();
????????mBeanServer.registerMBean(SystemInfo,objectName);//注冊
????????System.in.read();//防止程序結(jié)束
????}
}

<dependency>
????<groupId>io.micrometergroupId>
????<artifactId>micrometer-registry-prometheusartifactId>
dependency>
總結(jié)
碼農(nóng)突圍資料鏈接
1、臥槽!字節(jié)跳動《算法中文手冊》火了,完整版 PDF 開放下載!
2、計算機基礎(chǔ)知識總結(jié)與操作系統(tǒng) PDF 下載
3、艾瑪,終于來了!《LeetCode Java版題解》.PDF
4、Github 10K+,《LeetCode刷題C/C++版答案》出爐.PDF歡迎添加魚哥個人微信:smartfish2020,進粉絲群或圍觀朋友圈
評論
圖片
表情
