对微服务的监控的两种办法(springbootAdmin、prometheus+grafana)

编程入门 行业动态 更新时间:2024-10-26 08:20:54

对微服务的监控的<a href=https://www.elefans.com/category/jswz/34/1768716.html style=两种办法(springbootAdmin、prometheus+grafana)"/>

对微服务的监控的两种办法(springbootAdmin、prometheus+grafana)

1、springboot-admin实现对服务的监控

一、actuator客户端(即每一个需要监控的微服务)
1、在业务微服务中引入jar包
 <!-- 引入Actuator监控依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--这个包是作图形化服务端监控客户端:如果不用图形化客户端就不要导这个包--><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.3.0</version></dependency>
2、配置yml
management:server:port: 8092endpoints:web:exposure:# 从技术上更改端点的暴露 -- 通过HTTP公开所有的端点,可通过 /actuator/{ID} 去查看,如 /actuator/beansinclude: "*"base-path: /actuatorjmx:exposure:include: "*"endpoint:health:show-details: alwayshttptrace:enabled: truemetrics:enabled: truemetrics:export:prometheus:enabled: true
spring:boot:admin:client:#连接监控服务端的地址,也就是actuator的服务端的运行ip 端口url: http://localhost:8091instance:prefer-ip: true
二、actuator服务端的配置
1、创建一个springboot工程,导入包
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.3.0</version></dependency>
2、配置yml(正常springboot的配置无需特殊配置)
# 应用名称
spring:application:name: actuatormonitor
server:port: 8091
3、主启动类上加注解

@EnableAdminServer

这样就配置完成了:
访问服务端的地址即可看到监控的状态的图形化页面
localhost:8091即可

2、prometheus+grafana实时监控

先解决时间同步问题:

1、虚拟机与宿主机时间的同步 同步办法
①点击虚拟机的上面菜单栏 VM选择Install VMware Tools,这就安装了虚拟机工具
②点击VM选择settings,再选择options页签,选择VMware Tools,勾选上右上方的Synchronize guest
time with host 这样在xshell中执行date,即可看到时间已同步
2、docker与虚拟机的时间同步
在执行生成容器的命令的语句加上 -v /etc/localtime:/etc/localtime:ro 即把时间挂载与宿主机一致 例如:
docker run -d --name=prometheus -p 9090:9090 -v /home/soft/monitor/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /etc/localtime:/etc/localtime:ro prom/prometheus
检查方法 进入已生成容器查看: ①docker exec -it 23f20c2e43e8 /bin/bash
②再输入date查看如果遇到 OCI runtime exec failed: exec failed:错误,那就用docker exec -it 23f20c2e43e8 /bin/sh命令
3、linux服务器(或虚拟机)的时间查看设定:
查看:date
按格式查看:date "+%Y-%m-%d %H:%M:%S"
设置时间:date -s 字符串时间(例如:date -s "2018-10-10 11:22:22")
查看日历:cal

注意:这里的-v /etc/localtime:/etc/localtime:ro 后面的:ro是read
only的意思,即docker容器只能读来自宿主机的数据,不可以增删改。

docker安装使用prometheus和grafana:

