上一篇利用Metrics
搭建了一个简易的JVM实时监控工具。git
这里再配合Influxdb
和Grafana
能够构建一个很是漂亮的实时监控界面。github
Grafana监控界面web
采集数据(Metrics)-> 存储数据(InfluxDB) -> 显示数据(Grafana)。数据库
资源有限,我把这一套服务搭建在了一台CentOS 7
的服务器上。浏览器
InfluxDB
是Go语言写的一个时序型数据库,能够经过rpm的方式来安装它,这里最好能安装InfluxDB
的最新版本,不然跟Grafana
的交互会有一些不兼容。服务器
wget http://dl.influxdata.com/influxdb/releases/influxdb-0.12.2-1.x86_64.rpm yum localinstall influxdb-0.12.2-1.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
是一个很是好看的监控界面,它的安装方式也很是简单,经过yum
,一条命令就能够在server上完成安装。测试
yum install https://grafanarel.s3.amazonaws.com/builds/grafana-3.0.0-beta51460725904.x86_64.rpm
完成安装以后,启动并检查它的状态:ui
[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
就能够输出到对应的地方,对于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
界面上系统实时的数据信息也展示出来了:
实时监控画面
做者:whthomas 连接:http://www.jianshu.com/p/fadcf4d92b0e 來源:简书 著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。