Remove function and use PropertyResolver (#4240)

This relates to https://github.com/spring-cloud/spring-cloud-config/pull/2375

And reverts part of #4175

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2024-01-23 20:39:14 -05:00
committed by Ryan Baxter
parent 12eed88057
commit ab8a02b9bf
2 changed files with 37 additions and 44 deletions

View File

@@ -17,18 +17,16 @@
package org.springframework.cloud.netflix.eureka.config;
import java.util.Collections;
import java.util.List;
import com.netflix.discovery.shared.transport.EurekaHttpClient;
import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PropertyResolver;
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
import org.springframework.cloud.configuration.TlsProperties;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
@@ -45,52 +43,39 @@ public class EurekaConfigServerBootstrapper implements BootstrapRegistryInitiali
return;
}
// It is important that we pass a lambda for the Function or else we will get a
// ClassNotFoundException when config is not on the classpath
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, EurekaFunction::create);
}
private static Boolean getDiscoveryEnabled(Binder binder) {
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false)
&& binder.bind("eureka.client.enabled", Boolean.class).orElse(true)
&& binder.bind("spring.cloud.discovery.enabled", Boolean.class).orElse(true);
}
final static class EurekaFunction implements ConfigServerInstanceProvider.Function {
private final BootstrapContext context;
static EurekaFunction create(BootstrapContext context) {
return new EurekaFunction(context);
}
private EurekaFunction(BootstrapContext context) {
this.context = context;
}
@Override
public List<ServiceInstance> apply(String serviceId, Binder binder, BindHandler bindHandler, Log log) {
if (binder == null || !getDiscoveryEnabled(binder)) {
return Collections.emptyList();
registry.registerIfAbsent(EurekaClientConfigBean.class, context -> {
if (!getDiscoveryEnabled(context)) {
return null;
}
PropertyResolver propertyResolver = getPropertyResolver(context);
return propertyResolver.resolveConfigurationProperties(EurekaClientConfigBean.PREFIX,
EurekaClientConfigBean.class, EurekaClientConfigBean::new);
});
EurekaClientConfigBean config = binder.bind(EurekaClientConfigBean.PREFIX, EurekaClientConfigBean.class)
.orElseGet(EurekaClientConfigBean::new);
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
if (!getDiscoveryEnabled(context)) {
return (id) -> Collections.emptyList();
}
EurekaClientConfigBean config = context.get(EurekaClientConfigBean.class);
EurekaHttpClient httpClient = new RestTemplateTransportClientFactory(
context.getOrElse(TlsProperties.class, null),
context.getOrElse(EurekaClientHttpRequestFactorySupplier.class,
new DefaultEurekaClientHttpRequestFactorySupplier()))
.newClient(HostnameBasedUrlRandomizer.randomEndpoint(config, binder));
return new EurekaConfigServerInstanceProvider(httpClient, config).getInstances(serviceId);
}
new DefaultEurekaClientHttpRequestFactorySupplier())).newClient(
HostnameBasedUrlRandomizer.randomEndpoint(config, getPropertyResolver(context)));
return new EurekaConfigServerInstanceProvider(httpClient, config)::getInstances;
});
}
@Override
public List<ServiceInstance> apply(String serviceId) {
// This should never be called now but is here for backward
// compatibility
return apply(serviceId, null, null, null);
}
private static PropertyResolver getPropertyResolver(BootstrapContext context) {
return context.getOrElseSupply(PropertyResolver.class,
() -> new PropertyResolver(context.get(Binder.class), context.getOrElse(BindHandler.class, null)));
}
public static Boolean getDiscoveryEnabled(BootstrapContext bootstrapContext) {
PropertyResolver propertyResolver = getPropertyResolver(bootstrapContext);
return propertyResolver.get(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class, false)
&& propertyResolver.get("eureka.client.enabled", Boolean.class, true)
&& propertyResolver.get("spring.cloud.discovery.enabled", Boolean.class, true);
}
}

View File

@@ -23,12 +23,15 @@ import com.netflix.discovery.endpoint.EndpointUtils;
import com.netflix.discovery.shared.resolver.DefaultEndpoint;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PropertyResolver;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
public final class HostnameBasedUrlRandomizer implements EndpointUtils.ServiceUrlRandomizer {
private static final String EUREKA_INSTANCE_HOSTNAME = "eureka.instance.hostname";
private final String hostname;
HostnameBasedUrlRandomizer(String hostname) {
@@ -64,12 +67,17 @@ public final class HostnameBasedUrlRandomizer implements EndpointUtils.ServiceUr
}
public static DefaultEndpoint randomEndpoint(EurekaClientConfig config, Environment env) {
String hostname = env.getProperty("eureka.instance.hostname");
String hostname = env.getProperty(EUREKA_INSTANCE_HOSTNAME);
return new DefaultEndpoint(getEurekaUrl(config, hostname));
}
public static DefaultEndpoint randomEndpoint(EurekaClientConfig config, Binder binder) {
String hostname = binder.bind("eureka.instance.hostname", String.class).orElseGet(() -> null);
String hostname = binder.bind(EUREKA_INSTANCE_HOSTNAME, String.class).orElseGet(() -> null);
return new DefaultEndpoint(getEurekaUrl(config, hostname));
}
public static DefaultEndpoint randomEndpoint(EurekaClientConfig config, PropertyResolver propertyResolver) {
String hostname = propertyResolver.get(EUREKA_INSTANCE_HOSTNAME, String.class, null);
return new DefaultEndpoint(getEurekaUrl(config, hostname));
}