Springboot服务接入prometheus 监控

发布于:2025-03-03 ⋅ 阅读:(145) ⋅ 点赞:(0)

1、pom.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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2、application.yml配置文件添加

#暴露监控端点(按需配置)
management:
  endpoints:
    web:
      exposure:
        include: prometheus,health,metrics,info
  endpoint:
    prometheus:
      enabled: true
  metrics:
    export:
      prometheus:
        enabled: true

3、增加java文件SecurityConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @Description: 接口放行
 * @Author: 
 * @Date: 
 */

@Configuration
@EnableWebSecurity
public class SecurityConfig {
   @Bean
   public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
      http
         .authorizeHttpRequests(authorize -> authorize
            .antMatchers("/actuator/prometheus").permitAll()
            .anyRequest().authenticated()
         )
         .csrf(csrf -> csrf.disable());
      return http.build();
   }
}

4、服务地址和端口配置到prometheus的配置文件里


网站公告

今日签到

点亮在社区的每一天
去签到