Spring Boot实现微服务的监控

发布时间:2020-06-12 14:09:57 作者:元一
来源:亿速云 阅读:1330

创建项目

本文的主要目的是实现微服务的监控,简单了解了上述工具的概念后,我们就来动手实践一下。首先创建一个简单的Spring Boot项目,其主要依赖如下:

<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>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

编辑项目配置:

server:
  port: 9562
spring:
  application:
    # 指定应用名
    name: prometheus-demo
management:
  endpoints:
    web:
      exposure:
        # 将 Actuator 的 /actuator/prometheus 端点暴露出来
        include: 'prometheus'
  metrics:
    tags:
      # 为指标设置一个Tag,这里设置为应用名,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选
      application: ${spring.application.name}

完成以上步骤后,进行一个简单的测试,看看端点是否能正常返回监控数据。启动项目,访问/actuator/prometheus端点。正常情况下会返回如下内容:

# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds{application="prometheus-demo",} 1.577697308142E9
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_memory_used_bytes{application="prometheus-demo",id="direct",} 16384.0
# HELP tomcat_sessions_expired_sessions_total  
# TYPE tomcat_sessions_expired_sessions_total counter
tomcat_sessions_expired_sessions_total{application="prometheus-demo",} 0.0
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.006
jvm_gc_pause_seconds_count{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.032
jvm_gc_pause_seconds_count{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 0.008
# HELP jvm_gc_pause_seconds_max Time spent in GC pause
# TYPE jvm_gc_pause_seconds_max gauge
jvm_gc_pause_seconds_max{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.006
jvm_gc_pause_seconds_max{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.032
jvm_gc_pause_seconds_max{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 0.008
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 0.0
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 1.3801776E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} 3.522832E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 6860800.0
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.9782928E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 4825568.0
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{application="prometheus-demo",level="info",} 7.0
logback_events_total{application="prometheus-demo",level="trace",} 0.0
logback_events_total{application="prometheus-demo",level="warn",} 0.0
logback_events_total{application="prometheus-demo",level="debug",} 0.0
logback_events_total{application="prometheus-demo",level="error",} 0.0
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="prometheus-demo",} 30.499
# HELP jvm_buffer_count_buffers An estimate of the number of buffers in the pool
# TYPE jvm_buffer_count_buffers gauge
jvm_buffer_count_buffers{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_count_buffers{application="prometheus-demo",id="direct",} 2.0
# HELP system_cpu_count The number of processors available to the Java virtual machine
# TYPE system_cpu_count gauge
system_cpu_count{application="prometheus-demo",} 6.0
# HELP jvm_threads_peak_threads The peak live thread count since the Java virtual machine started or peak was reset
# TYPE jvm_threads_peak_threads gauge
jvm_threads_peak_threads{application="prometheus-demo",} 22.0
# HELP tomcat_sessions_alive_max_seconds  
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds{application="prometheus-demo",} 0.0
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 1.5204352E7
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 1.31596288E8
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} 3.7879808E7
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 6881280.0
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.76685056E8
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 5373952.0
# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
# TYPE jvm_buffer_total_capacity_bytes gauge
jvm_buffer_total_capacity_bytes{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_total_capacity_bytes{application="prometheus-demo",id="direct",} 16384.0
# HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes{application="prometheus-demo",} 1.3801776E7
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 1.5204352E7
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 2.841116672E9
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} -1.0
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 2.5165824E8
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.390411776E9
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 1.073741824E9
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="prometheus-demo",} 18.0
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="prometheus-demo",state="runnable",} 8.0
jvm_threads_states_threads{application="prometheus-demo",state="new",} 0.0
jvm_threads_states_threads{application="prometheus-demo",state="timed-waiting",} 2.0
jvm_threads_states_threads{application="prometheus-demo",state="blocked",} 0.0
jvm_threads_states_threads{application="prometheus-demo",state="waiting",} 12.0
jvm_threads_states_threads{application="prometheus-demo",state="terminated",} 0.0
# HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC
# TYPE jvm_gc_memory_promoted_bytes_total counter
jvm_gc_memory_promoted_bytes_total{application="prometheus-demo",} 8296848.0
# HELP tomcat_sessions_active_max_sessions  
# TYPE tomcat_sessions_active_max_sessions gauge
tomcat_sessions_active_max_sessions{application="prometheus-demo",} 0.0
# HELP tomcat_sessions_created_sessions_total  
# TYPE tomcat_sessions_created_sessions_total counter
tomcat_sessions_created_sessions_total{application="prometheus-demo",} 0.0
# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total{application="prometheus-demo",} 1.36924824E8
# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage{application="prometheus-demo",} 0.10024585094452443
# HELP system_cpu_usage The "recent cpu usage" for the whole system
# TYPE system_cpu_usage gauge
system_cpu_usage{application="prometheus-demo",} 0.38661791030714154
# HELP tomcat_sessions_active_current_sessions  
# TYPE tomcat_sessions_active_current_sessions gauge
tomcat_sessions_active_current_sessions{application="prometheus-demo",} 0.0
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="prometheus-demo",} 7195.0
# HELP http_server_requests_seconds  
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0
http_server_requests_seconds_sum{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.012429856
# HELP http_server_requests_seconds_max  
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.012429856
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="prometheus-demo",} 2.841116672E9
# HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live_threads gauge
jvm_threads_live_threads{application="prometheus-demo",} 22.0
# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_classes_total counter
jvm_classes_unloaded_classes_total{application="prometheus-demo",} 1.0
# HELP tomcat_sessions_rejected_sessions_total  
# TYPE tomcat_sessions_rejected_sessions_total counter
tomcat_sessions_rejected_sessions_total{application="prometheus-demo",} 0.0

