<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
默认包含端点
web应用另外包含的端点
上述端点默认都是开启的,只有shutdown没有开启,可通过配置(application.properties)设置开启状态
# 关闭所有默认设置
management.endpoints.enabled-by-default=false
# 手动开启info端点
management.endpoint.info.enabled=true
# 手动开启shutdown端点
management.endpoint.shutdown.enabled=true
端点默认暴露情况
默认只有health和info暴露
所有都不配置,默认访问
表示应用在线
# 手动暴露指定端点
management.endpoints.web.exposure.include=mappings,metrics
# 手动暴露所有端点
management.endpoints.web.exposure.include=*
暴露全部端点,部分端点未打印出来,是因为项目里没有引入相应依赖
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"beans": {
"href": "http://localhost:8080/actuator/beans",
"templated": false
},
"caches": {
"href": "http://localhost:8080/actuator/caches",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8080/actuator/caches/{cache}",
"templated": true
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:8080/actuator/conditions",
"templated": false
},
"configprops-prefix": {
"href": "http://localhost:8080/actuator/configprops/{prefix}",
"templated": true
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8080/actuator/env/{toMatch}",
"templated": true
},
"env": {
"href": "http://localhost:8080/actuator/env",
"templated": false
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8080/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8080/actuator/threaddump",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics",
"templated": false
},
"quartz": {
"href": "http://localhost:8080/actuator/quartz",
"templated": false
},
"quartz-jobsOrTriggers-group": {
"href": "http://localhost:8080/actuator/quartz/{jobsOrTriggers}/{group}",
"templated": true
},
"quartz-jobsOrTriggers": {
"href": "http://localhost:8080/actuator/quartz/{jobsOrTriggers}",
"templated": true
},
"quartz-jobsOrTriggers-group-name": {
"href": "http://localhost:8080/actuator/quartz/{jobsOrTriggers}/{group}/{name}",
"templated": true
},
"scheduledtasks": {
"href": "http://localhost:8080/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings",
"templated": false
}
}
}
端点的部分信息我们不想展示给外部,可以通过security配置权限来控制
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// 配置密码加密
@Bean
PasswordEncoder passwordEncoder(){
// 使用BCrypt强哈希函数,strength默认为10
return new BCryptPasswordEncoder();
}
// 配置用户
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception{
auth.inMemoryAuthentication()
.withUser("admin")
.password("$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq") // 123
.roles("admin")
.and()
.withUser("sang")
.password("$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq") // 123
.roles("user");
}
@Override
protected void configure(HttpSecurity http) throws Exception{
// 在自定义配置中禁用 csrf校验,否则 security会默认校验token,直调非get接口会提示403
http.csrf().disable();
// 匹配所有的Endpoint,但不包括@RequestMapping注解
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests()
.anyRequest().hasRole("admin")
.and()
.httpBasic();
}
}
再访问/actuator则需要登录,只有admin角色才能看到,user角色会提示403
对于不带参数的端点可以设置缓存
management.endpoint.beans.cache.time-to-live=100s
注意:如果配置了端点保护,缓存则不会生效,此时Principal会被视为端点的输入
默认路径为/actuator或/actuator/xxx,可对其进行修改
management.endpoints.web.base-path=/actu
management.endpoints.web.path-mapping.health=healthcheck
默认所有端点都不允许跨域访问
management.endpoints.web.cors.allowed-origins=http://localhost:8081
management.endpoints.web.cors.allowed-methods=GET,POST
默认不展示详细信息,如果需要查看,需要配置
management.endpoint.health.show-details=when_authorized // nerver 默认,when_authorized 认证用户,always 展示给所有用户
management.endpoint.health.roles=admin // 设置admin角色才能查看
management.health.defaults.enabled=false // 关闭所有配置
需要实现HealthIndicator接口
自定义网络连接数据
@Component
public class CustomHealth implements HealthIndicator {
public static boolean checkNetWork(){
String url = "www.google.com"; //要访问的URL地址,超时
// String url = "www.baidu.com"; // 连接正常
try (Socket socket = new Socket()) {
InetAddress address = InetAddress.getByName(url);
int timeout = 3000; //设置超时时间为3秒
socket.connect(new InetSocketAddress(address, 80), timeout);
socket.close();
return true;
} catch (Exception e){
System.err.println("无法连接到网络或者连接超时");
}
return false;
}
@Override
public Health health(){
if(checkNetWork()){
return Health.up().withDetail("msg", "网络连接正常...").build();
}
// 响应状态有DOWN、UP、OUT_OF_SERVICE、UNKNOWN
return Health.down().withDetail("msg", "网络断开...").build();
}
}
连接www.google.com测试
连接www.baidu.com测试
1、自定义信息,application.properties中的配置
info.author.name=star
info.aythor.address=beijing
2、git信息
3、项目构建信息
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.7.3</version>
</dependency>
项目启动类开启AdminServer
@EnableAdminServer // 开启AdminServer
application.properties中将client服务注册到AdminServer
spring.boot.admin.client.url=http://localhost:8080