隨着服務的複雜度上升,對服務的監控和管理的需求顯著增長,開發人員能夠使用Jconsole、jvisualvm、jinfo、jstat等工具分析服務的運行情況,可是對於運維人員以及其餘非開發人員就不具備可行性;故須要搭建一套圖形化的監控平臺。java
actuator是spring boot提供的對應用系統的自省和監控的集成功能,能夠對應用系統進行配置查看、相關功能統計等。Actuator使用方法linux
Spring Boot Actuator對外暴露應用的監控信息,Jolokia提供使用HTTP接口獲取JSON格式 的數據。Jolokia使用方法web
收集系統和服務的統計數據,並支持寫入到 InfluxDB 數據庫。官方地址redis
InfluxDB 是一個開源分佈式時序、事件和指標數據庫。它具有以下主要特性;官方地址spring
Grafana 是一個開箱即用的可視化工具,具備功能齊全的度量儀表盤和圖形編輯器,有靈活豐富的圖形化選項,能夠混合多種風格,支持多個數據源特色。官方地址sql
使用Centos,安裝方法以下,其餘系統安裝參考數據庫
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.3.5-1.x86_64.rpm sudo yum localinstall telegraf-1.3.5-1.x86_64.rpm
使用Centos,安裝方法以下,vim
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.3.5.x86_64.rpm sudo yum localinstall influxdb-1.3.5.x86_64.rpm
使用Centos安裝方法以下,其餘系統安裝參考:segmentfault
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.4.3-1.x86_64.rpm sudo yum localinstall grafana-4.4.3-1.x86_64.rpm
打開配置文件springboot
vim /etc/telegraf/telegraf.conf
配置輸入源
Jolokia作爲輸入源,在Vim中定位到該配置塊,在非編輯模式下輸入以下命令:
/inputs.telegraf
代碼以下:
[[inputs.jolokia]] #配置jolokia接口的路徑,可根據本身的實際狀況修改 context = "/jolokia/" //須要收集信息的服務地址,多個可增長[[inputs.jolokia.servers]]節點 [[inputs.jolokia.servers]] name = "as-server-01" host = "127.0.0.1" port = "9000" # # username = "myuser" # # password = "mypassword" #須要收集信息的節點,此處可參看後續的配置方法 #收集內存的使用狀況 #[[inputs.jolokia.metrics]] # name = "heap_memory_usage" #mbean = "java.lang:type=Memory" #attribute = "HeapMemoryUsage" #收集springboot中actuator的監控信息 [[inputs.jolokia.metrics]] name = "metrics" mbean ="org.springframework.boot:name=metricsEndpoint,type=Endpoint" attribute = "Data"
還支持其餘如MQTT、redis等服務,使用方法可參考官方文檔。
配置 輸出源
使用InfluxDb做爲輸出源,定位到該模塊
/outputs.influxdb
代碼以下:
[[outputs.influxdb]] ## The HTTP or UDP URL for your InfluxDB instance. Each item should be ## of the form: ## scheme "://" host [ ":" port] ## ## Multiple urls can be specified as part of the same cluster, ## this means that only ONE of the urls will be written to each interval. # urls = ["udp://localhost:8089"] # UDP endpoint example #influx的http地址,可根據實際狀況修改 urls = ["http://localhost:8086"] # required ## The target database for metrics (telegraf will create it if not exists). #數據庫名稱 database = "telegraf" # required
保存配置文件
啓動服務
systemctl start telegraf
打開配置文件
vi /etc/influxdb/influxdb.conf
若不須要修改端口或其餘,可以使用默認配置
啓動服務
systemctl start influxdb
使用默認配置,使用sqlite3數據庫保存配置信息,若須要更換數據庫,可打開/etc/grafana/grafana.ini配置
啓動
grafana-server
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
@RestController public class MyController { //簡單技術指標監控 @Autowired CounterService counterService; /** * 聲明日誌 */ private Logger LOGGER = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/hello") public SimpleResult ver() throws Exception{ counterService.increment("acess.max"); return null; } }
QQ截圖20170831154347
3.添加Dashboard,此處監控堆內存的使用狀況和剛纔添加的自定義指標,
Influxdb的使用方法和相關函數含義可自定查詢;
堆內存數據查詢
配置堆內存最大堆大小及已使用堆大小監控
自定義指標數據查詢
配置接口/hello訪問次數監控.jpg
[[inputs.jolokia.metrics]] name = "metrics" mbean ="org.springframework.boot:name=metricsEndpoint,type=Endpoint" attribute = "Data"
[[inputs.jolokia.metrics]] name = "heap_memory_usage" mbean = "java.lang:type=Memory" attribute = "HeapMemoryUsage"
https://blog.csdn.net/li_xue_zhao/article/details/79800140