Gets ReactorServiceInstanceLoadBalancer from context factory.

Along with https://github.com/spring-cloud/spring-cloud-commons/issues/825 this should eliminate the use of `BeanFactoryUtils.beanNamesForTypeIncludingAncestors()` during loadbalancing.

Fixes gh-1941
This commit is contained in:
spencergibb
2020-09-15 14:03:21 -04:00
parent fa0ffd0a2b
commit e4fc804fc2
2 changed files with 15 additions and 13 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.support.DelegatingServiceInstance;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
@@ -118,17 +119,18 @@ public class ReactiveLoadBalancerClientFilter implements GlobalFilter, Ordered {
return LoadBalancerUriTools.reconstructURI(serviceInstance, original);
}
@SuppressWarnings("deprecation")
private Mono<Response<ServiceInstance>> choose(ServerWebExchange exchange) {
URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
ReactorLoadBalancer<ServiceInstance> loadBalancer = this.clientFactory
.getInstance(uri.getHost(), ReactorLoadBalancer.class,
ServiceInstance.class);
.getInstance(uri.getHost(), ReactorServiceInstanceLoadBalancer.class);
if (loadBalancer == null) {
throw new NotFoundException("No loadbalancer available for " + uri.getHost());
}
return loadBalancer.choose(createRequest());
}
@SuppressWarnings("deprecation")
private Request createRequest() {
return ReactiveLoadBalancer.REQUEST;
}

View File

@@ -32,7 +32,7 @@ import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.loadbalancer.support.ServiceInstanceSuppliers;
@@ -121,10 +121,10 @@ public class ReactiveLoadBalancerClientFilterTests {
ServiceInstance serviceInstance = new DefaultServiceInstance("myservice1",
"myservice", "localhost", 8080, true);
when(clientFactory.getInstance("myservice", ReactorLoadBalancer.class,
ServiceInstance.class)).thenReturn(new RoundRobinLoadBalancer("myservice",
ServiceInstanceSuppliers.toProvider("myservice", serviceInstance),
-1));
when(clientFactory.getInstance("myservice",
ReactorServiceInstanceLoadBalancer.class)).thenReturn(
new RoundRobinLoadBalancer("myservice", ServiceInstanceSuppliers
.toProvider("myservice", serviceInstance), -1));
when(chain.filter(exchange)).thenReturn(Mono.empty());
@@ -133,8 +133,8 @@ public class ReactiveLoadBalancerClientFilterTests {
assertThat((LinkedHashSet<URI>) exchange
.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR)).contains(url);
verify(clientFactory).getInstance("myservice", ReactorLoadBalancer.class,
ServiceInstance.class);
verify(clientFactory).getInstance("myservice",
ReactorServiceInstanceLoadBalancer.class);
verifyNoMoreInteractions(clientFactory);
@@ -246,8 +246,8 @@ public class ReactiveLoadBalancerClientFilterTests {
public void shouldThrow4O4ExceptionWhenNoServiceInstanceIsFound() {
URI uri = UriComponentsBuilder.fromUriString("lb://service1").build().toUri();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
when(clientFactory.getInstance("service1", ReactorLoadBalancer.class,
ServiceInstance.class))
when(clientFactory.getInstance("service1",
ReactorServiceInstanceLoadBalancer.class))
.thenReturn(new RoundRobinLoadBalancer("service1",
ServiceInstanceSuppliers.toProvider("service1"), -1));
properties.setUse404(true);
@@ -273,8 +273,8 @@ public class ReactiveLoadBalancerClientFilterTests {
.forClass(ServerWebExchange.class);
when(chain.filter(captor.capture())).thenReturn(Mono.empty());
when(clientFactory.getInstance("service1", ReactorLoadBalancer.class,
ServiceInstance.class))
when(clientFactory.getInstance("service1",
ReactorServiceInstanceLoadBalancer.class))
.thenReturn(new RoundRobinLoadBalancer("service1",
ServiceInstanceSuppliers.toProvider("service1",
new DefaultServiceInstance("service1_1",