springboot管理及监控之Micrometer

返回首页

 

目录

 

Micrometer介绍:

springboot集成Micrometer

使用普罗米修斯

安装普罗米修斯

安装grafana

最后:

 

Micrometer介绍:

监控当然要可视化,仪表化。这样才能有最直观的感受。

事实上,已经有很多可视化仪表化的监控软件了,比如:AppOpticsAzure Monitor, Netflix AtlasCloudWatchDatadogDynatraceElasticGangliaGraphiteHumioInflux/TelegrafJMXKairosDBNew RelicPrometheusSignalFx, Google StackdriverStatsD,  Wavefront等。

这些软件都是获取协议数据,然后存储并转换成仪表输出。

但是软件多种多样,类型繁多,如果想要选择或者更替,就显得尤为麻烦。而Micrometer就是一个集成了上述这些监控软件的第三方,同时它还有一些自己的扩展。

你不必去适配各种监控软件,只需要使用和配置好Micrometer,间接地使用上述软件。

这么好的生产功能,spring也当然不放过,于是在Springboot2.x中正式将其纳入范围。

springboot集成Micrometer

springboot的生产就绪管理功能都在actuator包里,Micrometer和端点都是其中的功能,你若是想使用Micrometer也必须添加下面的依赖:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
			<version>2.1.4.RELEASE</version>
		</dependency>

接下来看你想使用什么监控,想使用什么监控就得加什么依赖,格式是这样的:io.micrometer/micrometer-registry-{system}

比如我们想使用Prometheus普罗米修斯

那么我们要添加普罗米修斯的依赖:

		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
			<version>1.1.4</version>
		</dependency>

每个监控软件的使用方法是不一样的,

比如说AppOptics,你需要在AppOptics网站注册账号token,在springboot里配置上你的token,springboot就会将监控信息发到AppOptics网站你的名下,你在AppOptics网站上就可以看到仪表化的数据。

再比如说Graphite,你需要另外启动一个Graphite服务器,然后再springboot里配置上Graphite服务器地址,springboot将监控信息发到Graphite服务器上,你可以使用Graphite服务器来展示仪表化的数据。

我们这里是以Prometheus为例:

使用普罗米修斯

添加普罗米修斯之后,启动springboot之后你直接访问http://localhost:8080/actuator/prometheus就行(如果你无法访问,请暴露端点),你会发现页面显示一大堆监控指标,如下:

# HELP tomcat_global_error_total  
# TYPE tomcat_global_error_total counter
tomcat_global_error_total{name="http-nio-8080",} 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 5480232.0
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads 18.0
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds 14.89
# 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 7.0064912E7
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge

...

...

...

这些数据可能每时每刻都在变化。如果你想造轮子,那么可以轮询请求该url,然后存储起来,并自己在页面展示,也是一个不错的监控系统。

但没有必要,有现成的东西让我们使用。

我们需要两个东西,一个是用来轮询请求并存储起来,一个是用来仪表展示。

请下载这俩东西:普罗米修斯服务器grafana

这俩有各个平台的版本,本例以window为例。

安装普罗米修斯

普罗米修斯是个压缩包,解压后大概长这样:

注意你需要配置里面的prometheus.yml。

在这个yml里要修改这段配置:

# my global config
global:
  scrape_interval:     15s 
  evaluation_interval: 15s 
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  - job_name: 'spring'
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['localhost:8080']

 

配置红色字体的意思是:

15秒请求localhost:8080/actuator/prometheus并且存储起来

然后启动prometheus.exe。

prometheus服务器启动的是9000端口。

安装grafana

grafana也是个解压版的,但需要先配置一番,打开conf.复制一份sample.ini并命名为custom.ini,然后启动bin目录下的grafana-server.exe即可。 

grafana启动的是3000端口。

grafana启动之后,我们打开localhost:3000,输入账户密码admin/admin就到了这个页面

然后选择添加数据源,选择普罗米修斯数据源。

接下来就用仪表盘解析普罗米修斯服务器存储的数据,如下图所示:查看了jvm_buffer_....的时间轴仪表.

基本使用教程完毕.

最后:

Micrometer都默认监控了哪些呢?如下:

  • JVM指标,报告利用率:

    • 各种内存和缓冲池
    • 与垃圾收集有关的统计
    • 线程利用率
    • 加载/卸载的类数
  • CPU指标
  • 文件描述符指标
  • Kafka消费者指标
  • Log4j2指标:记录每个级别记录到Log4j2的事件数
  • Logback指标:记录每个级别记录到Logback的事件数
  • 正常运行时间指标:报告正常运行时间表和表示应用程序绝对启动时间的固定计量表
  • Tomcat指标
  • Spring Integration指标

 

我们可以自定义指标吗?

当然可以,只是这里不写了.详情查看官方文档去吧.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值