Commit 756c6742 by Johannes Edmeier Committed by Johannes Edmeier

Rename client properties to `spring.boot.admin.client.*`

The Spring team discourages the use of the `spring.*` configuration namespace, hence I use the major update and move all properties. With this commit all client related properties are moved to `sbadmin.client`.
parent c60473e0
......@@ -68,65 +68,65 @@ TIP: There are plenty of properties to influence the way how the SBA Client regi
| Enables the Spring Boot Admin Client.
| `true`
| spring.boot.admin.url
| spring.boot.admin.client.url
| Comma separated ordered list of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. *Mandatory*.
|
| spring.boot.admin.api-path
| spring.boot.admin.client.api-path
| Http-path of registration endpoint at your admin server.
| `"api/applications"`
| spring.boot.admin.username +
spring.boot.admin.password
| spring.boot.admin.client.username +
spring.boot.admin.client.password
| Username and password in case the SBA Server api is protected with HTTP Basic authentication.
|
| spring.boot.admin.period
| spring.boot.admin.client.period
| Interval for repeating the registration (in ms).
| `10.000`
| spring.boot.admin.auto-registration
| spring.boot.admin.client.auto-registration
| If set to true the periodic task to register the application is automatically scheduled after the application is ready.
| `true`
| spring.boot.admin.auto-deregistration
| spring.boot.admin.client.auto-deregistration
| Switch to enable auto-deregistration at Spring Boot Admin server when context is closed.
| `false`
| spring.boot.admin.register-once
| If set to true the client will only register against one admin server (in order defined by `spring.boot.admin.url`); if that admin server goes down, will automatically register against the next admin server. If false, will register against all admin servers.
| spring.boot.admin.client.register-once
| If set to true the client will only register against one admin server (in order defined by `spring.boot.admin.instance.url`); if that admin server goes down, will automatically register against the next admin server. If false, will register against all admin servers.
| `true`
| spring.boot.admin.client.health-url
| Client-health-url to register with. Can be overridden in case the reachable URL is different (e.g. Docker). Must be unique in registry.
| spring.boot.admin.client.instance.health-url
| Health-url to register with. Can be overridden in case the reachable URL is different (e.g. Docker). Must be unique in registry.
| Guessed based on management-url and `endpoints.health.id`.
| spring.boot.admin.client.management-base-url
| spring.boot.admin.client.instance.management-base-url
| Base url for computing the management-url to register with. The path is inferred at runtime, and appended to the base url.
| Guessed based on `management.port`, service-url and `server.servlet-path`.
| spring.boot.admin.client.management-url
| spring.boot.admin.client.instance.management-url
| Management-url to register with. Can be overridden in case the reachable url is different (e.g. Docker).
| Guessed based on managment-base-url and `management.context-path`.
| spring.boot.admin.client.service-base-url
| spring.boot.admin.client.instance.service-base-url
| Base url for computing the service-url to register with. The path is inferred at runtime, and appended to the base url.
| Guessed based on hostname, `server.port`.
| spring.boot.admin.client.service-url
| Client-service-url to register with. Can be overridden in case the reachable url is different (e.g. Docker).
| spring.boot.admin.client.instance.service-url
| Service-url to register with. Can be overridden in case the reachable url is different (e.g. Docker).
| Guessed based on service-base-url and `server.context-path`.
| spring.boot.admin.client.name
| spring.boot.admin.client.instance.name
| Name to register with.
| `${spring.application.name}` if set, `"spring-boot-application"` otherwise.
| spring.boot.admin.client.prefer-ip
| spring.boot.admin.client.instance.prefer-ip
| Use the ip-address rather then the hostname in the guessed urls. If `server.address` / `management.address` is set, it get used. Otherwise the IP address returned from `InetAddress.getLocalHost()` gets used.
| `false`
| spring.boot.admin.client.metadata.*
| Metadata key-value-pairs to be associated with this instance.
| spring.boot.admin.client.instance.metadata.*
| Metadata key-value-pairs to be asscoiated with this instance.
|
|===
......
......@@ -68,7 +68,7 @@ Each application that wants to register has to include the Spring Boot Admin Cli
[source,yml]
.application.yml
----
spring.boot.admin.url: http://localhost:8080 #<1>
spring.boot.admin.client.url: http://localhost:8080 #<1>
management.security.enabled: false #<2>
----
<1> The URL of the Spring Boot Admin Server to register at.
......
......@@ -14,7 +14,7 @@ include::{samples-dir}/spring-boot-admin-sample/src/main/java/de/codecentric/boo
For a complete sample look at https://github.com/codecentric/spring-boot-admin/tree/master/spring-boot-admin-samples/spring-boot-admin-sample/[spring-boot-admin-sample].
NOTE: If you protect the `/api/applications` endpoint don't forget to configure the username and password on your SBA-Client using `spring.boot.admin.username` and `spring.boot.admin.password`.
NOTE: If you protect the `/api/applications` endpoint don't forget to configure the username and password on your SBA-Client using `spring.boot.admin.client.username` and `spring.boot.admin.instance.password`.
TIP: There are more samples (e.g. using OAuth2) in https://github.com/joshiste/spring-boot-admin-samples[joshiste/spring-boot-admin-samples^].
......@@ -26,12 +26,12 @@ Submitting the credentials using SBA Client:
[source,yaml]
.application.yml
----
spring.boot.admin:
url: http://localhost:8080
client:
metadata:
user.name: ${security.user.name}
user.password: ${security.user.password}
spring.boot.admin.client:
url: http://localhost:8080
instance:
metadata:
user.name: ${security.user.name}
user.password: ${security.user.password}
----
Submitting the credentials using Eureka:
......
......@@ -3,5 +3,4 @@ server.port=8081
info.stage=test
logging.file=/tmp/log.log
spring.application.name=@pom.artifactId@
spring.boot.admin.url=http://localhost:8081
\ No newline at end of file
spring.boot.admin.client.url=http://localhost:8081
\ No newline at end of file
......@@ -15,7 +15,8 @@ spring:
name: "@pom.artifactId@"
boot:
admin:
url: http://localhost:8080
client:
url: http://localhost:8080
profiles:
active:
- secure
......@@ -37,12 +38,13 @@ spring:
profiles: secure
boot:
admin:
client:
username: "${security.user.name}" #These two are needed so that the client
password: "${security.user.password}" #can register at the protected server api
client:
instance:
metadata:
user.name: "${security.user.name}" #These two are needed so that the server
user.password: "${security.user.password}" #can access the proteceted client endpoints
user.name: "${security.user.name}" #These two are needed so that the server
user.password: "${security.user.password}" #can access the proteceted client endpoints
security:
user:
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -2923,7 +2923,7 @@ sntp@1.x.x:
sockjs-client@^1.0.3:
version "1.1.4"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.1.4.tgz#5babe386b775e4cf14e7520911452654016c8b12"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-instance-1.1.4.tgz#5babe386b775e4cf14e7520911452654016c8b12"
dependencies:
debug "^2.6.6"
eventsource "0.1.6"
......
......@@ -5,7 +5,7 @@
<parent>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>spring-boot-admin-server-ui-login</artifactId>
......
......@@ -2907,7 +2907,7 @@ sntp@1.x.x:
sockjs-client@^1.0.3:
version "1.1.4"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.1.4.tgz#5babe386b775e4cf14e7520911452654016c8b12"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-instance-1.1.4.tgz#5babe386b775e4cf14e7520911452654016c8b12"
dependencies:
debug "^2.6.6"
eventsource "0.1.6"
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -963,7 +963,7 @@ encoding@^0.1.11:
engine.io-client@1.6.9:
version "1.6.9"
resolved "https://registry.yarnpkg.com/engine.io-client/-/engine.io-client-1.6.9.tgz#1d6ad48048a5083c95096943b29d36efdb212401"
resolved "https://registry.yarnpkg.com/engine.io-client/-/engine.io-instance-1.6.9.tgz#1d6ad48048a5083c95096943b29d36efdb212401"
dependencies:
component-emitter "1.1.2"
component-inherit "0.0.3"
......@@ -3565,7 +3565,7 @@ socket.io-adapter@0.4.0:
socket.io-client@1.4.6:
version "1.4.6"
resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-1.4.6.tgz#49b0ba537efd15b8297c84016e642e1c7c752c3d"
resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-instance-1.4.6.tgz#49b0ba537efd15b8297c84016e642e1c7c752c3d"
dependencies:
backo2 "1.0.2"
component-bind "1.0.0"
......@@ -3612,7 +3612,7 @@ socket.io@1.4.7:
sockjs-client@^1.0.3:
version "1.1.4"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.1.4.tgz#5babe386b775e4cf14e7520911452654016c8b12"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-instance-1.1.4.tgz#5babe386b775e4cf14e7520911452654016c8b12"
dependencies:
debug "^2.6.6"
eventsource "0.1.6"
......
......@@ -17,118 +17,118 @@ package de.codecentric.boot.admin.client.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.boot.admin")
public class AdminProperties {
/**
* The admin server urls to register at
*/
private String[] url;
/**
* The admin rest-apis path.
*/
private String apiPath = "api/applications";
/**
* Time interval (in ms) the registration is repeated
*/
private long period = 10_000L;
/**
* Username for basic authentication on admin server
*/
private String username;
/**
* Password for basic authentication on admin server
*/
private String password;
/**
* Enable automatic deregistration on shutdown
*/
private boolean autoDeregistration;
/**
* Enable automatic registration when the application is ready
*/
private boolean autoRegistration = true;
/**
* Enable registration against one or all admin servers
*/
private boolean registerOnce = true;
public void setUrl(String[] url) {
this.url = url.clone();
}
public String[] getUrl() {
return url.clone();
}
public void setApiPath(String apiPath) {
this.apiPath = apiPath;
}
public String getApiPath() {
return apiPath;
}
public String[] getAdminUrl() {
String adminUrls[] = url.clone();
for (int i = 0; i < adminUrls.length; i++) {
adminUrls[i] += "/" + apiPath;
}
return adminUrls;
}
public long getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public boolean isAutoDeregistration() {
return autoDeregistration;
}
public void setAutoDeregistration(boolean autoDeregistration) {
this.autoDeregistration = autoDeregistration;
}
public boolean isAutoRegistration() {
return autoRegistration;
}
public void setAutoRegistration(boolean autoRegistration) {
this.autoRegistration = autoRegistration;
}
public boolean isRegisterOnce() {
return registerOnce;
}
public void setRegisterOnce(boolean registerOnce) {
this.registerOnce = registerOnce;
}
@ConfigurationProperties(prefix = "spring.boot.admin.client")
public class ClientProperties {
/**
* The admin server urls to register at
*/
private String[] url;
/**
* The admin rest-apis path.
*/
private String apiPath = "api/applications";
/**
* Time interval (in ms) the registration is repeated
*/
private long period = 10_000L;
/**
* Username for basic authentication on admin server
*/
private String username;
/**
* Password for basic authentication on admin server
*/
private String password;
/**
* Enable automatic deregistration on shutdown
*/
private boolean autoDeregistration;
/**
* Enable automatic registration when the application is ready
*/
private boolean autoRegistration = true;
/**
* Enable registration against one or all admin servers
*/
private boolean registerOnce = true;
public void setUrl(String[] url) {
this.url = url.clone();
}
public String[] getUrl() {
return url.clone();
}
public void setApiPath(String apiPath) {
this.apiPath = apiPath;
}
public String getApiPath() {
return apiPath;
}
public String[] getAdminUrl() {
String adminUrls[] = url.clone();
for (int i = 0; i < adminUrls.length; i++) {
adminUrls[i] += "/" + apiPath;
}
return adminUrls;
}
public long getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public boolean isAutoDeregistration() {
return autoDeregistration;
}
public void setAutoDeregistration(boolean autoDeregistration) {
this.autoDeregistration = autoDeregistration;
}
public boolean isAutoRegistration() {
return autoRegistration;
}
public void setAutoRegistration(boolean autoRegistration) {
this.autoRegistration = autoRegistration;
}
public boolean isRegisterOnce() {
return registerOnce;
}
public void setRegisterOnce(boolean registerOnce) {
this.registerOnce = registerOnce;
}
}
......@@ -20,8 +20,8 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.boot.admin.client")
public class AdminClientProperties {
@ConfigurationProperties(prefix = "spring.boot.admin.client.instance")
public class InstanceProperties {
/**
* Management-url to register with. Inferred at runtime, can be overridden in case the
* reachable URL is different (e.g. Docker).
......
......@@ -37,31 +37,31 @@ import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@Configuration
@EnableConfigurationProperties({AdminProperties.class, AdminClientProperties.class})
@EnableConfigurationProperties({ClientProperties.class, InstanceProperties.class})
@Conditional(SpringBootAdminClientEnabledCondition.class)
public class SpringBootAdminClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public ApplicationRegistrator registrator(AdminProperties admin,
public ApplicationRegistrator registrator(ClientProperties client,
ApplicationFactory applicationFactory,
RestTemplateBuilder restTemplBuilder) {
RestTemplateBuilder builder = restTemplBuilder.messageConverters(new MappingJackson2HttpMessageConverter())
.requestFactory(SimpleClientHttpRequestFactory.class);
if (admin.getUsername() != null) {
builder = builder.basicAuthorization(admin.getUsername(), admin.getPassword());
if (client.getUsername() != null) {
builder = builder.basicAuthorization(client.getUsername(), client.getPassword());
}
return new ApplicationRegistrator(builder.build(), admin, applicationFactory);
return new ApplicationRegistrator(builder.build(), client, applicationFactory);
}
@Bean
@ConditionalOnMissingBean
public ApplicationFactory applicationFactory(AdminClientProperties client,
public ApplicationFactory applicationFactory(InstanceProperties instance,
ManagementServerProperties management,
ServerProperties server,
@Value("${endpoints.health.path:/${endpoints.health.id:health}}") String healthEndpointPath,
ServletContext servletContext) {
return new DefaultApplicationFactory(client, management, server, servletContext, healthEndpointPath);
return new DefaultApplicationFactory(instance, management, server, servletContext, healthEndpointPath);
}
@Bean
......@@ -76,13 +76,13 @@ public class SpringBootAdminClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public RegistrationApplicationListener registrationListener(AdminProperties admin,
public RegistrationApplicationListener registrationListener(ClientProperties client,
ApplicationRegistrator registrator) {
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
registrationTaskScheduler());
listener.setAutoRegister(admin.isAutoRegistration());
listener.setAutoDeregister(admin.isAutoDeregistration());
listener.setRegisterPeriod(admin.getPeriod());
listener.setAutoRegister(client.isAutoRegistration());
listener.setAutoDeregister(client.isAutoDeregistration());
listener.setRegisterPeriod(client.getPeriod());
return listener;
}
}
......@@ -25,9 +25,9 @@ import org.springframework.util.StringUtils;
/**
* This condition checks if the client should be enabled. Two properties are checked:
* spring.boot.admin.client.enabled and spring.boot.admin.url. The following table shows under which
* conditions the client is active.
*
* spring.boot.admin.client.enabled and spring.boot.admin.url. The following table shows under which conditions the
* client is active.
* <p>
* <pre>
* | enabled: false | enabled: true (default) |
* --------- | -------------- | ----------------------- |
......@@ -38,31 +38,29 @@ import org.springframework.util.StringUtils;
* </pre>
*/
public class SpringBootAdminClientEnabledCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata annotatedTypeMetadata) {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata annotatedTypeMetadata) {
if (!isEnabled(context.getEnvironment())) {
return ConditionOutcome.noMatch(
"Spring Boot Client is disabled, because 'spring.boot.admin.client.enabled' is false.");
}
if (!isEnabled(context.getEnvironment())) {
return ConditionOutcome.noMatch(
"Spring Boot Client is disabled, because 'spring.boot.admin.client.enabled' is false.");
}
if (isUrlEmpty(context.getEnvironment())) {
return ConditionOutcome.noMatch(
"Spring Boot Client is disabled, because 'spring.boot.admin.url' is empty.");
}
if (isUrlEmpty(context.getEnvironment())) {
return ConditionOutcome.noMatch(
"Spring Boot Client is disabled, because 'spring.boot.admin.url' is empty.");
}
return ConditionOutcome.match();
}
return ConditionOutcome.match();
}
private boolean isEnabled(Environment env) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(env,
"spring.boot.admin.client.");
return resolver.getProperty("enabled", Boolean.class, Boolean.TRUE);
}
private boolean isEnabled(Environment env) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(env, "spring.boot.admin.client.");
return resolver.getProperty("enabled", Boolean.class, Boolean.TRUE);
}
private boolean isUrlEmpty(Environment env) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(env, "spring.boot.admin.");
return StringUtils.isEmpty(resolver.getProperty("url", ""));
}
private boolean isUrlEmpty(Environment env) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(env, "spring.boot.admin.client.");
return StringUtils.isEmpty(resolver.getProperty("url", ""));
}
}
......@@ -15,7 +15,7 @@
*/
package de.codecentric.boot.admin.client.registration;
import de.codecentric.boot.admin.client.config.AdminProperties;
import de.codecentric.boot.admin.client.config.ClientProperties;
import java.util.Collections;
import java.util.Map;
......@@ -38,12 +38,14 @@ public class ApplicationRegistrator {
private static final HttpHeaders HTTP_HEADERS = createHttpHeaders();
private final AtomicInteger unsuccessfulAttempts = new AtomicInteger(0);
private final AtomicReference<String> registeredId = new AtomicReference<>();
private final AdminProperties admin;
private final ClientProperties client;
private final RestTemplate template;
private final ApplicationFactory applicationFactory;
public ApplicationRegistrator(RestTemplate template, AdminProperties admin, ApplicationFactory applicationFactory) {
this.admin = admin;
public ApplicationRegistrator(RestTemplate template,
ClientProperties client,
ApplicationFactory applicationFactory) {
this.client = client;
this.template = template;
this.applicationFactory = applicationFactory;
}
......@@ -63,7 +65,7 @@ public class ApplicationRegistrator {
public boolean register() {
boolean isRegistrationSuccessful = false;
Application self = createApplication();
for (String adminUrl : admin.getAdminUrl()) {
for (String adminUrl : client.getAdminUrl()) {
try {
@SuppressWarnings("rawtypes") ResponseEntity<Map> response = template.postForEntity(adminUrl,
new HttpEntity<>(self, HTTP_HEADERS), Map.class);
......@@ -76,7 +78,7 @@ public class ApplicationRegistrator {
}
isRegistrationSuccessful = true;
if (admin.isRegisterOnce()) {
if (client.isRegisterOnce()) {
break;
}
} else {
......@@ -93,10 +95,10 @@ public class ApplicationRegistrator {
if (unsuccessfulAttempts.get() == 0) {
LOGGER.warn(
"Failed to register application as {} at spring-boot-admin ({}): {}. Further attempts are logged on DEBUG level",
self, admin.getAdminUrl(), ex.getMessage());
self, client.getAdminUrl(), ex.getMessage());
} else {
LOGGER.debug("Failed to register application as {} at spring-boot-admin ({}): {}", self,
admin.getAdminUrl(), ex.getMessage());
client.getAdminUrl(), ex.getMessage());
}
}
}
......@@ -111,11 +113,11 @@ public class ApplicationRegistrator {
public void deregister() {
String id = registeredId.get();
if (id != null) {
for (String adminUrl : admin.getAdminUrl()) {
for (String adminUrl : client.getAdminUrl()) {
try {
template.delete(adminUrl + "/" + id);
registeredId.compareAndSet(id, null);
if (admin.isRegisterOnce()) {
if (client.isRegisterOnce()) {
break;
}
} catch (Exception ex) {
......@@ -127,10 +129,8 @@ public class ApplicationRegistrator {
}
/**
* Returns the id of this client as given by the admin server.
* @return the id of this client as given by the admin server.
* Returns null if the client has not registered against the admin server yet.
*
* @return
*/
public String getRegisteredId() {
return registeredId.get();
......
package de.codecentric.boot.admin.client.registration;
import de.codecentric.boot.admin.client.config.AdminClientProperties;
import de.codecentric.boot.admin.client.config.InstanceProperties;
import java.net.InetAddress;
import java.net.UnknownHostException;
......@@ -23,21 +23,20 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Rene Felgenträger
*/
public class DefaultApplicationFactory implements ApplicationFactory {
private AdminClientProperties client;
private InstanceProperties instance;
private ServerProperties server;
private ManagementServerProperties management;
private Integer localServerPort;
private Integer localManagementPort;
private final ServletContext servletContext;
private ServletContext servletContext;
private String healthEndpointPath;
public DefaultApplicationFactory(AdminClientProperties client,
public DefaultApplicationFactory(InstanceProperties instance,
ManagementServerProperties management,
ServerProperties server,
ServletContext servletContext,
String healthEndpointPath) {
this.client = client;
this.instance = instance;
this.management = management;
this.server = server;
this.servletContext = servletContext;
......@@ -55,15 +54,15 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
protected String getName() {
return client.getName();
return instance.getName();
}
protected String getServiceUrl() {
if (client.getServiceUrl() != null) {
return UriComponentsBuilder.fromUriString(client.getServiceUrl()).toUriString();
if (instance.getServiceUrl() != null) {
return UriComponentsBuilder.fromUriString(instance.getServiceUrl()).toUriString();
}
String baseUrl = client.getServiceBaseUrl();
String baseUrl = instance.getServiceBaseUrl();
if (getLocalServerPort() == null && StringUtils.isEmpty(baseUrl)) {
throw new IllegalStateException("service-base-url must be set when deployed to servlet-container");
}
......@@ -82,11 +81,11 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
protected String getManagementUrl() {
if (client.getManagementUrl() != null) {
return client.getManagementUrl();
if (instance.getManagementUrl() != null) {
return instance.getManagementUrl();
}
String baseUrl = client.getManagementBaseUrl();
String baseUrl = instance.getManagementBaseUrl();
UriComponentsBuilder builder;
if (!StringUtils.isEmpty(baseUrl)) {
......@@ -109,8 +108,8 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
protected String getHealthUrl() {
if (client.getHealthUrl() != null) {
return client.getHealthUrl();
if (instance.getHealthUrl() != null) {
return instance.getHealthUrl();
}
return UriComponentsBuilder.fromHttpUrl(getManagementUrl())
.path("/")
......@@ -120,7 +119,7 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
protected Map<String, String> getMetadata() {
return client.getMetadata();
return instance.getMetadata();
}
protected String getServiceHost() {
......@@ -164,7 +163,7 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
protected String getHost(InetAddress address) {
return client.isPreferIp() ? address.getHostAddress() : address.getCanonicalHostName();
return instance.isPreferIp() ? address.getHostAddress() : address.getCanonicalHostName();
}
@EventListener
......
......@@ -4,7 +4,7 @@
{
"name": "spring.boot.admin.client.enabled",
"type": "java.lang.Boolean",
"description": "Enable Spring Admin Client.",
"description": "Enable Spring Boot Admin Client.",
"defaultValue": "true"
}
]}
package de.codecentric.boot.admin.client.config;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -12,20 +9,21 @@ import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import de.codecentric.boot.admin.client.config.AdminProperties;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=0", "spring.boot.admin.url=http://example.com" })
"management.port=0", "spring.boot.admin.client.url=http://example.com"})
public class ClientApplicationTest {
@Autowired
private AdminProperties properties;
@Autowired
private ClientProperties properties;
@Test
public void test_context() {
assertThat(properties, notNullValue());
}
@Test
public void test_context() {
assertThat(properties, notNullValue());
}
}
@Configuration
......
......@@ -14,7 +14,6 @@ import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertTrue;
public class SpringBootAdminClientAutoConfigurationTest {
private ConfigurableApplicationContext context;
@After
......@@ -32,28 +31,24 @@ public class SpringBootAdminClientAutoConfigurationTest {
@Test
public void active() {
load("spring.boot.admin.url:http://localhost:8081");
load("spring.boot.admin.client.url:http://localhost:8081");
context.getBean(ApplicationRegistrator.class);
}
@Test
public void disabled() {
load("spring.boot.admin.url:http://localhost:8081", "spring.boot.admin.client.enabled:false");
load("spring.boot.admin.client.url:http://localhost:8081", "spring.boot.admin.client.enabled:false");
assertTrue(context.getBeansOfType(ApplicationRegistrator.class).isEmpty());
}
private void load(final String... environment) {
SpringApplication springApplication = new SpringApplication(TestClientApplication.class);
springApplication.addInitializers(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
}
});
springApplication.addInitializers(
(ApplicationContextInitializer<ConfigurableApplicationContext>) applicationContext -> EnvironmentTestUtils
.addEnvironment(applicationContext, environment));
this.context = springApplication.run("--server.port=0");
}
@Configuration
@EnableAutoConfiguration
static class TestClientApplication {
......
package de.codecentric.boot.admin.client.config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import org.junit.Before;
import org.junit.Test;
import org.mockito.BDDMockito;
......@@ -11,50 +7,52 @@ import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.mock.env.MockEnvironment;
import de.codecentric.boot.admin.client.config.SpringBootAdminClientEnabledCondition;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
public class SpringBootAdminClientEnabledConditionTest {
private SpringBootAdminClientEnabledCondition condition;
private AnnotatedTypeMetadata annotatedTypeMetadata;
private ConditionContext conditionContext;
@Before
public void setUp() {
condition = new SpringBootAdminClientEnabledCondition();
annotatedTypeMetadata = mock(AnnotatedTypeMetadata.class);
conditionContext = mock(ConditionContext.class);
}
@Test
public void test_emptyUrl_enabled() {
MockEnvironment environment = new MockEnvironment();
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertFalse(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
@Test
public void test_emptyUrl_disabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.boot.admin.client.enabled", "false");
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertFalse(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
@Test
public void test_nonEmptyUrl_disabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.boot.admin.client.enabled", "false");
environment.setProperty("spring.boot.admin.url", "http://localhost:8080/management");
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertFalse(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
@Test
public void test_nonEmptyUrl_enabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.boot.admin.url", "http://localhost:8080/management");
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertTrue(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
private SpringBootAdminClientEnabledCondition condition;
private AnnotatedTypeMetadata annotatedTypeMetadata;
private ConditionContext conditionContext;
@Before
public void setUp() {
condition = new SpringBootAdminClientEnabledCondition();
annotatedTypeMetadata = mock(AnnotatedTypeMetadata.class);
conditionContext = mock(ConditionContext.class);
}
@Test
public void test_emptyUrl_enabled() {
MockEnvironment environment = new MockEnvironment();
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertFalse(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
@Test
public void test_emptyUrl_disabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.boot.admin.client.enabled", "false");
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertFalse(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
@Test
public void test_nonEmptyUrl_disabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.boot.admin.client.enabled", "false");
environment.setProperty("spring.boot.admin.client.url", "http://localhost:8080/management");
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertFalse(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
@Test
public void test_nonEmptyUrl_enabled() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.boot.admin.client.url", "http://localhost:8080/management");
BDDMockito.given(conditionContext.getEnvironment()).willReturn(environment);
assertTrue(condition.getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch());
}
}
......@@ -37,14 +37,14 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.client.config.AdminProperties;
import de.codecentric.boot.admin.client.config.ClientProperties;
import de.codecentric.boot.admin.client.registration.Application;
import de.codecentric.boot.admin.client.registration.ApplicationFactory;
import de.codecentric.boot.admin.client.registration.ApplicationRegistrator;
public class ApplicationRegistratorTest {
private AdminProperties adminProps;
private ClientProperties client;
private ApplicationRegistrator registrator;
private RestTemplate restTemplate;
private HttpHeaders headers;
......@@ -53,8 +53,8 @@ public class ApplicationRegistratorTest {
public void setup() {
restTemplate = mock(RestTemplate.class);
adminProps = new AdminProperties();
adminProps.setUrl(new String[] { "http://sba:8080", "http://sba2:8080" });
client = new ClientProperties();
client.setUrl(new String[] { "http://sba:8080", "http://sba2:8080" });
ApplicationFactory factory = mock(ApplicationFactory.class);
when(factory.createApplication()).thenReturn(Application.create("AppName")
......@@ -63,7 +63,7 @@ public class ApplicationRegistratorTest {
.withServiceUrl("http://localhost:8080")
.build());
registrator = new ApplicationRegistrator(restTemplate, adminProps, factory);
registrator = new ApplicationRegistrator(restTemplate, client, factory);
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
......@@ -127,7 +127,7 @@ public class ApplicationRegistratorTest {
@SuppressWarnings("rawtypes")
@Test
public void register_multiple() {
adminProps.setRegisterOnce(false);
client.setRegisterOnce(false);
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
.thenReturn(new ResponseEntity<Map>(Collections.singletonMap("id", "-id-"),
......@@ -156,7 +156,7 @@ public class ApplicationRegistratorTest {
@SuppressWarnings("rawtypes")
@Test
public void register_multiple_one_failure() {
adminProps.setRegisterOnce(false);
client.setRegisterOnce(false);
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
.thenReturn(new ResponseEntity<Map>(Collections.singletonMap("id", "-id-"),
......@@ -185,7 +185,7 @@ public class ApplicationRegistratorTest {
@Test
public void register_multiple_all_failures() {
adminProps.setRegisterOnce(false);
client.setRegisterOnce(false);
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
.thenThrow(new RestClientException("Error"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment