Skip to content

Commit

Permalink
Fix EurekaConfigServerBootstrapConfiguration.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaMaciaszek committed Nov 4, 2024
1 parent f27b48b commit 10e1edf
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.springframework.cloud.netflix.eureka;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author Olga Maciaszek-Sharma
*/
@ConfigurationProperties("eureka.client.restclient.timeout")
public class RestClientTimeoutProperties extends TimeoutProperties {

@Override
public String toString() {
return "RestClientTimeoutProperties{" + ", connectTimeout=" + connectTimeout + ", connectRequestTimeout="
+ connectRequestTimeout + ", socketTimeout=" + socketTimeout + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,66 +34,11 @@
* @since 3.1.6
*/
@ConfigurationProperties("eureka.client.rest-template-timeout")
public class RestTemplateTimeoutProperties {

/**
* Default values are set to 180000, in keeping with {@link RequestConfig} and
* {@link SocketConfig} defaults.
*/
private int connectTimeout = 180000; // 3 * MINUTES

private int connectRequestTimeout = 180000; // 3 * MINUTES

private int socketTimeout = 180000; // 3 * MINUTES

public int getConnectTimeout() {
return connectTimeout;
}

public int getConnectRequestTimeout() {
return connectRequestTimeout;
}

public int getSocketTimeout() {
return socketTimeout;
}

public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}

public void setConnectRequestTimeout(int connectRequestTimeout) {
this.connectRequestTimeout = connectRequestTimeout;
}

public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

RestTemplateTimeoutProperties that = (RestTemplateTimeoutProperties) o;

return connectTimeout == that.connectTimeout && connectRequestTimeout == that.connectRequestTimeout
&& socketTimeout == that.socketTimeout;
}

@Override
public int hashCode() {
return Objects.hash(connectTimeout, connectRequestTimeout, socketTimeout);
}
public class RestTemplateTimeoutProperties extends TimeoutProperties {

@Override
public String toString() {
return "RestTemplateTimeoutProperties{" + ", connectTimeout=" + connectTimeout + ", connectRequestTimeout="
+ connectRequestTimeout + ", socketTimeout=" + socketTimeout + '}';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.springframework.cloud.netflix.eureka;

import java.util.Objects;

import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.core5.http.io.SocketConfig;

/**
* @author Olga Maciaszek-Sharma
*/
public class TimeoutProperties {


/**
* Default values are set to 180000, in keeping with {@link RequestConfig} and
* {@link SocketConfig} defaults.
*/
protected int connectTimeout = 180000; // 3 * MINUTES

protected int connectRequestTimeout = 180000; // 3 * MINUTES

protected int socketTimeout = 180000; // 3 * MINUTES

public int getConnectTimeout() {
return connectTimeout;
}

public int getConnectRequestTimeout() {
return connectRequestTimeout;
}

public int getSocketTimeout() {
return socketTimeout;
}

public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}

public void setConnectRequestTimeout(int connectRequestTimeout) {
this.connectRequestTimeout = connectRequestTimeout;
}

public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

RestTemplateTimeoutProperties that = (RestTemplateTimeoutProperties) o;

return connectTimeout == that.connectTimeout && connectRequestTimeout == that.connectRequestTimeout
&& socketTimeout == that.socketTimeout;
}

@Override
public int hashCode() {
return Objects.hash(connectTimeout, connectRequestTimeout, socketTimeout);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.configuration.SSLContextFactory;
import org.springframework.cloud.configuration.TlsProperties;
import org.springframework.cloud.netflix.eureka.RestClientTimeoutProperties;
import org.springframework.cloud.netflix.eureka.RestTemplateTimeoutProperties;
import org.springframework.cloud.netflix.eureka.http.DefaultEurekaClientHttpRequestFactorySupplier;
import org.springframework.cloud.netflix.eureka.http.EurekaClientHttpRequestFactorySupplier;
Expand All @@ -60,7 +61,7 @@
* @author Wonchul Heo
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(RestTemplateTimeoutProperties.class)
@EnableConfigurationProperties({RestTemplateTimeoutProperties.class, RestClientTimeoutProperties.class})
public class DiscoveryClientOptionalArgsConfiguration {

protected static final Log logger = LogFactory.getLog(DiscoveryClientOptionalArgsConfiguration.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration;
import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.cloud.configuration.TlsProperties;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.cloud.netflix.eureka.RestClientTimeoutProperties;
import org.springframework.cloud.netflix.eureka.RestTemplateTimeoutProperties;
import org.springframework.cloud.netflix.eureka.http.DefaultEurekaClientHttpRequestFactorySupplier;
import org.springframework.cloud.netflix.eureka.http.EurekaClientHttpRequestFactorySupplier;
Expand Down Expand Up @@ -63,38 +63,66 @@
@ConditionalOnClass(ConfigServicePropertySourceLocator.class)
@Conditional(EurekaConfigServerBootstrapConfiguration.EurekaConfigServerBootstrapCondition.class)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(RestTemplateTimeoutProperties.class)
public class EurekaConfigServerBootstrapConfiguration {

@Bean
@ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean() {
return new EurekaClientConfigBean();
}

@Bean
@ConditionalOnMissingBean(EurekaHttpClient.class)
@Conditional(RestTemplateEnabledCondition.class)
public RestTemplateEurekaHttpClient configDiscoveryRestTemplateEurekaHttpClient(EurekaClientConfigBean config,
Environment env, @Nullable TlsProperties properties,
EurekaClientHttpRequestFactorySupplier eurekaClientHttpRequestFactorySupplier,
ObjectProvider<RestTemplateBuilder> restTemplateBuilders) {
return (RestTemplateEurekaHttpClient) new RestTemplateTransportClientFactory(properties,
eurekaClientHttpRequestFactorySupplier, restTemplateBuilders::getIfAvailable)
.newClient(HostnameBasedUrlRandomizer.randomEndpoint(config, env));
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
@Conditional(RestTemplateConfiguration.RestTemplateEnabledCondition.class)
@EnableConfigurationProperties(RestTemplateTimeoutProperties.class)
static class RestTemplateConfiguration {

@Bean
@ConditionalOnMissingBean
EurekaClientHttpRequestFactorySupplier defaultEurekaClientHttpRequestFactorySupplier(
RestTemplateTimeoutProperties restTemplateTimeoutProperties) {
return new DefaultEurekaClientHttpRequestFactorySupplier(restTemplateTimeoutProperties);
}
@Bean
@ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean() {
return new EurekaClientConfigBean();
}

@Bean
@ConditionalOnMissingBean(EurekaHttpClient.class)
@Conditional(RestTemplateEnabledCondition.class)
public RestTemplateEurekaHttpClient configDiscoveryRestTemplateEurekaHttpClient(EurekaClientConfigBean config,
Environment env, @Nullable TlsProperties properties,
EurekaClientHttpRequestFactorySupplier eurekaClientHttpRequestFactorySupplier,
ObjectProvider<RestTemplateBuilder> restTemplateBuilders) {
return (RestTemplateEurekaHttpClient) new RestTemplateTransportClientFactory(properties,
eurekaClientHttpRequestFactorySupplier, restTemplateBuilders::getIfAvailable)
.newClient(HostnameBasedUrlRandomizer.randomEndpoint(config, env));
}

@Bean
@ConditionalOnMissingBean
@Conditional(RestTemplateEnabledCondition.class)
EurekaClientHttpRequestFactorySupplier defaultEurekaClientHttpRequestFactorySupplier(
RestTemplateTimeoutProperties restTemplateTimeoutProperties) {
return new DefaultEurekaClientHttpRequestFactorySupplier(restTemplateTimeoutProperties);
}

@Bean
public ConfigServerInstanceProvider.Function eurekaConfigServerInstanceProvider(EurekaHttpClient client,
EurekaClientConfig config) {
return new EurekaConfigServerInstanceProvider(client, config)::getInstances;
}

static class RestTemplateEnabledCondition extends AllNestedConditions {

RestTemplateEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}

@ConditionalOnProperty(prefix = "eureka.client", name = "webclient.enabled", matchIfMissing = true,
havingValue = "false")
static class OnWebClientDisabled {

}

@ConditionalOnProperty(prefix = "eureka.client", name = "restclient.enabled", havingValue = "false")
static class OnRestClientDisabled {

}

}

@Bean
public ConfigServerInstanceProvider.Function eurekaConfigServerInstanceProvider(EurekaHttpClient client,
EurekaClientConfig config) {
return new EurekaConfigServerInstanceProvider(client, config)::getInstances;
}

@Configuration(proxyBeanMethods = false)
Expand All @@ -115,54 +143,53 @@ public WebClientEurekaHttpClient configDiscoveryWebClientEurekaHttpClient(Eureka

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.web.client.RestClient")
@Conditional(RestClientEnabledCondition.class)
@ImportAutoConfiguration(RestClientAutoConfiguration.class)
protected static class RestClientConfiguration {
@Conditional(RestClientConfiguration.RestClientEnabledCondition.class)
@EnableConfigurationProperties(RestClientTimeoutProperties.class)
static class RestClientConfiguration {

@Bean
@ConditionalOnMissingBean(EurekaHttpClient.class)
public RestClientEurekaHttpClient configDiscoveryRestClientEurekaHttpClient(EurekaClientConfigBean config,
@Nullable TlsProperties properties,
EurekaClientHttpRequestFactorySupplier eurekaClientHttpRequestFactorySupplier,
ObjectProvider<RestClient.Builder> builder, Environment env) {
return (RestClientEurekaHttpClient) new RestClientTransportClientFactory(builder::getIfAvailable)
return (RestClientEurekaHttpClient) new RestClientTransportClientFactory(properties,
eurekaClientHttpRequestFactorySupplier,
builder::getIfAvailable)
.newClient(HostnameBasedUrlRandomizer.randomEndpoint(config, env));
}

}

static class RestTemplateEnabledCondition extends AllNestedConditions {

RestTemplateEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
@Bean
@ConditionalOnMissingBean
EurekaClientHttpRequestFactorySupplier defaultEurekaClientHttpRequestFactorySupplier(
RestClientTimeoutProperties restClientTimeoutProperties) {
return new DefaultEurekaClientHttpRequestFactorySupplier(restClientTimeoutProperties);
}

@ConditionalOnProperty(prefix = "eureka.client", name = "webclient.enabled", matchIfMissing = true,
havingValue = "false")
static class OnWebClientDisabled {

@Bean
@ConditionalOnMissingBean(value = EurekaClientConfig.class, search = SearchStrategy.CURRENT)
public EurekaClientConfigBean eurekaClientConfigBean() {
return new EurekaClientConfigBean();
}

@ConditionalOnProperty(prefix = "eureka.client", name = "restclient.enabled", havingValue = "false")
static class OnRestClientDisabled {

}
static class RestClientEnabledCondition extends AllNestedConditions {

}
RestClientEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}

static class RestClientEnabledCondition extends AllNestedConditions {
@ConditionalOnProperty(prefix = "eureka.client", name = "webclient.enabled", matchIfMissing = true,
havingValue = "false")
static class OnWebClientDisabled {

RestClientEnabledCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
}

@ConditionalOnProperty(prefix = "eureka.client", name = "webclient.enabled", matchIfMissing = true,
havingValue = "false")
static class OnWebClientDisabled {

}
@ConditionalOnProperty(prefix = "eureka.client", name = "restclient.enabled", matchIfMissing = true,
havingValue = "true")
static class OnRestClientEnabled {

@ConditionalOnProperty(prefix = "eureka.client", name = "restclient.enabled", matchIfMissing = true,
havingValue = "true")
static class OnRestClientEnabled {
}

}

Expand Down
Loading

0 comments on commit 10e1edf

Please sign in to comment.