javascript
不学无数——SpringBoot入门Ⅷ
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
SpringBoot--Actuator
1. 初識Actuator
在開發(fā)過程中,項目被放置到生產服務器上運行時,有可能需要我們全方位的監(jiān)控應用服務的運行情況。此時SpringBoot提供了Actuator模塊進行監(jiān)控和管理
2. 使用方法
在gradle中添加依賴
compile("org.springframework.boot:spring-boot-starter-actuator")然后啟動項目后會發(fā)現在啟動中發(fā)現如下的輸出信息
這些端點信息是暴露在外面的原生信息,例如此時訪問http://localhost:8080/health會發(fā)現在網站中輸出如下信息
在SpringBoot2.0中映射的地址是/actuator/health
{"status": "UP","diskSpace": {"status": "UP","total": 250685575168,"free": 172327690240,"threshold": 10485760},"db": {"status": "UP","database": "MySQL","hello": 1} }3. 端點介紹
Actuator的端點能夠進行監(jiān)控和管理應用。SpringBoot有許多的內嵌的端點,如果還需要其他的也可以自己添加。例如health端點提供了基本的應用健康信息。
每一個獨立的端點都可以進行選擇暴露或者不暴露,默認情況下有些端點是開啟的,如果不想暴露的話,那么可以在配置文件中進行配置endpoints + . + name,舉例如下:
endpoints.env.enabled=false下面給出幾個端點的簡單介紹
| autoconfig | 展示出所有自動配置的報告,展示自動配置的先決條件,并且分段展示出配置成功的和配置失敗的,并且展示出原因,其中positiveMatches 是自動化配置成功的,negativeMatches 是自動化配置不成功的 | true |
| beans | 該端點用來獲取應用上下文中創(chuàng)建的所有Bean | true |
| configprops | 展示出來所有@ConfigurationProperties的屬性信息 | true |
| dump | 暴露出程序運行中的線程信息 | true |
| env | 它用來獲取應用所有可用的環(huán)境屬性報告。包括:環(huán)境變量、JVM屬性、應用的配置配置、命令行中的參數 | true |
| health | 用來獲取應用的各類健康指標信息 | true |
| info | 該端點用來返回一些應用自定義的信息。默認情況下,該端點只會返回一個空的json內容。我們可以在application.properties配置文件中通過info前綴來設置一些屬性 | true |
| metrics | 該端點用來返回當前應用的各類重要度量指標,比如:內存信息、線程信息、垃圾回收信息等 | true |
| mappings | 展示出所有的@RequestMapping路徑 | true |
| trace | 該端點用來返回基本的HTTP跟蹤信息。默認情況下,跟蹤信息的存儲采用 | true |
3.1 自定義info端點信息
在添加了Actuator進行訪問info端點的時候,我們會發(fā)現頁面中顯示了一個空的json信息。如果想要顯示信息的話,那么可以在配置文件中通過設置info.*進行賦值,例如:
info.app.encoding=UTF-8 info.app.java.source=1.8 info.app.java.target=1.8這是訪問localhost:8080/info可以發(fā)現如下信息
{"app": {"java": {"target": "1.8","source": "1.8"},"encoding": "UTF-8"},"name": "BuXueWuShu" }4. 自定義端點
有時候自帶的端點信息不符合我們的需求,需要我們自定義一些端點信息。在自定義端點信息之前我們需要看一下Endpoint這個SpringBoot中的類。
public interface Endpoint<T> {//暴露在外的Id值,例如health、envString getId();//控制Id信息是否暴露boolean isEnabled();//用于權限的控制boolean isSensitive();//訪問Id值返回的信息T invoke(); }發(fā)現暴露出來的端點都是實現了Endpoint這個類,例如trace這個端點。
@ConfigurationProperties(prefix = "endpoints.trace") public class TraceEndpoint extends AbstractEndpoint<List<Trace>> {private final TraceRepository repository;/*** Create a new {@link TraceEndpoint} instance.* @param repository the trace repository*/public TraceEndpoint(TraceRepository repository) {super("trace");Assert.notNull(repository, "Repository must not be null");this.repository = repository;}@Overridepublic List<Trace> invoke() {return this.repository.findAll();}}然后發(fā)現在spring.factories文件中自動配置了
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,\點進去以后發(fā)現在其中已經自動將默認的端點注入進Spring容器中了
@Bean@ConditionalOnMissingBean -- 表示在容器中沒有此實體Bean時創(chuàng)建public TraceEndpoint traceEndpoint() {return new TraceEndpoint(this.traceRepository == null? new InMemoryTraceRepository() : this.traceRepository);}因此自定義端點也是類似的原理,我們做個簡單的如下:
public class MyEndPoint implements Endpoint {@Overridepublic String getId() {return "buxuewushu";}@Overridepublic boolean isEnabled() {return true;}@Overridepublic boolean isSensitive() {return true;}@Overridepublic Object invoke() {User user=new User();user.setName("不學無數");user.setAddress("HeNan");return user;} }將其放入spring.factories自動注入進容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.FirstSpringBoot.EndPoint.MyEndPoint然后啟動項目輸入localhost:8080/buxuewushu出現以下的信息
{"name": "不學無數","address": "HeNan" }5. 參考文章
- https://www.jianshu.com/p/9fab4e81d7bb
- http://blog.didispace.com/spring-boot-actuator-1/
- https://docs.spring.io/spring-boot/docs/1.5.15.RELEASE/reference/htmlsingle/#production-ready-endpoints
轉載于:https://my.oschina.net/u/4030990/blog/2907377
總結
以上是生活随笔為你收集整理的不学无数——SpringBoot入门Ⅷ的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云实时计算,前世功,今生能!
- 下一篇: 网站的domain不在首页的原因