Use configuration properties instead of environment. (#662)
* Use configuration properties instead of environment. * Fix after review.
This commit is contained in:
committed by
GitHub
parent
f25bbad456
commit
8c054083cc
@@ -26,6 +26,7 @@ import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
|
||||
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager;
|
||||
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceSupplier;
|
||||
@@ -48,12 +49,18 @@ import org.springframework.core.env.Environment;
|
||||
* @author Tim Ysewyn
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties
|
||||
@EnableConfigurationProperties(LoadBalancerProperties.class)
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
public class LoadBalancerClientConfiguration {
|
||||
|
||||
private static final int REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER = 193827465;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
LoadBalancerProperties loadBalancerProperties() {
|
||||
return new LoadBalancerProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.util.Map;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
|
||||
|
||||
/**
|
||||
* An implementation of {@link ServiceInstanceListSupplier} that filters instances
|
||||
@@ -42,14 +42,14 @@ public class ZonePreferenceServiceInstanceListSupplier
|
||||
|
||||
private final ServiceInstanceListSupplier delegate;
|
||||
|
||||
private final Environment environment;
|
||||
private final LoadBalancerProperties loadBalancerProperties;
|
||||
|
||||
private String zone;
|
||||
|
||||
public ZonePreferenceServiceInstanceListSupplier(ServiceInstanceListSupplier delegate,
|
||||
Environment environment) {
|
||||
LoadBalancerProperties loadBalancerProperties) {
|
||||
this.delegate = delegate;
|
||||
this.environment = environment;
|
||||
this.loadBalancerProperties = loadBalancerProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +64,7 @@ public class ZonePreferenceServiceInstanceListSupplier
|
||||
|
||||
private List<ServiceInstance> filteredByZone(List<ServiceInstance> serviceInstances) {
|
||||
if (zone == null) {
|
||||
zone = environment.getProperty("spring.cloud.loadbalancer.zone");
|
||||
zone = loadBalancerProperties.getZone();
|
||||
}
|
||||
if (zone != null) {
|
||||
List<ServiceInstance> filteredInstances = new ArrayList<>();
|
||||
|
||||
@@ -27,7 +27,7 @@ import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
@@ -44,10 +44,10 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
private DiscoveryClientServiceInstanceListSupplier delegate = mock(
|
||||
DiscoveryClientServiceInstanceListSupplier.class);
|
||||
|
||||
private Environment environment = mock(Environment.class);
|
||||
private LoadBalancerProperties loadBalancerProperties = new LoadBalancerProperties();
|
||||
|
||||
private ZonePreferenceServiceInstanceListSupplier supplier = new ZonePreferenceServiceInstanceListSupplier(
|
||||
delegate, environment);
|
||||
delegate, loadBalancerProperties);
|
||||
|
||||
private ServiceInstance first = serviceInstance("test-1", buildZoneMetadata("zone1"));
|
||||
|
||||
@@ -63,8 +63,7 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
@Test
|
||||
void shouldFilterInstancesByZone() {
|
||||
when(environment.getProperty("spring.cloud.loadbalancer.zone"))
|
||||
.thenReturn("zone1");
|
||||
loadBalancerProperties.setZone("zone1");
|
||||
when(delegate.get()).thenReturn(
|
||||
Flux.just(Arrays.asList(first, second, third, fourth, fifth)));
|
||||
|
||||
@@ -79,8 +78,7 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
@Test
|
||||
void shouldReturnAllInstancesIfNoZoneInstances() {
|
||||
when(environment.getProperty("spring.cloud.loadbalancer.zone"))
|
||||
.thenReturn("zone1");
|
||||
loadBalancerProperties.setZone("zone1");
|
||||
when(delegate.get()).thenReturn(Flux.just(Arrays.asList(third, fourth)));
|
||||
|
||||
List<ServiceInstance> filtered = supplier.get().blockFirst();
|
||||
@@ -91,8 +89,7 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
@Test
|
||||
void shouldNotThrowNPEIfNullInstanceMetadata() {
|
||||
when(environment.getProperty("spring.cloud.loadbalancer.zone"))
|
||||
.thenReturn("zone1");
|
||||
loadBalancerProperties.setZone("zone1");
|
||||
when(delegate.get()).thenReturn(
|
||||
Flux.just(Collections.singletonList(serviceInstance("test-6", null))));
|
||||
assertThatCode(() -> supplier.get().blockFirst()).doesNotThrowAnyException();
|
||||
@@ -100,7 +97,7 @@ class ZonePreferenceServiceInstanceListSupplierTests {
|
||||
|
||||
@Test
|
||||
void shouldReturnAllInstancesIfNoZone() {
|
||||
when(environment.getProperty("spring.cloud.loadbalancer.zone")).thenReturn(null);
|
||||
loadBalancerProperties.setZone(null);
|
||||
when(delegate.get())
|
||||
.thenReturn(Flux.just(Arrays.asList(first, second, third, fourth)));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user