浏览原文
1、简介
当某个应用程序在生产环境中运行时,监控其运行状况是必要的。通过实时理解应用程序的运行状况,你能在问题呈现之前失去正告,也能够在客户留神到问题之前解决问题。
在本文中,咱们将创立一个Spring Boot应用程序,在Spring Actuator,Micrometer,Prometheus和Grafana的帮忙下来监控零碎。其中,Spring Actuator和Micrometer是Spring Boot App的一部分。
简要阐明了不同组件的目标:
Spring Actuator
:在应用程序里提供泛滥 Web 接口,通过它们理解利用程序运行时的外部情况。无关更多信息,请参见Spring Boot 2.0中的Spring Boot Actuator。Micrometer
:为 Java 平台上的性能数据收集提供了一个通用的 API,它提供了多种度量指标类型(Timers、Guauges、Counters等),同时反对接入不同的监控零碎,例如 Influxdb、Graphite、Prometheus 等。Spring Boot Actuator对此提供了反对。Prometheus
:一个工夫序列数据库,用于收集指标。Grafana
:用于显示指标的仪表板。
上面,咱们将别离介绍每个组件。本文中应用的代码存档在GitHub上。
2、创立示例利用
首先要做的是创立一个能够监控的应用程序。通过Spring Initializr
,并增加Spring Boot Actuator,Prometheus和Spring Web依赖项, 咱们创立了一个如下所示的Spring MVC应用程序。
Spring Boot 根底就不介绍了,举荐下这个实战教程:
https://github.com/javastacks...
@RestControllerpublic class MetricsController {@GetMapping("/endPoint1")public String endPoint1() {return "Metrics for endPoint1";}@GetMapping("/endPoint2")public String endPoint2() {return "Metrics for endPoint2"; }}
启动应用程序:
$ mvn spring-boot:run
验证接口是否失常:
$ curl http://localhost:8080/endPoint1Metrics for endPoint1$ curl http://localhost:8080/endPoint2Metrics for endPoint2
验证Spring Actuator接口。为了使响应信息不便可读,咱们通过python -mjson.tool来格式化信息。
$ curl http://localhost:8080/actuator | python -mjson.tool...{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false },"health":{"href":"http://localhost:8080/actuator/health","templated":false },"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true },"info":{"href":"http://localhost:8080/actuator/info","templated":false } }}
默认状况下,会显示以上信息。除此之外,Spring Actuator能够提供更多信息,然而你须要启用它。为了启用Prometheus,你须要将以下信息增加到application.properties文件中。
management.endpoints.web.exposure.include=health,info,prometheus
重启应用程序,拜访http://localhost:8080/actuato...从Prometheus拉取数据,返回了大量可用的指标信息。咱们这里只显示输入的一小部分,因为它是一个很长的列表。
$ curl http://localhost:8080/actuator/prometheus# HELP jvm_gc_pause_seconds Time spent in GC pause# TYPE jvm_gc_pause_seconds summaryjvm_gc_pause_seconds_count{action="end of minor GC",cause="G1 Evacuation Pause",} 2.0jvm_gc_pause_seconds_sum{action="end of minor GC",cause="G1 Evacuation Pause",} 0.009...
如前所述,还须要Micrometer。Micrometer
为最风行的监控零碎提供了一个简略的仪表板,容许仪表化JVM利用,而无需关怀是哪个供应商提供的指标。它的作用和SLF4J
相似,只不过它关注的不是Logging(日志),而是application metrics(利用指标)。简而言之,它就是利用监控界的SLF4J。
Spring Boot Actuator为Micrometer提供了主动配置。Spring Boot2在spring-boot-actuator中引入了micrometer,对1.x的metrics进行了重构,另外反对对接的监控零碎也更加丰盛(Atlas
、Datadog
、Ganglia
、Graphite
、Influx
、JMX
、NewRelic
、Prometheus
、SignalFx
、StatsD
、Wavefront
)。
更新后的application.properties
文件如下所示:
management.endpoints.web.exposure.include=health,info,metrics,prometheus
重启应用程序,并从http://localhost:8080/actuator/metrics
中检索数据。
$ curl http://localhost:8080/actuator/metrics | python -mjson.tool...{"names": ["http.server.requests","jvm.buffer.count","jvm.buffer.memory.used",...
能够间接通过指标名来检索具体信息。例如,如果查问http.server.requests
指标,能够按以下形式检索:
$ curl http://localhost:8080/actuator/metrics/http.server.requests | python -mjson.tool...{"name": "http.server.requests","description": null,"baseUnit": "seconds","measurements": [ {"statistic": "COUNT","value": 3.0 }, {"statistic": "TOTAL_TIME","value": 0.08918682 },...
3、增加Prometheus
Prometheus是Cloud Native Computing Foundation的一个开源监控零碎。因为咱们的应用程序中有一个/actuator/Prometheus
端点来供 Prometheus 抓取数据,因而你当初能够配置Prometheus来监控你的Spring Boot应用程序。
Prometheus有几种装置办法,在本文中,咱们将在Docker容器中运行Prometheus。
你须要创立一个prometheus.yml
文件,以增加到Docker容器中。
global:scrape_interval:15sscrape_configs:- job_name: 'myspringmetricsplanet'metrics_path: '/actuator/prometheus'static_configs:- targets: ['HOST:8080']
scrape_interval
:Prometheus多久轮询一次应用程序的指标job_name
:轮询工作名称metrics_path
:指标的URL的门路targets
:主机名和端口号。应用时,替换HOST为主机的IP地址
如果在Linux上查找IP地址有艰难,则能够应用以下命令:
$ ip -f inet -o addr show docker0 | awk '{print $4}' | cut -d '/' -f 1
启动Docker容器并将本地prometheus.yml
文件,映射到Docker容器中的文件。
$ docker run \ -p 9090:9090 \ -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus
胜利启动Docker容器后,首先验证Prometheus是否可能通过 http://localhost:9090/targets
收集数据。
如上图所示,咱们遇到context deadline exceeded
谬误,造成Prometheus无法访问主机上运行的Spring Boot应用程序。如何解决呢?
能够通过将Docker容器增加到你的主机网络来解决此谬误,这将使Prometheus可能拜访Spring Boot应用程序。
$ docker run \ --name prometheus \ --network host \ -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \ -d \ prom/prometheus
再次验证,状态批示为UP。
当初能够显示Prometheus指标。通过拜访http://localhost:9090/graph
,在搜寻框中输出http_server_requests_seconds_max
并单击“执行”按钮,将为你提供申请期间的最长执行工夫。
4、增加Grafana
最初增加的组件是Grafana。只管Prometheus能够显示指标,但Grafana能够帮忙你在更精美的仪表板中显示指标。Grafana也反对几种装置形式,在本文中,咱们也将在Docker容器中运行它。
$ docker run --name grafana -d -p 3000:3000 grafana/grafana
点击 http://localhost:3000/
,就能够拜访Grafana。
默认的用户名/明码为admin/admin
。单击“登录”按钮后,你须要更改默认明码。
接下来要做的是增加一个数据源。单击左侧边栏中的“配置”图标,而后抉择“Data Sources(数据源)”。
单击Add data source
(增加数据源)按钮。
Prometheus在列表的顶部,抉择Prometheus。
填写可拜访Prometheus的URL,将HTTP Access
设置为Browser
,而后单击页面底部的Save&Test
按钮。
关注公众号,学习更多 Java 干货!
一切正常后,将显示绿色的告诉标语,批示数据源正在工作。
当初该创立仪表板了。你能够自定义一个,但也能够应用开源的仪表板。用于显示Spring Boot指标的一种罕用仪表板是JVM仪表板。
在左侧边栏中,点击+号,而后抉择导入。
输出JVM仪表板的URL https://grafana.com/grafana/d...,而后单击“Load(加载)”按钮。
为仪表板输出一个有意义的名称(例如MySpringMonitoringPlanet),抉择Prometheus作为数据源,而后单击Import按钮。
到目前为止,你就能够应用一个很酷的Grafana仪表板。
也能够将自定义面板增加到仪表板。在仪表板顶部,单击Add panel(增加面板)图标。
单击Add new panel(增加新面板)。
在Metrics 字段中,输出http\_server\_requests\_seconds\_max,在右侧栏中的Panel title字段中,能够输出面板的名称。
最初,单击右上角的Apply 按钮,你的面板将增加到仪表板。不要遗记保留仪表板。
为应用程序设置一些负载,并查看仪表板上的http\_server\_requests\_seconds\_max指标产生了什么。
$ watch -n 5 curl http://localhost:8080/endPoint1$ watch -n 10 curl http://localhost:8080/endPoint2
5、论断
在本文中,咱们学习了如何为Spring Boot应用程序增加一些根本监控。这非常容易,只须要通过将Spring Actuator,Micrometer,Prometheus和Grafana组合应用。
当然,这只是一个终点,然而从这里开始,你能够为Spring Boot应用程序扩大和配置更多、更具体的指标。