Merge branch '3.1.x'

This commit is contained in:
Ryan Baxter
2022-06-03 16:31:32 -04:00
5 changed files with 53 additions and 7 deletions

View File

@@ -17,6 +17,8 @@
<main.basedir>${basedir}/..</main.basedir>
<configprops.inclusionPattern>spring.cloud.consul.*</configprops.inclusionPattern>
<upload-docs-zip.phase>deploy</upload-docs-zip.phase>
<!-- Don't upload docs jar to central / repo.spring.io -->
<maven-deploy-plugin-default.phase>none</maven-deploy-plugin-default.phase>
</properties>
<dependencies>
<dependency>

View File

@@ -4,7 +4,7 @@ set -o errexit
mkdir -p target
SCRIPT_URL="https://raw.githubusercontent.com/spring-cloud-samples/brewery/master/runAcceptanceTests.sh"
SCRIPT_URL="https://raw.githubusercontent.com/spring-cloud-samples/brewery/2021.0.x/runAcceptanceTests.sh"
AT_WHAT_TO_TEST="CONSUL"
cd target

View File

@@ -36,7 +36,12 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ConditionalOnProperty(value = "spring.cloud.consul.discovery.enabled", matchIfMissing = true)
@ConditionalOnProperty(value = ConditionalOnConsulDiscoveryEnabled.PROPERTY, matchIfMissing = true)
public @interface ConditionalOnConsulDiscoveryEnabled {
/**
* Property key.
*/
String PROPERTY = "spring.cloud.consul.discovery.enabled";
}

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.consul.discovery.configclient;
import java.util.Collections;
import com.ecwid.consul.v1.ConsulClient;
import org.springframework.boot.BootstrapRegistry;
@@ -29,6 +31,7 @@ import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
import org.springframework.cloud.consul.ConsulAutoConfiguration;
import org.springframework.cloud.consul.ConsulProperties;
import org.springframework.cloud.consul.discovery.ConditionalOnConsulDiscoveryEnabled;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.util.ClassUtils;
@@ -83,7 +86,7 @@ public class ConsulConfigServerBootstrapper implements BootstrapRegistryInitiali
});
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
if (!isDiscoveryEnabled(context.get(Binder.class))) {
return null;
return (id) -> Collections.emptyList();
}
ConsulDiscoveryClient discoveryClient = context.get(ConsulDiscoveryClient.class);
return discoveryClient::getInstances;
@@ -96,7 +99,9 @@ public class ConsulConfigServerBootstrapper implements BootstrapRegistryInitiali
}
private boolean isDiscoveryEnabled(Binder binder) {
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false);
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false)
&& binder.bind(ConditionalOnConsulDiscoveryEnabled.PROPERTY, Boolean.class).orElse(true)
&& binder.bind("spring.cloud.discovery.enabled", Boolean.class).orElse(true);
}
}

View File

@@ -16,8 +16,10 @@
package org.springframework.cloud.consul.discovery.configclient;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import com.ecwid.consul.transport.TransportException;
import org.junit.jupiter.api.Test;
import org.springframework.boot.BootstrapRegistry;
@@ -34,6 +36,7 @@ import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class ConsulConfigServerBootstrapperTests {
@@ -44,11 +47,40 @@ public class ConsulConfigServerBootstrapperTests {
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
.get(ConfigServerInstanceProvider.Function.class);
assertThat(providerFn).as("ConfigServerInstanceProvider.Function was created when it shouldn't")
.isNull();
assertThat(providerFn.apply("id"))
.as("ConfigServerInstanceProvider.Function should return empty list")
.isEqualTo(Collections.EMPTY_LIST);
})).run().close();
}
@Test
public void consulDiscoveryClientNotEnabledProvidesEmptyList() {
new SpringApplicationBuilder(TestConfig.class)
.properties("--server.port=0", "spring.cloud.service-registry.auto-registration.enabled=false",
"spring.cloud.config.discovery.enabled=true", "spring.cloud.consul.discovery.enabled=false")
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
.get(ConfigServerInstanceProvider.Function.class);
assertThat(providerFn.apply("id"))
.as("ConfigServerInstanceProvider.Function should return empty list")
.isEqualTo(Collections.EMPTY_LIST);
})).run().close();
}
@Test
public void springCloudDiscoveryClientNotEnabledProvidesEmptyList() {
new SpringApplicationBuilder(TestConfig.class)
.properties("--server.port=0", "spring.cloud.service-registry.auto-registration.enabled=false",
"spring.cloud.config.discovery.enabled=true", "spring.cloud.discovery.enabled=false")
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
.get(ConfigServerInstanceProvider.Function.class);
assertThat(providerFn.apply("id"))
.as("ConfigServerInstanceProvider.Function should return empty list")
.isEqualTo(Collections.EMPTY_LIST);
})).run().close();
}
@Test
public void enabledAddsInstanceProviderFn() {
AtomicReference<ConsulDiscoveryClient> bootstrapDiscoveryClient = new AtomicReference<>();
@@ -63,7 +95,9 @@ public class ConsulConfigServerBootstrapperTests {
bootstrapDiscoveryClient.set(event.getBootstrapContext().get(ConsulDiscoveryClient.class));
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
.get(ConfigServerInstanceProvider.Function.class);
assertThat(providerFn).as("ConfigServerInstanceProvider.Function was not created when it should.")
assertThatThrownBy(() -> providerFn.apply("id")).isInstanceOf(TransportException.class)
.hasMessageContaining("org.apache.http.conn.HttpHostConnectException: Connect to localhost:8500")
.as("Should have tried to reach out to Consul to get config server instance")
.isNotNull();
})).run();
ConsulDiscoveryClient discoveryClient = context.getBean(ConsulDiscoveryClient.class);