(这里的每个容器都同步了时间-v /etc/localtime:/etc/localtime:ro

1、安装prometheus

docker run -d --name=prometheus -p 9090:9090 \
-v /home/soft/monitor/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /etc/localtime:/etc/localtime:ro \
prom/prometheus
访问方法:192.168.200.137:9090可以看到prometheus 是否启动

2、安装Grafana

docker run -d -p 3000:3000 --name=grafana \
-v /home/soft/monitor/grafana/Grafana-storage:/var/lib/Grafana \
-v /etc/localtime:/etc/localtime:ro \
grafana/grafana
访问方法:192.168.200.137:3000可以看到grafana 是否启动

3、docker部署cAdvisor:(监控docker的容器的运行指标)

docker run -d \
–volume=/:/rootfs:ro \
–volume=/var/run:/var/run:ro \
–volume=/sys:/sys:ro \
–volume=/var/lib/docker/:/var/lib/docker:ro \
–volume=/dev/disk/:/dev/disk:ro \
-v /etc/localtime:/etc/localtime:ro \
–publish=8080:8080 \
–detach=true \
–name=cadvisor \
google/cadvisor:latest
访问方法:192.168.200.137:8080 可以看到cAdvisor是否启动
访问:http://192.168.200.137:8080/metrics有数据就说明cAdvisor是启动好了的

在prometheus.yml中加入:

- job_name: 'docker'static_configs:- targets: ['192.168.200.137:8080']labels:instance: docker

在grafana中导入datasource,再引入import的模板,

监控docker的模板一般193就好用,其他模板随意试探,只要好用就行,模板地址:

就可以看到曲线图了(cAdvisor就是监控docker的)

4、监控linux的cpu、内存等网络信息,安装node-explore(据说最好安装在linux上)

docker run -d -p 9100:9100 --name=node-exporter \
-v “/proc:/host/proc:ro” \
-v “/sys:/host/sys:ro” \
-v “/:/rootfs:ro” \
-v /etc/localtime:/etc/localtime:ro \
–net=“host” \
prom/node-exporter
访问 http://192.168.200.137:9100就能看到效果
访问:http://192.168.200.137:9100/metrics有数据就说明node-explore是启动好了的

在prometheus.yml中加入:

- job_name: 'node-explore'static_configs:- targets: ['192.168.200.137:9100']labels:instance: node-explore

在访问prometheus就能看到效果:

选择模板9276就能看到监控的主机信息,其他模板随意试探,只要好用就行

5、监控springboot

5.1、在springboot中引入

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--prometheus监控  ;<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency> 

这里没写版本号是因为:(pringboot自动找对应版本)

  <parent><version>2.2.5.RELEASE</version></parent>

5.2、 配置yml文件,暴露端口

management:endpoints:web:exposure:include: "*"base-path: /actuatorjmx:exposure:include: "*"endpoint:health:show-details: alwayshttptrace:enabled: truemetrics:enabled: truemetrics:export:prometheus:enabled: truetags:#为指标设置一个名为 ${spring.application.name}的Tag,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选application: ${spring.application.name}

5.3、配置
在主启动类上加

@Value("${spring.application.name}")
private String application;
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(){return (registry)->registry.config()monTags("application",application);
}

5.4、 制作镜像
把springboot打成jar包,上传到 /usr/local/docker/monitor文件夹下,只要满足 /usr/local/docker/即可,monitor是自己建的。
写一个dockerfile,名字就叫 Dockerfile
vim Dockerfile

# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER lc <513778675@qq>
# VOLUME 指定了临时文件目录为/tmp。 # 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为monitor.jar
ADD monitor.jar monitor.jar
# 运行jar包
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/monitor.jar"]

生成docker镜像:docker build -t monitor .(最后还有一个点,你别忘了)
这个时候docker images就能看到该镜像(我当前这个springboot是运行在8091端口的)
生成容器:docker run -d --name=monitor -p 8091:8091 monitor
这样就把springboot的jar包生成容器在docker上运行起来了
访问:http://192.168.200.137:8091/metrics有数据就说明node-explore是启动好了的

5.5、 配置prometheus

  - job_name: 'monitor'metrics_path: '/actuator/prometheus'static_configs:- targets: ['192.168.200.137:8091']  labels:instance: monitor

注意这里多了一个metrics_path因为prometheus默认是前缀http://,后缀是/metrics,但是springboot通过actuator暴露出来端点是默认有/actuator的 所以这里要重新配置采集数据的路径 。

在grafana上引入模板,监控springboot我用的是 12900模板,其他模板随意试探,只要好用就行

就能看到图形效果

如果看不到 选一下 Application的下拉选和时间范围

6.实例监控与遇到的问题
以我本地启动项目 macro-auth 为例:

①注意引入的版本号:springboot的版本号是2.2.5.RELEASE 而引入的prometheus版本号是1.3.5 (版本不正确会报错)actuator的版本号是2.5.0
②我的 macro-auth 若是直接访问(以登录接口为例)的地址是:localhost:20001/auth/sys-user/login通过网关访问的地址是:localhost:20000/api/auth/sys-user/login
③配置步骤:
3.1 springboot引入依赖包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--prometheus监控  ;
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
3.2 配置yml文件
management:endpoints:web:base-path: /actuatorexposure:include: "*"jmx:exposure:include: "*"endpoint:health:show-details: alwayshttptrace:enabled: truemetrics:enabled: true
3.3 配置prometheus的yml(prometheus.yml)

只列出关键相关配置:(targets只写ip+端口,如有其他路径配置写在metrics_path里)

scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: "prometheus"metrics_path: '/api/auth/actuator/prometheus'# scheme defaults to 'http'.static_configs:- targets: ["localhost:20000"]
3.4 访问

直接访问 http://localhost:20000/api/auth/actuator/prometheus有数据返回则是通了
启动prometheus:访问http://localhost:9090/targets就看到服务监控到了
在grafana中监控prometheus并且下载模板号12900 即可完成对springboot的监控

7.监控受springsecurity安全保护的资源服务器

7.1 以我本地 macro-order为例

导包和yml配置与第6步相同,不再赘述

7.2 网关配置白名单

访问prometheus的路径放入白名单,不做拦截。GateWayFilterConfig类中

网关配置文件 yml中

7.3 在资源服务order中配置此路径不拦截。ResourceConfigServer类中


这里macro-order中的过滤器可能会报错,因为获取不到json-token。在TokenAuthenticationFilter类中

这样在访问 http://localhost:20000/api/order/actuator/prometheus就可以拿到数据了

更多推荐

对微服务的监控的两种办法(springbootAdmin、prometheus+grafana)

本文发布于:2024-03-04 21:26:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1710461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   办法   springbootAdmin   prometheus   grafana

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!