采用prometheus监控springboot项目

1. springboot项目中配置prometheus

对于springboot应用,需要以下几个步骤
springboot应用开启endpoint,添加actuator的依赖和promethus的依赖

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

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

在yml文件或者properties文件中,加入以下配置:

# 监控相关配置
# 开启监控
management:
  endpoints:
    web:
      exposure:
        include: "*"  

这里需要注意是,*号是需要加双引号的。
启动项目:访问 http://ip:port/actuator/prometheus
如果有数据展示说明配置成功。要是项目配置了context-path。需要加上context-path的路径。
项目里面不建议配置context-path

2. 下载prometheus

配置prometheus
首先要在官网 https://prometheus.io/
在下载页面,选择何时的版本下载,推荐下载tar.gz包。下载好后,进行解压。在合适的路径下即可。
这里介绍下prometheus的目录和文件:
1、prometheus采用的都是yml文件的配置方式。
2、在根目录下,有个prometheus.yml配置文件,文件初始化的内容如下:

global:
  scrape_interval:     15s   # 这个是每次数据手机的频率
  evaluation_interval: 15s   # 评估告警规则的频率。

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:               # 通过这里的配置控制prometheus监控的资源
  - job_name: prometheus      # prometheus自身默认的
    static_configs:
      - targets: ['localhost:9090']  # 默认暴露的是9090端口服务

global是全局配置。具体见上面的注释说明。

3. prometheus配置文件修改

添加我们的应用,对springboot进行监控

- job_name: 'spring-sample'
    metrics_path: 'actuator/prometheus'    # 这里我们springboot暴露出来的endpoint
    scrape_interval: 5s                    # 信息收集时间是间隔5秒
    static_configs:
    - targets: ['localhost:8778']          # 这里是springboot暴露出来的地址和端口

4. 启动prometheus

这些配置完成后,可以启动prometheus,
cd进到prometheus的根目录
linux中prometheus启动命令

./prometheus --config.file=prometheus.yml

windows中prometheus启动命令

添加:–web.enable-lifecycle
这种启动方法使Prometheus支持reload 配置文件**

prometheus.exe --web.enable-lifecycle

reload地址

http://localhost:9090/-/reload

5. 配置grafana

下载grafana,直接启动即可。

  1. 启动命令参见官网:./grafana-server web
  2. 配置datasource,选择prometheus。这个里面有个很重要的注意点,我看网上很多人在转如何用prometheus监控springboot应用,估计自己没去实际搭建,在interval这个时间上,默认是数字,比如15,代表是15秒。在添加dashboard的时候,会发现监控图标左上角是个红点,报错:Invalid interval string, expecting a number followed by one of “Mwdhmsy” ,这个错的解决方案就是在这些时间间隔后面加个"s"。问题解决。
  3. 选择dashboard,import的里输入一个模板,可以去dashboards去找你对应的模板,我们这里选用jvm的4701模板,然后就能看到你的springboot的监控信息了。到此,整个搭建完成。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要解决 Prometheus 监控 Spring Boot 项目无法访问 /actuator/prometheus 的权限问题,可以按照以下步骤进行操作: 1. 确认 Prometheus 已正确配置和运行。确保 Prometheus 的配置文件中已添加正确的目标 URL,即 Spring Boot 应用程序的 /actuator/prometheus 端点。 2. 在 Spring Boot 项目中,确保已添加合适的依赖。在项目的 pom.xml 文件中,添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> ``` 3. 确认 Spring Boot 项目的配置文件中已启用 Prometheus 监控。在 application.properties 文件中,添加以下配置: ``` # 启用 Prometheus endpoint management.endpoints.web.exposure.include=prometheus ``` 4. 如果项目中使用了安全框架(如 Spring Security),则需要为 Prometheus 端点添加访问权限。在项目的安全配置文件中,添加以下配置: ```java @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/actuator/prometheus").permitAll() // 允许所有用户访问 Prometheus 端点 .anyRequest().authenticated(); // 其他端点需要身份验证 } } ``` 执行以上步骤后,重新启动 Spring Boot 项目Prometheus,然后可以尝试访问 /actuator/prometheus 端点。现在,应该能够成功访问该端点,并获取到 Prometheus 所需的监控数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值