该端点返回的数据是Prometheus需要使用的。每一项都有相应的注释解释其含义,相信不难看懂。例如:

# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 0.0

表示:prometheus-demo 应用堆内存中的 PS Survivor Space 区域占用的空间是 0.0 个字节。

Actuator:

对Spring Boot监控能力有过了解的小伙伴都应该知道Spring Boot Actuator这个子项目,它为应用提供了强大的监控能力。从Spring Boot 2.x开始,Actuator将底层改为Micrometer,提供了更强、更灵活的监控能力。Micrometer是一个监控门面,可以类比成监控界的 Slf4j 。借助Micrometer,应用能够对接各种监控系统,例如本文所要介绍的:Prometheus

Prometheus :

Prometheus是一个由SoundCloud开发的开源系统监控+告警+时序列数据库(TSDB),Prometheus大部分组件使用Go语言编写,是Google BorgMon监控系统的开源版本。目前在CNCF基金会托管,并已成功孵化。在开源社区Prometheus目前也是相当活跃,在性能上Prometheus也足够支撑上万台规模的集群。

Prometheus的功能:

更多内容参考:官方文档,GitHub仓库

Grafana:

Grafana 是一款采用 GO 语言编写的开源应用,支持跨平台度量分析和可视化 + 告警。可以通过将采集的数据查询然后可视化地展示,并及时通知。Grafana  支持多种数据源和展示方式,总而言之是一款强大酷炫的监控指标可视化工具。

更多内容参考:官方文档,GitHub仓库


安装Prometheus服务

接下来就是需要在服务器上安装Prometheus服务,用于从微服务暴露的监控端点中采集监控数据。为了简单起见,我这里采用docker的安装方式,其他安装方式可以参考 官方安装文档。

首先为Prometheus准备一个配置文件:

[root@localhost ~]# mkdir /etc/prometheus
[root@localhost ~]# vim /etc/prometheus/prometheus.yml
scrape_configs:
# 任意写,建议英文,不要包含特殊字符
- job_name: 'spring'
  # 多久采集一次数据
  scrape_interval: 15s
  # 采集时的超时时间
  scrape_timeout: 10s
  # 采集的端点
  metrics_path: '/actuator/prometheus'
  # 被采集的服务地址,即微服务的ip及端口
  static_configs:
  - targets: ['192.168.1.252:9562']

该配置文件的目的是让Prometheus服务自动每隔15秒请求 http://192.168.1.252:9562/actuator/prometheus 。更多配置项参考:Prometheus Configuration官方文档

最后通过docker启动Prometheus服务,命令如下:

[root@localhost ~]# docker run -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

启动成功后,正常情况下访问http://{ip}:9090,就可以看到Prometheus的首页:
Spring Boot实现微服务的监控

