一、创建项目引入依赖
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();
}
}