轉自http://www.jianshu.com/p/fadcf4d92b0e
這里再配合Influxdb
和Grafana
可以構建一個非常漂亮的實時監控界面。

采集數據(Metrics)-> 存儲數據(InfluxDB) -> 顯示數據(Grafana)。
資源有限,我把這一套服務搭建在了一台CentOS 7
的服務器上。
安裝influxdb
InfluxDB
是Go語言寫的一個時序型數據庫,可以通過rpm的方式來安裝它,這里最好能安裝InfluxDB
的最新版本,否則跟Grafana
的交互會有一些不兼容。
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.2.4.x86_64.rpm
sudo yum localinstall influxdb-1.2.4.x86_64.rpm
安裝完成之后,啟動並查看這個服務是否在正常運行:
systemctl start influxdb.service
[root@metrics ~]# systemctl status influxdb.service ● influxdb.service - InfluxDB is an open-source, distributed, time series database Loaded: loaded (/usr/lib/systemd/system/influxdb.service; enabled; vendor preset: disabled) Active: active (running) since 一 2016-04-25 17:50:04 CST; 1 day 1h ago Docs: https://docs.influxdata.com/influxdb/ Main PID: 17871 (sh) CGroup: /system.slice/influxdb.service ├─17871 /bin/sh -c /usr/bin/influxd -config /etc/influxdb/influxdb.conf >>/dev/nul... └─17872 /usr/bin/influxd -config /etc/influxdb/influxdb.conf 4月 25 17:50:04 metrics systemd[1]: Started InfluxDB is an open-source, distributed...se. 4月 25 17:50:04 metrics systemd[1]: Starting InfluxDB is an open-source, distribute...... Hint: Some lines were ellipsized, use -l to show in full.
啟動后打開 web 管理界面 http://192.168.2.183:8083/ 默認用戶名和密碼是 root 和 root. InfluxDB 的 Web 管理界面端口是 8083,HTTP API 監聽端口是 8086,如果需要更改這些默認設定,修改 InfluxDB 的配置文件(/etc/influxdb/influxdb.conf)並重啟就可以了。
安裝 Grafana
Grafana
是一個非常好看的監控界面,它的安裝方式也非常簡單,通過yum
,一條命令就可以在server上完成安裝。
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.3.2-1.x86_64.rpm
sudo yum localinstall grafana-4.3.2-1.x86_64.rpm
完成安裝之后,啟動並檢查它的狀態:
[root@metrics ~]# systemctl start grafana-server.service [root@metrics ~]# systemctl status grafana-server.service ● grafana-server.service - Starts and stops a single grafana instance on this system Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; vendor preset: disabled) Active: active (running) since 一 2016-04-25 09:37:07 CST; 1 day 10h ago Docs: http://docs.grafana.org Main PID: 10309 (grafana-server) CGroup: /system.slice/grafana-server.service └─10309 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile= cfg:d...
用瀏覽器訪問 Grafana
,默認端口是3000
,默認的帳號密碼都是 admin(可以在配置文件中找到),登錄之后配置數據庫

Metrics Reporter
之前提到Metrics
只需要配置它的Reporter
就可以輸出到對應的地方,對於Influxdb
,在github
上找到了一個Influxdb
的Reporter
(https://github.com/davidB/metrics-influxdb)。
在Maven
中引入:
<dependency> <groupId>com.github.davidb</groupId> <artifactId>metrics-influxdb</artifactId> <version>0.8.2</version> </dependency>
並在代碼中配置Influxdb
的數據源:
@Bean(name = "influxdbReporter") public ScheduledReporter influxdbReporter(MetricRegistry metrics) throws Exception { return InfluxdbReporter.forRegistry(metrics) .protocol(InfluxdbProtocols.http("host_ip_address", port, "username", "password", "database")) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .filter(MetricFilter.ALL) .skipIdleMetrics(false) .build(); }
啟動項目之后,我們開始配置我們需要看到的圖表信息:

最后我們利用wrk
測試工具並發訪問連接:
➜ ~ wrk -t 10 -c 50 -d 5s http://127.0.0.1:8888/hello Running 5s test @ http://127.0.0.1:8888/hello 10 threads and 50 connections Thread Stats Avg Stdev Max +/- Stdev Latency 6.59ms 12.17ms 210.05ms 92.16% Req/Sec 1.40k 427.51 2.79k 71.80% 69902 requests in 5.04s, 10.28MB read Requests/sec: 13873.71 Transfer/sec: 2.04MB
於此同時Grafana
界面上系統實時的數據信息也展現出來了:
