Spring Boot 1.x 集成 Prometheus 监控

Prometheus介绍

使用领先的开源监控解决方案为您的指标和警报提供支持 在项目中,如果我们都使用 k8s 部署,那么就需要集成Prometheus
来监控和收集指标来提供告警功能,这对于企业内是有非常打的作用,Prometheus 天然支持,并且开源,正如官网所说
使用领先的开源监控解决方案为您的指标和警报提供支持。

在这里插入图片描述

官网地址: https://prometheus.io/
github项目地址: https://github.com/prometheus/prometheus
更多详细信息,请阅读官网。

集成

SpringBoot 1.x 集成我们可以使用 https://github.com/JonathanFaz/simpleclient_spring_boot 项目很快的进行集成。
pom.xml

<!-- spring boot prometheus-->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.15.0</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_hotspot</artifactId>
    <version>0.15.0</version>
</dependency>

application.properties

management.port=8093
management.security.enabled=false
endpoints.enabled=false
endpoints.prometheus.enabled=true

注意: management.port 端口是 指标端口,稍后我们会返回这个端口查看prometheus 指标是否配置成功。
启动类
在springboot 启动类上面加入以下配置:

@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class AppWebApplication {

    public static void main(String[] args) {
        // 开启JVM监控参数
        DefaultExports.initialize();
        SpringApplication.run(AppWebApplication.class, args);
    }

然后,启动项目访问:
http://localhost:8093/prometheus
8093是我们配置的端口,切记 如果项目使用security 做授权拦截的,记得给 prometheus 放开,避免访问需要权限。
在这里插入图片描述
如果出现以下图,说明配置成功了。

服务发现配置(k8s)

接下我们需要项目接入prometheus 服务,在k8s 配置文件中加入以下配置即可:
template:
metadata:
下面配置:

annotations:
  # 获知对应的endpoint是需要被scrape的
  prometheus.io/scrape: "true"
  # 获知进程暴露的metrics的具体路径
  prometheus.io/path: /prometheus
  # 获知进程暴露的metrics的端口
  prometheus.io/port: "8093"

prometheus.io/port 当前端口就是配置中的端口,切记。
到这里就已经接入了prometheus了,不过,有一点就是,运维已经配置好了 prometheus ,并且项目已经接入k8s环境了。

问题

接入过程中出现的问题:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'halMessageConverterSupportedMediaTypeCustomizer': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/hateoas/mvc/TypeConstrainedMappingJackson2HttpMessageConverter
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)

出现这个问题,需要加入以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
    <version>${spring-boot-version}</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值