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

Support Spring RestClient as TransportClientFactory #4281

Merged
merged 23 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -25,7 +25,6 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand Down Expand Up @@ -73,10 +72,8 @@ public TlsProperties tlsProperties() {

@Bean
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
@Conditional(JerseyClientNotPresentOrNotEnabledCondition.class)
heowc marked this conversation as resolved.
Show resolved Hide resolved
@Conditional(RestTemplateEnabledCondition.class)
@ConditionalOnMissingBean(value = { AbstractDiscoveryClientOptionalArgs.class }, search = SearchStrategy.CURRENT)
@ConditionalOnProperty(prefix = "eureka.client", name = "webclient.enabled", matchIfMissing = true,
heowc marked this conversation as resolved.
Show resolved Hide resolved
heowc marked this conversation as resolved.
Show resolved Hide resolved
havingValue = "false")
public RestTemplateDiscoveryClientOptionalArgs restTemplateDiscoveryClientOptionalArgs(TlsProperties tlsProperties,
EurekaClientHttpRequestFactorySupplier eurekaClientHttpRequestFactorySupplier,
ObjectProvider<RestTemplateBuilder> restTemplateBuilders) throws GeneralSecurityException, IOException {
Expand Down Expand Up @@ -117,7 +114,7 @@ private static void setupTLS(AbstractDiscoveryClientOptionalArgs<?> args, TlsPro
}

@Configuration(proxyBeanMethods = false)
@Conditional(JerseyClientPresentAndEnabledCondition.class)
@Conditional(RestTemplateEnabledCondition.class)
@ConditionalOnBean(value = AbstractDiscoveryClientOptionalArgs.class, search = SearchStrategy.CURRENT)
heowc marked this conversation as resolved.
Show resolved Hide resolved
static class DiscoveryClientOptionalArgsTlsConfiguration {

Expand Down Expand Up @@ -174,19 +171,27 @@ public WebClientNotFoundConfiguration() {

}

static class JerseyClientPresentAndEnabledCondition extends AllNestedConditions {
static class RestTemplateEnabledCondition extends AnyNestedCondition {

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

@ConditionalOnClass(name = "org.glassfish.jersey.client.JerseyClient")
static class OnJerseyClientPresent {
heowc marked this conversation as resolved.
Show resolved Hide resolved
@ConditionalOnProperty(value = "eureka.client.jersey.enabled", matchIfMissing = true)
static class OnJerseyClientEnabled {

}

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

}

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

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.netflix.eureka.config;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.netflix.eureka.http.RestClientEurekaHttpClient;
import org.springframework.cloud.netflix.eureka.http.WebClientEurekaHttpClient;
import org.springframework.cloud.test.ClassPathExclusions;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author Wonchul Heo
*/
class EurekaConfigServerBootstrapConfigurationClientTests {

@Test
void properBeansCreatedWhenRestClientEnabled() {
heowc marked this conversation as resolved.
Show resolved Hide resolved
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EurekaConfigServerBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.config.discovery.enabled=true")
.withPropertyValues("eureka.client.enabled=true")
.withPropertyValues("eureka.client.restclient.enabled=true")
.run(context -> {
assertThat(context).hasSingleBean(RestClientEurekaHttpClient.class);
assertThat(context).doesNotHaveBean(WebClientEurekaHttpClient.class);
});
}

@Test
void properBeansCreatedWhenWebClientEnabled() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EurekaConfigServerBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.config.discovery.enabled=true")
.withPropertyValues("eureka.client.enabled=true")
.withPropertyValues("eureka.client.webclient.enabled=true")
.run(context -> {
assertThat(context).hasSingleBean(WebClientEurekaHttpClient.class);
assertThat(context).doesNotHaveBean(RestClientEurekaHttpClient.class);
});
}

@Nested
@ClassPathExclusions({ "spring-webflux-*" })
static class NoWebFlux {

@Test
void properBeansCreatedWhenRestClientEnabled() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EurekaConfigServerBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.config.discovery.enabled=true")
.withPropertyValues("eureka.client.enabled=true")
.withPropertyValues("eureka.client.restclient.enabled=true")
.run(context -> {
assertThat(context).hasSingleBean(RestClientEurekaHttpClient.class);
assertThat(context).doesNotHaveBean(WebClientEurekaHttpClient.class);
});
}

@Test
void properBeansCreatedWhenWebEnabledThenFailed() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EurekaConfigServerBootstrapConfiguration.class))
.withPropertyValues("spring.cloud.config.discovery.enabled=true")
.withPropertyValues("eureka.client.enabled=true")
.withPropertyValues("eureka.client.webclient.enabled=true")
.run(context -> {
assertThatThrownBy(() -> context.getBean(WebClientEurekaHttpClient.class))
.hasRootCauseInstanceOf(NoSuchBeanDefinitionException.class)
.hasRootCauseMessage(
"No qualifying bean of type 'com.netflix.discovery.shared.transport.EurekaHttpClient' available: "
+ "expected at least 1 bean which qualifies as autowire candidate. "
+ "Dependency annotations: {}");
});
}

}

}