1.Maven中引入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>x.x.x</version>
</dependency>
2.在application.properties
或者application.yml
文件中进行配置。
management:
endpoints:
enabled-by-default: true # 暴露所有端点信息
web:
exposure:
include: "*" # 以web方式暴露
3.
常用端点使用
info:
app:
name: My Spring Boot Application
description: This is a sample spring boot application
version: 1.0.0
- 指标端点:访问
http://localhost:8080/actuator/metrics
,可以查看应用的各种指标信息,如内存使用情况、线程池状态、HTTP 请求统计等。还可以访问http://localhost:8080/actuator/metrics/{metricName}
查看具体指标的详细信息 - 环境端点:访问
http://localhost:8080/actuator/env
,可以查看应用的环境变量和属性信息
自定义端点
可以通过创建一个类并使用@Endpoint
注解来定义自定义端点。
java
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public Map<String, Object> custom() {
Map<String, Object> map = new HashMap<>();
map.put("custom", "This is a custom endpoint.");
return map;
}
}
端点安全保护
可以使用 Spring Security 来保护端点。以下是一个简单的配置示例: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.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.requestMatchers(AntPathRequestMatcher.antMatcher("/actuator/**")).hasRole("ADMIN")
.anyrequest().permitall()
.and()
.httpBasic();
}
}
标签:http,Spring,Boot,actuator,springframework,端点,org,Actuator,import
From: https://www.cnblogs.com/fanhaoyang/p/18679011