隨着服務的複雜度上升,對服務的監控和管理的需求顯著增加,開發人員可以使用Jconsole、jvisualvm、jinfo、jstat等工具分析服務的運行狀況,但是對於運維人員以及其他非開發人員就不具有可行性;故需要搭建一套圖形化的監控平臺。
簡介
Actuator
actuator是spring boot提供的對應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。Actuator使用方法
Jolokia
Spring Boot Actuator對外暴露應用的監控信息,Jolokia提供使用HTTP接口獲取JSON格式 的數據。Jolokia使用方法
Telegraf
收集系統和服務的統計數據,並支持寫入到 InfluxDB 數據庫。官方地址
Influxdb
InfluxDB 是一個開源分佈式時序、事件和指標數據庫。它具備如下主要特性;官方地址
- Time Series (時間序列):你以使用與時間有關的相關函數(如最大,最小,求和等)
- Metrics(度量):你可以實時對大量數據進行計算
- Eevents(事件):它支持任意的事件數據
Grafana
Grafana 是一個開箱即用的可視化工具,具有功能齊全的度量儀表盤和圖形編輯器,有靈活豐富的圖形化選項,可以混合多種風格,支持多個數據源特點。官方地址
安裝
Telegraf
使用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
Influxdb
使用Centos,安裝方法如下,
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.3.5.x86_64.rpm
sudo yum localinstall influxdb-1.3.5.x86_64.rpm
Grafana
使用Centos安裝方法如下,其他系統安裝參考:
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
配置
Telegraf
-
打開配置文件
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
Influxdb
-
打開配置文件
vi /etc/influxdb/influxdb.conf
-
若不需要修改端口或其他,可使用默認配置
-
啓動服務
systemctl start influxdb
Grafana
-
使用默認配置,使用sqlite3數據庫保存配置信息,若需要更換數據庫,可打開/etc/grafana/grafana.ini配置
-
啓動
grafana-server
使用
- 新建Springboot應用,添加如下依賴:
<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;
}
}
- 打開Grafana
- http://localhost:3000
- 添加數據源
QQ截圖20170831154347
3.添加Dashboard,此處監控堆內存的使用情況和剛纔添加的自定義指標,
Influxdb的使用方法和相關函數含義可自定查詢;
堆內存數據查詢
配置堆內存最大堆大小及已使用堆大小監控
自定義指標數據查詢
配置接口/hello訪問次數監控.jpg
- 結果如下
其他
- Telegraf中Jolokia監控源配置方法,可先打開Jconsole工具,找到需監控的應用,打開Mbean選項:
- 找到springboot中的Endpoint的metricsEndpoint
上文中Telegraf配置
[[inputs.jolokia.metrics]]
name = "metrics"
mbean ="org.springframework.boot:name=metricsEndpoint,type=Endpoint"
attribute = "Data"
- name可自定義
- mbean 對應我們選擇的需監控對象的ObjectName的值org.springframework.boot:type=Endpoint,name=metricsEndpoint
- attribute 對應節點的屬性中的Data
- 其他需要監控的節點信息,可按該方法找到對應的信息,如內存信息監控信息
[[inputs.jolokia.metrics]]
name = "heap_memory_usage"
mbean = "java.lang:type=Memory"
attribute = "HeapMemoryUsage"
grafana + influxdb + telegraf , 構建linux性能監控平臺
https://blog.csdn.net/li_xue_zhao/article/details/79800140