Actuator監控:SpringBoot自帶的,對生成環境進行監控的系統
使用:既然是監控,那就不能監控一個空項目
這里我使用SpringBoot整合MyBatis的Demo:
https://www.cnblogs.com/xuyiqing/p/10837299.html
依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
然后直接啟動項目即可
訪問http://localhost:8080/actuator
{ "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "health-component-instance": { "href": "http://localhost:8080/actuator/health/{component}/{instance}", "templated": true }, "health-component": { "href": "http://localhost:8080/actuator/health/{component}", "templated": true }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "info": { "href": "http://localhost:8080/actuator/info", "templated": false } } }
顯示可監控列表
訪問http://localhost:8080/actuator/health
{"status":"UP"}
說明狀態正常
進一步地,使用Actuator獲取地JSON可以寫前端頁面進行顯示,或者結合定時任務
比如定時訪問http://localhost:8080/actuator/health,如果返回UP說明正常,然后進行其他操作
問題解決:訪問http://localhost:8080/actuator/health顯示404
原因:SpringBoot版本問題,SpringBoot1.x訪問http://localhost:8080/health
其實這里獲得的是較少的信息,更多的信息需要進行配置:
management.endpoints.web.exposure.include=*
現在訪問:http://localhost:8080/actuator
{ "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "auditevents": { "href": "http://localhost:8080/actuator/auditevents", "templated": false }, "beans": { "href": "http://localhost:8080/actuator/beans", "templated": false }, "caches-cache": { "href": "http://localhost:8080/actuator/caches/{cache}", "templated": true }, "caches": { "href": "http://localhost:8080/actuator/caches", "templated": false }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "health-component-instance": { "href": "http://localhost:8080/actuator/health/{component}/{instance}", "templated": true }, "health-component": { "href": "http://localhost:8080/actuator/health/{component}", "templated": true }, "conditions": { "href": "http://localhost:8080/actuator/conditions", "templated": false }, "configprops": { "href": "http://localhost:8080/actuator/configprops", "templated": false }, "env": { "href": "http://localhost:8080/actuator/env", "templated": false }, "env-toMatch": { "href": "http://localhost:8080/actuator/env/{toMatch}", "templated": true }, "info": { "href": "http://localhost:8080/actuator/info", "templated": false }, "loggers": { "href": "http://localhost:8080/actuator/loggers", "templated": false }, "loggers-name": { "href": "http://localhost:8080/actuator/loggers/{name}", "templated": true }, "heapdump": { "href": "http://localhost:8080/actuator/heapdump", "templated": false }, "threaddump": { "href": "http://localhost:8080/actuator/threaddump", "templated": false }, "metrics-requiredMetricName": { "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}", "templated": true }, "metrics": { "href": "http://localhost:8080/actuator/metrics", "templated": false }, "scheduledtasks": { "href": "http://localhost:8080/actuator/scheduledtasks", "templated": false }, "httptrace": { "href": "http://localhost:8080/actuator/httptrace", "templated": false }, "mappings": { "href": "http://localhost:8080/actuator/mappings", "templated": false } } }
訪問:http://localhost:8080/actuator/env
獲得配置信息
訪問:http://localhost:8080/actuator/beans
監控項目Spring中的beans
訪問:http://localhost:8080/actuator/metrics
查看應用基本指標列表
通常不建議全部開啟,因為不安全
所以,可以這樣配置
#開啟全部 management.endpoints.web.exposure.include=* #開啟某個 management.endpoints.web.exposure.include=metrics #關閉某個 management.endpoints.web.exposure.exclude=metrics
SpringBoot的學習暫時結束了:
總結和未來規划:
學會了基本的配置、整合Mybatis、Redis、ActiveMQ、ElasticSearch等
技術棧的規划:
初級路線:Java基礎->Java Web(html+css+js,servlet,request+response,ajax,mysql+jdbc...)->SSM、SSH->SpringBoot->Spring Cloud
高級路線:Linux->源碼閱讀(Spring、JDK)->數據庫優化(分庫分表,慢查詢,調優...)->緩存(Redis集群...)->HTTP/HTTPS協議優化
->安全(XSS、DDOS、CSRF)->消息隊列(ActiveMQ,Kafka,RocketMQ)->搜索框架(Lucene、Solr、ElasticSearch)->Docker、K8S
下面開始Spring Cloud的學習!
