建议先预习Spring Boot Actuator,请戳《初识篇》
WEB Endpoints,以HTTP协议对外暴露提供访问接口。
█ 自动配置
META-INF/spring.factories:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration
- EndpointAutoConfiguration
公共配置,对于Web EndPoints与JMX EndPoints共用的配置。
@Configuration(proxyBeanMethods = false)
public class EndpointAutoConfiguration {
// 负责方法参数的转换的参数值映射器
@Bean
@ConditionalOnMissingBean
public ParameterValueMapper endpointOperationParameterMapper(
// 自动注入被@EndpointConverter标注的Converter与GenericConverter类型的bean
// Spring Boot Actuator默认没有提供,可自定义添加
@EndpointConverter ObjectProvider<Converter<?, ?>> converters,
@EndpointConverter ObjectProvider<GenericConverter> genericConverters) {
ConversionService conversionService = createConversionService(
converters.orderedStream().collect(Collectors.toList()),
genericConverters.orderedStream().collect(Collectors.toList()));
return new ConversionServiceParameterValueMapper(conversionService);
}
// 参数转换器
private ConversionService createConversionService(List<Converter<?, ?>> converters,
List<GenericConverter> genericConverters) {
// 默认为空,满足if条件
if (genericConverters.isEmpty() && converters.isEmpty()) {
// 添加Spring Boot默认提供的转换器
// ApplicationConversionService.getSharedInstance()也会调用new ApplicationConversionService();来添加默认提供的转换器
return ApplicationConversionService.getSharedInstance();
}
// 如果有自定义的转换器,在自定义的基础上,加上Spring Boot默认提供的转化器
ApplicationConversionService conversionService = new ApplicationConversionService();
converters.forEach(conversionService::addConverter);
genericConverters.forEach(conversionService::addConverter);
return conversionService;
}
// 缓存切面,第一次访问后,会将结果缓存起来。下一次再获取直接从缓存中取
// 可以设置缓存失效时间
@Bean
@ConditionalOnMissingBean
public CachingOperationInvokerAdvisor endpointCachingOperationInvokerAdvisor(Environment environment) {
return new CachingOperationInvokerAdvisor(new EndpointIdTimeToLivePropertyFunction(environment));
}
}
举例:自定义转换器
@Configuration
public class CustomConversionConfiguration{
/**
* 自定义Converter
* Converter接口是org.springframework.core.convert.converter.Converter
*
* 将Integer类型的值转换成String类型
*
*/
@Bean
// 加上注解标注给EndPoint提供服务
@EndpointConverter
public Converter<Integer, String> customConverter() {
return new CustomConverter();
}
static class CustomConverter implements Converter<Integer, String>{
@Override
public String convert(Integer source) {
if (source == null) {
return null;
}
return source.toString();
}
}
/**
*
* 自定义GenericConverter
* GenericConverter接口是:org.springframework.core.convert.converter.GenericConverter
*
* @return
*/
@Bean
// 加上注解标注给EndPoint提供服务
@EndpointConverter
public GenericConverter customGenericConverter() {
return new GenericConverter() {
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
Set<ConvertiblePair> convertiblePairSet = new HashSet<>(2);
// 将Integer类型的值转换成String类型的值
convertiblePairSet.add(new ConvertiblePair(Integer.class, String.class));
return convertiblePairSet;
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
// 实现自定义逻辑
return null;
}
};
}
}
- WebEndpointAutoConfiguration
用于Web EndPoints构建的相关配置。
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication
@AutoConfigureAfter(EndpointAutoConfiguration.class)
// WebEndpointProperties:management.endpoints.web前缀的配置
@EnableConfigurationProperties(WebEndpointProperties.class)
public class WebEndpointAutoConfiguration {
private final ApplicationContext applicationContext;
private final WebEndpointProperties properties;
// 通过构造器注入ApplicationContext与WebEndpointProperties
public WebEndpointAutoConfiguration(ApplicationContext applicationContext, WebEndpointProperties properties) {
this.applicationContext = applicationContext;
this.properties = properties;
}
// 访问路径映射
// 默认EndPoint的id就是访问路径,可通过配置management.endpoints.web.pathMapping更改访问路径。
@Bean
public PathMapper webEndpointPathMapper() {
// 默认this.properties.getPathMapping()是空集合
return new MappingWebEndpointPathMapper(this.properties.getPathMapping());
}
// 默认的接口响应Media类型
@Bean
@ConditionalOnMissingBean
public EndpointMediaTypes endpointMediaTypes() {
// application/vnd.spring-boot.actuator.v3+json
// application/vnd.spring-boot.actuator.v2+json
// application/json
return EndpointMediaTypes.DEFAULT;
}
// EndPoint http服务暴露过滤器
// 根据配置:management.endpoints.web.exposure.include 设置暴露服务
// 根据配置:management.endpoints.web.exposure.exclude 设置不暴露服务
@Bean
public ExposeExcludePropertyEndpointFilter<ExposableWebEndpoint> webExposeExcludePropertyEndpointFilter() {
WebEndpointProperties.Exposure exposure = this.properties.getExposure();
// 默认暴露 info、health
return new ExposeExcludePropertyEndpointFilter<>(ExposableWebEndpoint.class, exposure.getInclude(),
exposure.getExclude(), "info", "health");
}
// Controller EndPoint服务暴露过滤
@Bean
public ExposeExcludePropertyEndpointFilter<ExposableControllerEndpoint> controllerExposeExcludePropertyEndpointFilter() {
WebEndpointProperties.Exposure exposure = th