Commit 34d08e58 by Johannes Edmeier

CloudPlatform aware default for s.b.a.client.auto-deregistration

When running on any CloudPlatform the auto-deregistration defaults to true otherwise the behaviour is unchanged. closes #658
parent 28309bec
......@@ -28,7 +28,7 @@
<properties>
<revision>2.0.0-SNAPSHOT</revision>
<spring-boot.version>2.0.0.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.M7</spring-cloud.version>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
<hazelcast-tests.version>3.9.2</hazelcast-tests.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
......
......@@ -18,8 +18,11 @@ package de.codecentric.boot.admin.client.config;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
@lombok.Data
@ConfigurationProperties(prefix = "spring.boot.admin.client")
......@@ -65,11 +68,12 @@ public class ClientProperties {
/**
* Enable automatic deregistration on shutdown
* If not set it defaults to true if a active {@link CloudPlatform} is present;
*/
private boolean autoDeregistration;
private Boolean autoDeregistration = null;
/**
* Enable automatic registration when the application is ready
* Enable automatic registration when the application is ready.
*/
private boolean autoRegistration = true;
......@@ -83,6 +87,14 @@ public class ClientProperties {
*/
private boolean enabled = true;
private final Environment environment;
public ClientProperties(Environment environment) {
Assert.notNull(environment, "Environment must not be null");
this.environment = environment;
}
public String[] getAdminUrl() {
String[] adminUrls = url.clone();
for (int i = 0; i < adminUrls.length; i++) {
......@@ -90,4 +102,8 @@ public class ClientProperties {
}
return adminUrls;
}
public boolean isAutoDeregistration() {
return this.autoDeregistration != null ? autoDeregistration : CloudPlatform.getActive(environment) != null;
}
}
......@@ -58,7 +58,7 @@ public class SpringBootAdminClientEnabledCondition extends SpringBootCondition {
private ClientProperties getClientProperties(ConditionContext context) {
Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources.get(context.getEnvironment());
ClientProperties clientProperties = new ClientProperties();
ClientProperties clientProperties = new ClientProperties(context.getEnvironment());
new Binder(sources).bind("spring.boot.admin.client", Bindable.ofInstance(clientProperties));
return clientProperties;
}
......
/*
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.client.config;
import org.junit.Test;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
public class ClientPropertiesTest {
@Test
public void should_default_autoDeregister_to_false() {
MockEnvironment env = new MockEnvironment();
ClientProperties clientProperties = new ClientProperties(env);
assertThat(clientProperties.isAutoDeregistration()).isFalse();
clientProperties.setAutoDeregistration(false);
assertThat(clientProperties.isAutoDeregistration()).isFalse();
clientProperties.setAutoDeregistration(true);
assertThat(clientProperties.isAutoDeregistration()).isTrue();
}
@Test
public void should_default_autoDeregister_to_true() {
MockEnvironment env = new MockEnvironment();
env.setProperty("VCAP_APPLICATION", "");
ClientProperties clientProperties = new ClientProperties(env);
assertThat(clientProperties.isAutoDeregistration()).isTrue();
clientProperties.setAutoDeregistration(false);
assertThat(clientProperties.isAutoDeregistration()).isFalse();
clientProperties.setAutoDeregistration(true);
assertThat(clientProperties.isAutoDeregistration()).isTrue();
}
@Test
public void should_return_all_admiUrls() {
ClientProperties clientProperties = new ClientProperties(new MockEnvironment());
clientProperties.setApiPath("register");
clientProperties.setUrl(new String[]{"http://first", "http://second"});
assertThat(clientProperties.getAdminUrl()).containsExactly("http://first/register", "http://second/register");
}
}
......@@ -29,6 +29,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
......@@ -52,7 +53,7 @@ public class ApplicationRegistratorTest {
public void setup() {
restTemplate = mock(RestTemplate.class);
client = new ClientProperties();
client = new ClientProperties(new MockEnvironment());
client.setUrl(new String[]{"http://sba:8080", "http://sba2:8080"});
ApplicationFactory factory = mock(ApplicationFactory.class);
......
......@@ -86,8 +86,8 @@ spring.boot.admin.client.password
| `true`
| spring.boot.admin.client.auto-deregistration
| Switch to enable auto-deregistration at Spring Boot Admin server when context is closed.
| `false`
| Switch to enable auto-deregistration at Spring Boot Admin server when context is closed. If the value is unset the feature is active if a running CloudPlatform was detected.
| `null`
| 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.
......
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