下面列出了怎么用org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties的API类实例代码及写法,或者点击链接到github查看源代码。
static Optional<Pattern> getEndpointsPatterns(WebEndpointProperties webEndpointProperties,
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
Collection<ExposableWebEndpoint> endpoints = endpointsSupplier.getEndpoints();
if (endpoints.isEmpty()) {
return Optional.empty();
}
String pattern = endpoints.stream().map(PathMappedEndpoint::getRootPath)
.map(path -> path + "|" + path + "/.*").collect(
Collectors.joining("|",
getPathPrefix(webEndpointProperties.getBasePath()) + "/(",
")"));
if (StringUtils.hasText(pattern)) {
return Optional.of(Pattern.compile(pattern));
}
return Optional.empty();
}
@Test
public void testShouldReturnEndpointsWithoutContextPath() {
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
ServerProperties properties = new ServerProperties();
properties.getServlet().setContextPath("foo");
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = () -> {
ExposableWebEndpoint infoEndpoint = createEndpoint("info");
ExposableWebEndpoint healthEndpoint = createEndpoint("health");
return Arrays.asList(infoEndpoint, healthEndpoint);
};
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(webEndpointProperties, endpointsSupplier)
.pattern();
then(pattern).isNotEmpty();
then(pattern.get().pattern())
.isEqualTo("/actuator/(info|info/.*|health|health/.*)");
}
@Test
public void testShouldReturnEndpointsWithoutContextPathAndBasePathSetToRoot() {
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
webEndpointProperties.setBasePath("/");
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = () -> {
ExposableWebEndpoint infoEndpoint = createEndpoint("info");
ExposableWebEndpoint healthEndpoint = createEndpoint("health");
return Arrays.asList(infoEndpoint, healthEndpoint);
};
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(webEndpointProperties, endpointsSupplier)
.pattern();
then(pattern).isNotEmpty();
then(pattern.get().pattern()).isEqualTo("/(info|info/.*|health|health/.*)");
}
@Test
public void testShouldReturnEndpointsWithContextPathAndBasePathSetToRoot() {
WebEndpointProperties webEndpointProperties = new WebEndpointProperties();
webEndpointProperties.setBasePath("/");
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = () -> {
ExposableWebEndpoint infoEndpoint = createEndpoint("info");
ExposableWebEndpoint healthEndpoint = createEndpoint("health");
return Arrays.asList(infoEndpoint, healthEndpoint);
};
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(webEndpointProperties, endpointsSupplier)
.pattern();
then(pattern).isNotEmpty();
then(pattern.get().pattern()).isEqualTo("/(info|info/.*|health|health/.*)");
}
@Autowired
public LightminClientProperties(final ManagementServerProperties managementServerProperties,
final ServerProperties serverProperties,
@Value("${spring.batch.lightmin.application-name:null}") final String name,
@Value("${endpoints.health.id:health}") final String healthEndpointId,
final WebEndpointProperties webEndpointProperties,
final Environment environment) {
if (name == null || "null".equals(name)) {
this.name = environment.getProperty("spring.application.name", "spring-boot-application");
} else {
this.name = name;
}
this.healthEndpointId = healthEndpointId;
this.managementServerProperties = managementServerProperties;
this.serverProperties = serverProperties;
this.webEndpointProperties = webEndpointProperties;
}
/**
* Create a {@link ClusterCheckerTask} if one hasn't been supplied.
*
* @param genieHostInfo Information about the host this Genie process is running on
* @param properties The properties to use to configure the task
* @param dataServices The {@link DataServices} instance to use
* @param restTemplate The rest template for http calls
* @param webEndpointProperties The properties where Spring actuator is running
* @param registry The spectator registry for getting metrics
* @return The {@link ClusterCheckerTask} instance
*/
@Bean
@ConditionalOnMissingBean(ClusterCheckerTask.class)
public ClusterCheckerTask clusterCheckerTask(
final GenieHostInfo genieHostInfo,
final ClusterCheckerProperties properties,
final DataServices dataServices,
@Qualifier("genieRestTemplate") final RestTemplate restTemplate,
final WebEndpointProperties webEndpointProperties,
final MeterRegistry registry
) {
return new ClusterCheckerTask(
genieHostInfo,
properties,
dataServices,
restTemplate,
webEndpointProperties,
registry
);
}
/**
* Constructor.
*
* @param genieHostInfo Information about the host this Genie process is running on
* @param properties The properties to use to configure the task
* @param dataServices The {@link DataServices} encapsulation instance to use
* @param restTemplate The rest template for http calls
* @param webEndpointProperties The properties where Spring actuator is running
* @param registry The spectator registry for getting metrics
*/
public ClusterCheckerTask(
@NotNull final GenieHostInfo genieHostInfo,
@NotNull final ClusterCheckerProperties properties,
@NotNull final DataServices dataServices,
@NotNull final RestTemplate restTemplate,
@NotNull final WebEndpointProperties webEndpointProperties,
@NotNull final MeterRegistry registry
) {
this.hostname = genieHostInfo.getHostname();
this.properties = properties;
this.persistenceService = dataServices.getPersistenceService();
this.restTemplate = restTemplate;
this.registry = registry;
this.scheme = this.properties.getScheme() + "://";
this.healthEndpoint = ":" + this.properties.getPort() + webEndpointProperties.getBasePath() + "/health";
this.healthIndicatorsToIgnore = Splitter.on(",").omitEmptyStrings()
.trimResults().splitToList(properties.getHealthIndicatorsToIgnore());
// Keep track of the number of nodes currently unreachable from the the master
Gauge.builder(UNHEALTHY_HOSTS_GAUGE_METRIC_NAME, this.errorCounts, Map::size)
.register(registry);
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.SAME)
public SkipPattern skipPatternForActuatorEndpointsSamePort(
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(webEndpointProperties, endpointsSupplier);
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
@ConditionalOnProperty(name = "management.server.servlet.context-path", havingValue = "/", matchIfMissing = true)
public SkipPattern skipPatternForActuatorEndpointsDifferentPort(
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(webEndpointProperties, endpointsSupplier);
}
@Test
public void testShouldReturnEmptyWhenNoEndpoints() {
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = Collections::emptyList;
Optional<Pattern> pattern = new SkipPatternAutoConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(new WebEndpointProperties(), endpointsSupplier)
.pattern();
then(pattern).isEmpty();
}
protected RequestMatcher createDelegate(WebApplicationContext context, RequestMatcherFactory requestMatcherFactory) {
WebEndpointProperties properties = context.getBean(WebEndpointProperties.class);
if (StringUtils.hasText(properties.getBasePath())) {
return requestMatcherFactory.antPath(properties.getBasePath() + "/**");
}
return EMPTY_MATCHER;
}
static Optional<Pattern> getEndpointsPatterns(String contextPath,
WebEndpointProperties webEndpointProperties,
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
Collection<ExposableWebEndpoint> endpoints = endpointsSupplier.getEndpoints();
if (endpoints.isEmpty()) {
return Optional.empty();
}
String basePath = webEndpointProperties.getBasePath();
String pattern = patternFromEndpoints(contextPath, endpoints, basePath);
if (StringUtils.hasText(pattern)) {
return Optional.of(Pattern.compile(pattern));
}
return Optional.empty();
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.SAME)
public SingleSkipPattern skipPatternForActuatorEndpointsSamePort(
final ServerProperties serverProperties,
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(
serverProperties.getServlet().getContextPath(), webEndpointProperties,
endpointsSupplier);
}
@Bean
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
@ConditionalOnProperty(name = "management.server.servlet.context-path",
havingValue = "/", matchIfMissing = true)
public SingleSkipPattern skipPatternForActuatorEndpointsDifferentPort(
final ServerProperties serverProperties,
final WebEndpointProperties webEndpointProperties,
final EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier) {
return () -> getEndpointsPatterns(null, webEndpointProperties,
endpointsSupplier);
}
@Test
public void should_return_empty_when_no_endpoints() {
EndpointsSupplier<ExposableWebEndpoint> endpointsSupplier = Collections::emptyList;
Optional<Pattern> pattern = new SkipPatternConfiguration.ActuatorSkipPatternProviderConfig()
.skipPatternForActuatorEndpointsSamePort(new ServerProperties(),
new WebEndpointProperties(), endpointsSupplier)
.skipPattern();
then(pattern).isEmpty();
}
@Bean
@ConditionalOnProperty("management.server.port")
ArmeriaServerConfigurator secureActuatorServerConfigurator(WebEndpointProperties properties,
ManagementServerProperties serverProperties,
ConfigurableEnvironment environment,
ArmeriaSettings armeriaSettings) {
return sb -> {
final Port port = obtainManagementServerPort(serverProperties.getPort());
if (port != null) {
configurePorts(sb, ImmutableList.of(port));
addLocalManagementPortPropertyAlias(environment, port);
configureSecureDecorator(sb, port, properties.getBasePath(), armeriaSettings);
}
};
}
public CloudFoundryApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor, CloudFoundryApplicationProperties cfApplicationProperties) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
this.cfApplicationProperties = cfApplicationProperties;
this.instance = instance;
}
public DefaultApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
MetadataContributor metadataContributor) {
this.instance = instance;
this.management = management;
this.server = server;
this.pathMappedEndpoints = pathMappedEndpoints;
this.webEndpoint = webEndpoint;
this.metadataContributor = metadataContributor;
}
public ServletApplicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, MetadataContributor metadataContributor,
DispatcherServletPath dispatcherServletPath) {
super(instance, management, server, pathMappedEndpoints, webEndpoint, metadataContributor);
this.servletContext = servletContext;
this.server = server;
this.management = management;
this.instance = instance;
this.dispatcherServletPath = dispatcherServletPath;
}
@Bean
@Lazy(false)
@ConditionalOnMissingBean
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, ServletContext servletContext, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, ObjectProvider<List<MetadataContributor>> metadataContributors,
DispatcherServletPath dispatcherServletPath) {
return new ServletApplicationFactory(instance, management, server, servletContext, pathMappedEndpoints,
webEndpoint,
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)),
dispatcherServletPath);
}
@Bean
@Lazy(false)
@ConditionalOnMissingBean
public ApplicationFactory applicationFactory(InstanceProperties instance, ManagementServerProperties management,
ServerProperties server, PathMappedEndpoints pathMappedEndpoints, WebEndpointProperties webEndpoint,
ObjectProvider<List<MetadataContributor>> metadataContributors) {
return new DefaultApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint,
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)));
}
@Bean
@Lazy(false)
@ConditionalOnMissingBean
public CloudFoundryApplicationFactory applicationFactory(InstanceProperties instance,
ManagementServerProperties management, ServerProperties server, PathMappedEndpoints pathMappedEndpoints,
WebEndpointProperties webEndpoint, ObjectProvider<List<MetadataContributor>> metadataContributors,
CloudFoundryApplicationProperties cfApplicationProperties) {
return new CloudFoundryApplicationFactory(instance, management, server, pathMappedEndpoints, webEndpoint,
new CompositeMetadataContributor(metadataContributors.getIfAvailable(Collections::emptyList)),
cfApplicationProperties);
}
@Autowired
public GlobalModelAttributes(final WebEndpointProperties webEndpointProperties,
final EdisonApplicationProperties edisonApplicationProperties) {
this.webEndpointProperties = webEndpointProperties;
this.edisonApplicationProperties = edisonApplicationProperties;
}
@Bean
WebEndpointProperties webEndpointProperties() {
return Mockito.mock(WebEndpointProperties.class);
}