Spring Boot整合 Actuator 详细了解

一、创建项目引入依赖

 1、引入与自己项目Springboot对应的版本即可       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>

2、Springboot security 引入 需要配置用户名和密码 参照下面
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>

二、案例 之简单操作 启动按下面网址即可展示信息

一、基础使用

查看健康状况
http://localhost:8080/actuator/health

查看各种监控指标jvm的请求的等等
http://localhost:8080/actuator/metrics 

查看配置信息
http://localhost:8080/actuator/configprops

查看日志
http://localhost:8080/actuator/loggers


查看内存使用情况
https://cunchu.51240.com/ 转换网址
http://localhost:8080/actuator/metrics/jvm.memory.used

三、根据需要配置自己需要的,加权限

application.properties

1、开启关闭监控点
management.endpoint.<NAME>.enable=true
management.endpoint.<NAME>.enable=false

2、不想全开启想开启某些的的话可以设置
actuator默认时全开启的
方式一
1)关闭默认
management.endpoints.enabled-by-default=false
2)设置自己想要看的部分
management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true
management.endpoint.env.enabled=true

方式二 
management.endpoints.web.exposure.include= beans,metrics,info,health,loggers
方式三 的作用大于方式二 二三一起结合访问不了loggers
management.endpoint.loggers.enabled=false

3、暴露访问的接口

# 包含那些协议
management.endpoint.<PROTOCOL>.expose.exclude=
management.endpoint.<PROTOCOL>.expose.include=

# 监控标准支持那些 included http endpoints
management.endpoint.http.expose.include=metrics

#exclude jmx endpoints
management.endpoints.jmx.exposure.exclude=beans


# security 默认密码 random string
#spring.security.user.name=<username>
#spring.security.user.password=<password>
spring.security.user.name=ADMIN
spring.security.user.password=123456

四、配置

1、health的配置


package com.hao.flame.health;

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


/**
 * @author haoxiansheng
 * @date 2020-05-30
 * 用户自定义HealthIndicator
 */
@Component // customizing the health Endpoint
public class FooServiceHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // perform a custom health check

        // inspect the status

        // healthy
        //return Health.up().build();

        // unhealthy
        return Health.down()
                .withDetail("response_code", "...")
                .withDetail("response_ms", "...")
                .withDetail("num_retries","...")
                .build();
    }
}


package com.hao.flame.health;

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

/**
 * @author haoxiansheng
 * @date 2020-05-30
 */
@Component
public class MaxMemoryHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        boolean invalid = Runtime.getRuntime().maxMemory() < (1000 * 1024*1024);
        Status status = invalid ? Status.DOWN : Status.UP;
        return Health.status(status).build();
    }
}

2、endpoint实现

package com.hao.flame.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author haoxiansheng
 * @date 2020-05-30
 */
@Component
@Endpoint(id = "readiness")
public class ReadinessEndpoint {
    private String ready = "NOT_READY";

    @ReadOperation
    public String getReadiness() {
        return ready;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void setReady() {
        ready = "READY";
    }

}




package com.hao.flame;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

/**
 * @author haoxiansheng
 * @date 2020-05-30
 */
@Component
@Endpoint(id = "container")
public class DockerEndpoint {

    @ReadOperation
    public String foo() {
        // Gather and return information(e.g. get info about the docker container)
        return "info";
    }

    @WriteOperation()
    public void bar() {
        // Do some action (e.g. restart the docker container)
    }
}

3、info

package com.hao.flame;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

/**
 * @author haoxiansheng
 * @date 2020-05-30
 */
@Component // customizing the info Endpoint
public class ProjectInfoContributor implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {
        // add new info
        builder.withDetail("project_name", "...")
                .withDetail("owned_by_team", "...")
                .withDetail("point_of_contact", "....");
    }
}

4、ActuatorSecurity 拦截实现

只有角色时ADMIN才可以访问
package com.hao.flame;


import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
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;

/**
 * @author haoxiansheng
 * @date 2020-05-30
 */
@EnableWebSecurity
@Configuration(proxyBeanMethods = false)
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

    /**
     * 小心导入包错误 {@link EndpointRequest}
     * import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests((requests) ->
                requests.anyRequest().hasRole("ADMIN"));

        http.httpBasic();
    }

}

官网地址

官网地址

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值