Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove function and use PropertyResolver #4240

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,52 +43,39 @@ public void initialize(BootstrapRegistry registry) {
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;
}

EurekaClientConfigBean config = binder.bind(EurekaClientConfigBean.PREFIX, EurekaClientConfigBean.class)
.orElseGet(EurekaClientConfigBean::new);
PropertyResolver propertyResolver = getPropertyResolver(context);
return propertyResolver.resolveConfigurationProperties(EurekaClientConfigBean.PREFIX,
EurekaClientConfigBean.class, 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) {
Copy link

@pantherdd pantherdd Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary auto-boxing to Boolean (and I now realized, that's why this implementation has a method named get* instead of is*).

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
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) {
Expand Down Expand Up @@ -64,12 +67,17 @@ public static String getEurekaUrl(EurekaClientConfig config, String hostname) {
}

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));
}

Expand Down