Updates to use updated ConfigData apis

This commit is contained in:
spencergibb
2020-10-18 11:52:53 -04:00
parent 7f26710d2d
commit b8cc08f74c
7 changed files with 53 additions and 37 deletions

View File

@@ -23,20 +23,20 @@ import org.apache.curator.framework.CuratorFramework;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoader;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
public class ZookeeperConfigDataLoader implements ConfigDataLoader<ZookeeperConfigDataLocation> {
public class ZookeeperConfigDataLoader implements ConfigDataLoader<ZookeeperConfigDataResource> {
@Override
public ConfigData load(ConfigDataLoaderContext context, ZookeeperConfigDataLocation location) {
public ConfigData load(ConfigDataLoaderContext context, ZookeeperConfigDataResource resource) {
try {
CuratorFramework curator = context.getBootstrapContext().get(CuratorFramework.class);
ZookeeperPropertySource propertySource = new ZookeeperPropertySource(location.getContext(),
ZookeeperPropertySource propertySource = new ZookeeperPropertySource(resource.getContext(),
curator);
return new ConfigData(Collections.singletonList(propertySource));
}
catch (Exception e) {
throw new ConfigDataLocationNotFoundException(location, e);
throw new ConfigDataResourceNotFoundException(resource, e);
}
}

View File

@@ -25,6 +25,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
@@ -40,7 +41,7 @@ import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationResolver<ZookeeperConfigDataLocation> {
public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationResolver<ZookeeperConfigDataResource> {
/**
* Zookeeper Config Data prefix.
@@ -54,8 +55,8 @@ public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationRe
}
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, String location) {
if (!location.startsWith(PREFIX)) {
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
if (!location.hasPrefix(PREFIX)) {
return false;
}
// only bind on correct prefix
@@ -67,18 +68,18 @@ public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationRe
}
@Override
public List<ZookeeperConfigDataLocation> resolve(ConfigDataLocationResolverContext context, String location,
boolean optional) throws ConfigDataLocationNotFoundException {
public List<ZookeeperConfigDataResource> resolve(ConfigDataLocationResolverContext context, ConfigDataLocation location)
throws ConfigDataLocationNotFoundException {
return Collections.emptyList();
}
@Override
public List<ZookeeperConfigDataLocation> resolveProfileSpecific(ConfigDataLocationResolverContext context,
String location, boolean optional, Profiles profiles) throws ConfigDataLocationNotFoundException {
public List<ZookeeperConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext context,
ConfigDataLocation location, Profiles profiles) throws ConfigDataLocationNotFoundException {
UriComponents locationUri = parseLocation(location);
// create curator
CuratorFactory.registerCurator(context.getBootstrapContext(), locationUri, optional);
CuratorFactory.registerCurator(context.getBootstrapContext(), locationUri, location.isOptional());
// create locations
ZookeeperConfigProperties properties = loadConfigProperties(context.getBinder());
@@ -97,9 +98,9 @@ public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationRe
event.getApplicationContext().getEnvironment().getPropertySources().addFirst(propertySource);
});
ArrayList<ZookeeperConfigDataLocation> locations = new ArrayList<>();
ArrayList<ZookeeperConfigDataResource> locations = new ArrayList<>();
contexts.forEach(propertySourceContext -> locations
.add(new ZookeeperConfigDataLocation(propertySourceContext, optional)));
.add(new ZookeeperConfigDataResource(propertySourceContext, location.isOptional())));
return locations;
}
@@ -113,16 +114,17 @@ public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationRe
}
@Nullable
protected UriComponents parseLocation(String location) {
String uri = location.substring(PREFIX.length());
if (!StringUtils.hasText(uri)) {
protected UriComponents parseLocation(ConfigDataLocation location) {
String originalUri = location.getNonPrefixedValue(PREFIX);
if (!StringUtils.hasText(originalUri)) {
return null;
}
if (!uri.startsWith("//")) {
uri = PREFIX + "//" + uri;
String uri;
if (!originalUri.startsWith("//")) {
uri = PREFIX + "//" + originalUri;
}
else {
uri = location;
uri = originalUri;
}
return UriComponentsBuilder.fromUriString(uri).build();
}

View File

@@ -18,15 +18,15 @@ package org.springframework.cloud.zookeeper.config;
import java.util.Objects;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.core.style.ToStringCreator;
public class ZookeeperConfigDataLocation extends ConfigDataLocation {
public class ZookeeperConfigDataResource extends ConfigDataResource {
private final String context;
private final boolean optional;
public ZookeeperConfigDataLocation(String context, boolean optional) {
public ZookeeperConfigDataResource(String context, boolean optional) {
this.context = context;
this.optional = optional;
}
@@ -47,7 +47,7 @@ public class ZookeeperConfigDataLocation extends ConfigDataLocation {
if (o == null || getClass() != o.getClass()) {
return false;
}
ZookeeperConfigDataLocation that = (ZookeeperConfigDataLocation) o;
ZookeeperConfigDataResource that = (ZookeeperConfigDataResource) o;
return this.optional == that.optional &&
this.context.equals(that.context);
}

View File

@@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.Binder;
@@ -40,11 +41,11 @@ public class ZookeeperConfigDataLocationResolverTests {
public void testParseLocation() {
ZookeeperConfigDataLocationResolver resolver = new ZookeeperConfigDataLocationResolver(LogFactory.getLog(getClass()));
UriComponents uriComponents = resolver.parseLocation(
"zookeeper:myhost:2182/mypath1;/mypath2;/mypath3");
ConfigDataLocation.of("zookeeper:myhost:2182/mypath1;/mypath2;/mypath3"));
assertThat(uriComponents.toUri()).hasScheme("zookeeper").hasHost("myhost")
.hasPort(2182).hasPath("/mypath1;/mypath2;/mypath3");
uriComponents = resolver.parseLocation("zookeeper:myhost:2182");
uriComponents = resolver.parseLocation(ConfigDataLocation.of("zookeeper:myhost:2182"));
assertThat(uriComponents.toUri()).hasScheme("zookeeper").hasHost("myhost")
.hasPort(2182).hasPath("");
}
@@ -52,7 +53,7 @@ public class ZookeeperConfigDataLocationResolverTests {
@Test
public void testResolveProfileSpecificWithCustomPaths() {
String location = "zookeeper:myhost:2182/mypath1;/mypath2;/mypath3";
List<ZookeeperConfigDataLocation> locations = testResolveProfileSpecific(location);
List<ZookeeperConfigDataResource> locations = testResolveProfileSpecific(location);
assertThat(locations).hasSize(3);
assertThat(toContexts(locations)).containsExactly("/mypath1", "/mypath2",
"/mypath3");
@@ -61,18 +62,18 @@ public class ZookeeperConfigDataLocationResolverTests {
@Test
public void testResolveProfileSpecificWithAutomaticPaths() {
String location = "zookeeper:myhost:1234";
List<ZookeeperConfigDataLocation> locations = testResolveProfileSpecific(location);
List<ZookeeperConfigDataResource> locations = testResolveProfileSpecific(location);
assertThat(locations).hasSize(4);
assertThat(toContexts(locations)).containsExactly("config/testapp,dev",
"config/testapp", "config/application,dev", "config/application");
}
private List<String> toContexts(List<ZookeeperConfigDataLocation> locations) {
return locations.stream().map(ZookeeperConfigDataLocation::getContext)
private List<String> toContexts(List<ZookeeperConfigDataResource> locations) {
return locations.stream().map(ZookeeperConfigDataResource::getContext)
.collect(Collectors.toList());
}
private List<ZookeeperConfigDataLocation> testResolveProfileSpecific(String location) {
private List<ZookeeperConfigDataResource> testResolveProfileSpecific(String location) {
ZookeeperConfigDataLocationResolver resolver = createResolver();
MockEnvironment env = new MockEnvironment();
@@ -89,7 +90,7 @@ public class ZookeeperConfigDataLocationResolverTests {
Profiles profiles = mock(Profiles.class);
when(profiles.getAccepted()).thenReturn(Collections.singletonList("dev"));
return resolver.resolveProfileSpecific(context, location, false, profiles);
return resolver.resolveProfileSpecific(context, ConfigDataLocation.of(location), profiles);
}
private ZookeeperConfigDataLocationResolver createResolver() {

View File

@@ -22,7 +22,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.cloud.context.refresh.ConfigDataContextRefresher;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
@@ -57,7 +57,7 @@ public class ZookeeperConfigDataNotOptionalIntegrationTests {
context.close();
}
}
}).isInstanceOf(ConfigDataLocationNotFoundException.class);
}).isInstanceOf(ConfigDataResourceNotFoundException.class);
}
@Configuration

View File

@@ -30,7 +30,7 @@ import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataException;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.util.StringUtils;
@@ -136,7 +136,7 @@ public abstract class CuratorFactory {
catch (Exception e) {
if (!optional) {
log.error("Unable to connect to zookeeper", e);
throw new ConfigDataLocationNotFoundException("Unable to connect to zookeeper", null, e);
throw new ZookeeperConnectException("Unable to connect to zookeeper", e);
}
if (log.isDebugEnabled()) {
log.debug("Unable to connect to zookeeper", e);
@@ -156,4 +156,16 @@ public abstract class CuratorFactory {
}
}
private static class ZookeeperConnectException extends ConfigDataException {
/**
* Create a new {@link ConfigDataException} instance.
* @param message the exception message
* @param cause the exception cause
*/
protected ZookeeperConnectException(String message, Throwable cause) {
super(message, cause);
}
}
}

View File

@@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;