点击 Insert metric at cursor ,即可选择监控指标;点击 Graph ,即可让指标以图表方式展示;点击Execute 按钮,即可看到类似下图的结果:
Spring Boot实现微服务的监控

功能说明:


Grafana可视化

上一小节我们已经成功搭建了Prometheus服务,并简单介绍了Prometheus自带的监控数据可视化界面,然而使用体验并不好,功能也比较少。下面我们来集成Grafana实现更友好、更贴近生产的监控数据可视化平台。

同样需要在服务器上安装Grafana服务,为了简单起见,我这里依旧采用docker的安装方式。其他安装方式可以参考 官方安装文档。

使用docker只需要一行命令就可以启动Grafana,如下:

[root@localhost ~]# docker run -d -p 3000:3000 grafana/grafana
配置监控数据源

Grafana启动成功后,访问http://{ip}:3000/login进行登录,默认账户密码均为admin
Spring Boot实现微服务的监控

登录成功后,首页如下:
Spring Boot实现微服务的监控

首先需要添加监控数据的来源,点击首页中的Add data source ,即可看到类似如下的界面:
Spring Boot实现微服务的监控

这里点击Prometheus,即可看到类似如下界面,在这里配置Prometheus服务相关的信息:
Spring Boot实现微服务的监控

保存成功后会有如下提示:
Spring Boot实现微服务的监控


创建监控Dashboard

点击导航栏上的 + 按钮,并点击Dashboard,将会看到类似如下的界面:
Spring Boot实现微服务的监控

点击 Add Query ,即可看到类似如下的界面:
Spring Boot实现微服务的监控

在红框标记的位置添加指标查询,指标的取值详见Spring Boot应用的 /actuator/prometheus 端点,例如jvm_memory_used_bytesjvm_threads_states_threadsjvm_threads_live_threads 等。

Grafana会给你较好的提示,并且支持较为复杂的计算,例如聚合、求和、平均等。如果想要绘制多个线条,可点击Add Query 按钮。如上图所示,笔者为图表绘制了两条线,分别代表daemon以及peak线程。

点击下图的按钮,并填入Title,即可设置图表标题:
Spring Boot实现微服务的监控

若需要为Dashboard添加新的图表则点击上图中的左上角按钮:
Spring Boot实现微服务的监控

并按下图步骤操作即可:
Spring Boot实现微服务的监控

如果需要保存该Dashboard,则点击右上角的保存按钮即可:
Spring Boot实现微服务的监控
Spring Boot实现微服务的监控


Dashboard市场

至此,我们已经成功将Grafana与Prometheus集成,实现了较为丰富的图表展示——将关心的监控指标放置到Dashboard上,并且非常灵活!然而,这个配置的操作虽然不难,但还是挺费时间的。

那么是否有配置好的又强大、又通用、拿来即用的Dashboard呢?答案是肯定的!前往 Grafana Lab - Dashboards ,输入关键词即可搜索指定Dashboard:
Spring Boot实现微服务的监控

如上图所示,可以找到若干款以 Prometheus 作为数据源,支持Micrometer的Dashboard。下面,简单演示一下如何使用 JVM(Micrometer) 这个Dashboard。点击 JVM(Micrometer) 进入Dashboard详情介绍页,如下图所示:
Spring Boot实现微服务的监控

如图已详细描述了该Dashboard的特性、配置。其中的management.metrics.tags.application ,前面安装Prometheus服务时已经配置过了。该页的右上角用红框标注的 4701 是一个非常重要的数字,因为这是该Dashboard的id。

回到Grafana的首页,我们来导入这个Dashboard,按下图步骤操作:
Spring Boot实现微服务的监控

输入后即可看到类似如下的界面,选择数据源,并点击Import:
Spring Boot实现微服务的监控

此时,即可看到类似如下的界面,我们常关心的指标该Dashboard均已支持:
Spring Boot实现微服务的监控

在上方的选项栏中可以选择查看不同的服务/应用:
Spring Boot实现微服务的监控

此外,还有一些比较好用的Dashboard。

推荐阅读:
  1. Spring Boot和Spring Cloud的联系
  2. Spring Cloud 微服务开发系列整理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot actuator 微服务监控

上一篇:objective-C: retain copy assgin nonatomic 等属性详解

下一篇:oracle 如何更改为归档模式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》