下面列出了怎么用org.springframework.boot.actuate.endpoint.EndpointId的API类实例代码及写法,或者点击链接到github查看源代码。
private ExposableWebEndpoint createEndpoint(final String name) {
return new ExposableWebEndpoint() {
@Override
public String getRootPath() {
return name;
}
@Override
public EndpointId getEndpointId() {
return EndpointId.of(name);
}
@Override
public boolean isEnableByDefault() {
return false;
}
@Override
public Collection<WebOperation> getOperations() {
return null;
}
};
}
@SuppressWarnings("unchecked")
void assertReadProperties(Class<?> testConfigClass) {
load(testConfigClass, (discoverer) -> {
Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
assertThat(endpoints).containsKey(endpointId);
ExposableWebEndpoint endpoint = endpoints.get(endpointId);
assertThat(endpoint.getOperations()).hasSize(1);
WebOperation operation = endpoint.getOperations().iterator().next();
Object invoker = ReflectionTestUtils.getField(operation, "invoker");
assertThat(invoker).isInstanceOf(ReflectiveOperationInvoker.class);
Map<String, Properties> properties = (Map<String, Properties>) ((ReflectiveOperationInvoker) invoker).invoke(
new InvocationContext(mock(SecurityContext.class), Collections.emptyMap()));
assertThat(properties).hasSize(1);
});
}
private void load(Function<EndpointId, Long> timeToLive,
PathMapper endpointPathMapper,
Class<?> configuration,
Consumer<WebEndpointDiscoverer> consumer) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configuration)) {
ConversionServiceParameterValueMapper parameterMapper = new ConversionServiceParameterValueMapper(DefaultConversionService.getSharedInstance());
EndpointMediaTypes mediaTypes = new EndpointMediaTypes(
Collections.singletonList("application/json"),
Collections.singletonList("application/json"));
WebEndpointDiscoverer discoverer = new WebEndpointDiscoverer(context,
parameterMapper,
mediaTypes,
Collections.singletonList(endpointPathMapper),
Collections.singleton(new CachingOperationInvokerAdvisor(timeToLive)),
Collections.emptyList());
consumer.accept(discoverer);
}
}
protected String getHealthEndpointPath() {
String health = pathMappedEndpoints.getPath(EndpointId.of("health"));
if (StringUtils.hasText(health)) {
return health;
}
String status = pathMappedEndpoints.getPath(EndpointId.of("status"));
if (StringUtils.hasText(status)) {
return status;
}
throw new IllegalStateException("Either health or status endpoint must be enabled!");
}
@Test
public void test_mgmtPortPath() {
webEndpoint.setBasePath("/admin");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/alive");
publishApplicationReadyEvent(factory, 8080, 8081);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8081/admin");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/alive");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");
}
@Test
public void test_default() {
instanceProperties.setMetadata(singletonMap("instance", "test"));
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 8080, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");
assertThat(app.getMetadata()).containsExactly(entry("contributor", "test"), entry("instance", "test"));
}
@Test
public void test_ssl() {
server.setSsl(new Ssl());
server.getSsl().setEnabled(true);
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 8080, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("https://" + getHostname() + ":8080/actuator");
assertThat(app.getHealthUrl()).isEqualTo("https://" + getHostname() + ":8080/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("https://" + getHostname() + ":8080/");
}
@Test
public void test_ssl_management() {
management.setSsl(new Ssl());
management.getSsl().setEnabled(true);
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/alive");
publishApplicationReadyEvent(factory, 8080, 9090);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("https://" + getHostname() + ":9090/actuator");
assertThat(app.getHealthUrl()).isEqualTo("https://" + getHostname() + ":9090/actuator/alive");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/");
}
@Test
public void test_preferIpAddress_serveraddress_missing() {
instanceProperties.setPreferIp(true);
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/application/alive");
publishApplicationReadyEvent(factory, 8080, null);
Application app = factory.createApplication();
assertThat(app.getServiceUrl()).matches("http://\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}:8080/");
}
@Test
public void test_preferIpAddress_managementaddress_missing() {
instanceProperties.setPreferIp(true);
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/application/alive");
publishApplicationReadyEvent(factory, 8080, 8081);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).matches("http://\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}:8081/actuator");
}
@Test
public void test_preferIpAddress() throws UnknownHostException {
instanceProperties.setPreferIp(true);
server.setAddress(InetAddress.getByName("127.0.0.1"));
management.setAddress(InetAddress.getByName("127.0.0.2"));
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 8080, 8081);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://127.0.0.2:8081/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://127.0.0.2:8081/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://127.0.0.1:8080/");
}
@Test
public void test_all_baseUrls() {
instanceProperties.setManagementBaseUrl("http://management:8090");
instanceProperties.setServiceBaseUrl("http://service:80");
webEndpoint.setBasePath("/admin");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
Application app = factory.createApplication();
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/");
assertThat(app.getManagementUrl()).isEqualTo("http://management:8090/admin");
assertThat(app.getHealthUrl()).isEqualTo("http://management:8090/admin/health");
}
@Test
public void test_service_baseUrl() {
instanceProperties.setServiceBaseUrl("http://service:80");
webEndpoint.setBasePath("/admin");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
Application app = factory.createApplication();
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/");
assertThat(app.getManagementUrl()).isEqualTo("http://service:80/admin");
assertThat(app.getHealthUrl()).isEqualTo("http://service:80/admin/health");
}
@Test
public void test_service_path() {
instanceProperties.setServiceBaseUrl("http://service:80");
instanceProperties.setServicePath("app");
webEndpoint.setBasePath("/admin");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
Application app = factory.createApplication();
assertThat(app.getServiceUrl()).isEqualTo("http://service:80/app");
assertThat(app.getManagementUrl()).isEqualTo("http://service:80/app/admin");
assertThat(app.getHealthUrl()).isEqualTo("http://service:80/app/admin/health");
}
@Test
public void should_use_application_uri() {
when(this.pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
this.cfApplicationProperties.setUris(singletonList("application/Uppercase"));
Application app = this.factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://application/Uppercase/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://application/Uppercase/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://application/Uppercase/");
}
@Test
public void should_use_service_base_uri() {
when(this.pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
this.cfApplicationProperties.setUris(singletonList("application/Uppercase"));
this.instanceProperties.setServiceBaseUrl("https://serviceBaseUrl");
Application app = this.factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("https://serviceBaseUrl/actuator");
assertThat(app.getHealthUrl()).isEqualTo("https://serviceBaseUrl/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("https://serviceBaseUrl/");
}
@Test
public void test_contextPath_mgmtPath() {
servletContext.setContextPath("app");
webEndpoint.setBasePath("/admin");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
publishApplicationReadyEvent(factory, 8080, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8080/app/admin/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app");
}
@Test
public void test_contextPath_mgmtPortPath() {
servletContext.setContextPath("app");
webEndpoint.setBasePath("/admin");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/admin/health");
publishApplicationReadyEvent(factory, 8080, 8081);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":8081/admin");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":8081/admin/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":8080/app");
}
@Test
public void test_contextPath() {
servletContext.setContextPath("app");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 80, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/app/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/app/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/app");
}
@Test
public void test_servletPath() {
when(dispatcherServletPath.getPrefix()).thenReturn("app");
servletContext.setContextPath("srv");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 80, null);
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/srv/app/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/srv");
}
@Test
public void test_servicePath() {
servletContext.setContextPath("app");
when(pathMappedEndpoints.getPath(EndpointId.of("health"))).thenReturn("/actuator/health");
publishApplicationReadyEvent(factory, 80, null);
instance.setServicePath("/servicePath/");
Application app = factory.createApplication();
assertThat(app.getManagementUrl()).isEqualTo("http://" + getHostname() + ":80/servicePath/app/actuator");
assertThat(app.getHealthUrl()).isEqualTo("http://" + getHostname() + ":80/servicePath/app/actuator/health");
assertThat(app.getServiceUrl()).isEqualTo("http://" + getHostname() + ":80/servicePath/app");
}
AbstractEndpointTests(EndpointId endpointId) {
this.endpointId = endpointId;
}
void assertActuatorEndpointLoaded(Class<?> testConfigClass) {
load(testConfigClass, (discoverer) -> {
Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
assertThat(endpoints).containsOnlyKeys(endpointId);
});
}
void assertActuatorEndpointNotLoaded(Class<?> testConfigClass) {
load(testConfigClass, (discoverer) -> {
Map<EndpointId, ExposableWebEndpoint> endpoints = mapEndpoints(discoverer.getEndpoints());
assertThat(endpoints).doesNotContainKey(endpointId);
});
}
private void load(Class<?> configuration, Consumer<WebEndpointDiscoverer> consumer) {
this.load((id) -> null, EndpointId::toString, configuration, consumer);
}
private Map<EndpointId, ExposableWebEndpoint> mapEndpoints(Collection<ExposableWebEndpoint> endpoints) {
Map<EndpointId, ExposableWebEndpoint> endpointById = new HashMap<>();
endpoints.forEach((endpoint) -> endpointById.put(endpoint.getEndpointId(), endpoint));
return endpointById;
}