Springboot监控模块(actuator)

1.添加依赖

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

2.敏感信息加密--引入security

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

3.资源配置

http://localhost:8080/health

1.加入用户信息

security:
  user:
    name: admin
    password: 123456

2.基础配置

management:
  # 一般启动独立端口(默认和应用端口一致),启用后源端口不可查
  server:
    port: 8080
  endpoints:
    web:
      # 默认前缀路径,可修改
      base-path: /actuator
      exposure:
        # 向外暴露的端点,可用通配符('*',需要单引号)
        include: health,info,metrics
        # 排除暴露的端点
        exclude: env,heapdump

 localhost:8080/actuator

3.配置 info 开头的属性

info:
  contact:
    email: 2769****13@qq.com
    phone: 123456789
  description: actuator-test-application

访问localhost:8080/info接口,泄露springboot项目信息。

4.健康指标 

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class RocketMQHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check();
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }

        return Health.up().build();
    }

    private int check() {
        // 自定义健康检查
        return 0;
    }
}

访问localhost:8080/env接口,泄露springboot环境变量信息

5.指标端点 metrics 

localhost:8080/metrics

4.隐藏端点 

management:
  server:
    port: 8080
    security:
      enabled: true
  endpoint:
    shutdown:
      enabled: true
    web:
      exposure:
        include: "*"
        exclude: info,health,test
      base-path: /actuator
      path-mapping:
        prometheus: metrics
        health: /health
        metrics: /metrics

5.配置类

ps:springboot的版本为2.的版本,否则EndpointRequest没有包

# 需要Security验证的actuator端点名,多个端点之间使用,隔开。
actuator:
  security:
    endpoints: info,prometheus,test
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${actuator.security.endpoints:#{null}}")
    private String endpoints;


    @Value("${spring.security.user.name}")
    public String userName;

    @Value("${spring.security.user.password}")
    public String password;

    @Value("${spring.security.user.roles}")
    public String role;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        方式1:开放的actuator端点全部都验证
//        http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
//                .anyRequest().authenticated().and().httpBasic();
//        方式2:仅验证名单中的actuator端点
        http.requestMatcher(EndpointRequest.to(transformEndpoints(endpoints))).authorizeRequests()
                .anyRequest().authenticated().and().httpBasic();
    }

    private String[] transformEndpoints(String endpoints) {
        // isEmpty判空方法
        if (ObjectUtils.isEmpty(endpoints)) {
            return new String[0];
        }
        return endpoints.split(",");
    }
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunnxin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值