spring boot監控與管理

引言

在微服務架構中,咱們把單塊系統拆分紅多個服務。在部署時,一個服務極可能分佈在不一樣的主機,甚至是不一樣的機房。隨着服務的不斷增多,服務間相互調用、相互依賴。爲了掌握服務的狀態及服務所在主機的狀況,就顯得尤其重要!git

Spring Boot Actuator

spring boot actuator爲咱們暴露不少端點(endpoints),能夠用來獲取環境屬性、線程信息、健康情況等。下表列出了經常使用的端點。github

路徑 描述
/health 顯示應用程序的狀態
/metrics 顯示內存、GC、線程、堆棧等信息
/trace 顯示最近100條請求詳細信息
/mappings 顯示全部經過@RequestMapping配置的路徑
/env 顯示所有環境屬性,包括profiles、systemProperties、systemEnvironment、applicationConfig
/dump 顯示線程快照
/autoconfig 顯示自動配置的生效及未生效列表,對了解bean的加載頗有幫助
/beans 查看程序中全部的bean
/shutdown 關閉應用程序,默認是不可用的,能夠經過endpoints.shutdown.enabled=true開啓

如何開啓

引入spring-boot-startr-actuator後自動開啓。spring

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

雖然很簡單,若是你們不方便本身搭建,能夠經過git下載一下例子瀏覽器

git clone https://github.com/feelgood3000/actuator.git

下載後運行ActuatorApplication的main()方法便可。 安全

在瀏覽器打開以下地址:http://localhost:8080/metrics,返回內容以下session

{
"mem": 446758,
"mem.free": 316977,
"processors": 4,
"instance.uptime": 39453,
"uptime": 46245,
"systemload.average": 3.32568359375,
"heap.committed": 391680,
"heap.init": 131072,
"heap.used": 74702,
"heap": 1864192,
"nonheap.committed": 55872,
"nonheap.init": 2496,
"nonheap.used": 55079,
"nonheap": 0,
"threads.peak": 25,
"threads.daemon": 21,
"threads.totalStarted": 28,
"threads": 23,
"classes": 7793,
"classes.loaded": 7793,
"classes.unloaded": 0,
"gc.ps_scavenge.count": 10,
"gc.ps_scavenge.time": 170,
"gc.ps_marksweep.count": 2,
"gc.ps_marksweep.time": 130,
"httpsessions.max": -1,
"httpsessions.active": 0,
"datasource.primary.active": 0,
"datasource.primary.usage": 0
}

其餘能夠本身看一下,返回內容經過字段就能夠知道含義。架構

配置

端點開關

支持對每一個端點進行開關,配置爲endpoints.[endpointName].enabled=true,endpointName爲端點的名字,好比咱們想禁用info端點,在application.properties中添加以下內容便可:app

endpoints.info.enabled=false

若是咱們只想開啓其中一兩個端點,咱們能夠先把全部端點禁用,再單獨配置要開啓的端點。ide

endpoints.enabled=false
endpoints.info.enabled=true

端點端口號

有時爲了安全考慮,咱們不想跟服務佔用同一個端口,能夠單獨給actuator配置端口。spring-boot

server.port=8080
management.port=8888

自定義路徑

咱們也能夠修改默認的路徑,來起到安全的目的。
配置格式爲endpoints.[endpointsName].id=[path]
好比咱們想把metrics的訪問路徑從http://localhost:8080/metrics修改成http://localhost:8080/feelgood3000,則配置爲:

endpoints.metrics.id=feelgood3000

添加前綴

咱們能夠經過配置修改訪問endpoints的路徑。

management.context-path=/manage

添加如上配置後,全部端點的路徑從原來的http://localhost:8080/endpointName,改成http://localhost:8080/manage/endpointName了。

相關文章
相關標籤/搜索