Add Actuator Health Checking

pull/808/head
Your Name 2023-07-22 23:58:25 -05:00
parent 61c7131343
commit 19f71cfa52
4 changed files with 115 additions and 1 deletions

View File

@ -18,10 +18,27 @@ package me.zhengjie.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@ -30,6 +47,7 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
@ -40,6 +58,7 @@ import java.util.List;
*/
@Configuration
@EnableWebMvc
@EnableSwagger2
public class ConfigurerAdapter implements WebMvcConfigurer {
/** 文件配置 */
@ -85,4 +104,49 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
converter.setDefaultCharset(StandardCharsets.UTF_8);
converters.add(converter);
}
/**
* endpoints,springboot2.6.xactuator error problems
*
* @param webEndpointsSupplier the web endpoints supplier
* @param servletEndpointsSupplier the servlet endpoints supplier
* @param controllerEndpointsSupplier the controller endpoints supplier
* @param endpointMediaTypes the endpoint media types
* @param corsProperties the cors properties
* @param webEndpointProperties the web endpoint properties
* @param environment the environment
* @return the web mvc endpoint handler mapping
*/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties,
Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping);
}
/**
* shouldRegisterLinksMapping
* @param webEndpointProperties webEndpointProperties
* @param environment environment
* @param basePath /
* @return boolean
*/
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties,
Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}

View File

@ -22,6 +22,8 @@ import me.zhengjie.modules.security.security.*;
import me.zhengjie.modules.security.service.OnlineUserService;
import me.zhengjie.modules.security.service.UserCacheManager;
import me.zhengjie.utils.enums.RequestMethodEnum;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -98,6 +100,8 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
// .antMatchers("/login").permitAll()
// 静态资源等等
.antMatchers(
HttpMethod.GET,

View File

@ -114,3 +114,38 @@ file:
# 文件大小 /M
maxSize: 100
avatarMaxSize: 5
management:
# 可以指定暴露哪些actuator服务'*'为全部注意加上引号被注释的写法表示只允许healthinfo, metrics, shutdown
endpoints:
web:
exposure:
include: health,info, metrics, shutdown
# include: '*' # all
# default: http://localhost:8000/actuator/*
# base-path: http://localhost:8000/${base-path}/*
# base-path: /check (web.base-path)
endpoint:
# 通过/actuator/shutdown停止服务
shutdown:
enabled: true
# 显示health的详细内容
health:
show-details: always
info: # 显示任意的应用信息,默认关闭,如果是更低一些的版本默认是开启的
env:
enabled: true
# 自定义/actuator/info中的各种内容可以自定义也可以取默认的一些系统/服务环境变量
info:
app:
encoding: "@project.build.sourceEncoding@"
java:
source: "@java.version@"
target: "@java.version@"
build:
artifact: @project.artifactId@
name: @project.name@
description: @project.description@
pomVersion: @project.version@
# 甚至可以自定义test
test: 'I love Spring Boot'

13
pom.xml
View File

@ -90,6 +90,11 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--监控sql日志-->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
@ -110,7 +115,13 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.43</version>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- druid数据源驱动 -->