use of org.springframework.cloud.client.loadbalancer.Request in project hummer-framework by hummer-team.
the class GlobalLoadBalancerFilter method filter.
/**
* Process the Web request and (optionally) delegate to the next {@code WebFilter}
* through the given {@link GatewayFilterChain}.
*
* @param exchange the current server exchange
* @param chain provides a way to delegate to the next filter
* @return {@code Mono<Void>} to indicate when request processing is complete
*/
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
URI url = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR);
if (url == null) {
return chain.filter(exchange);
}
String schemePrefix = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR);
if (!"lb".equals(url.getScheme()) && !"lb".equals(schemePrefix)) {
return chain.filter(exchange);
}
ServerWebExchangeUtils.addOriginalRequestUrl(exchange, url);
if (log.isTraceEnabled()) {
log.trace(ReactiveLoadBalancerClientFilter.class.getSimpleName() + " url before: " + url);
}
return choose(exchange).doOnNext((response) -> {
if (!response.hasServer()) {
throw NotFoundException.create(false, "Unable to find instance for " + url.getHost());
} else {
URI uri = exchange.getRequest().getURI();
String overrideScheme = null;
if (schemePrefix != null) {
overrideScheme = url.getScheme();
}
DelegatingServiceInstance serviceInstance = new DelegatingServiceInstance(response.getServer(), overrideScheme);
URI requestUrl = LoadBalancerUriTools.reconstructURI(serviceInstance, uri);
if (log.isTraceEnabled()) {
log.trace("LoadBalancerClientFilter url chosen: " + requestUrl);
}
exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, requestUrl);
}
}).then(chain.filter(exchange));
}
use of org.springframework.cloud.client.loadbalancer.Request in project hummer-framework by hummer-team.
the class HostByRandomWeightLoadBalancer method choose.
@Override
protected Mono<Instance> choose(Request request, String serviceId, List<Instance> instances) {
return Mono.fromCallable(() -> {
List<Instance> instancesToChoose = instances;
NacosDiscoveryProperties discoveryProperties = SpringApplicationContext.getBean(NacosDiscoveryProperties.class);
if (StringUtils.isNotBlank(discoveryProperties.getClusterName())) {
List<Instance> sameClusterInstances = instances.stream().filter(instance -> Objects.equals(discoveryProperties.getClusterName(), instance.getClusterName())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(sameClusterInstances)) {
instancesToChoose = sameClusterInstances;
} else {
LOGGER.warn("A cross-cluster call occurs,name = {}, clusterName = {}, instance = {}", serviceId, discoveryProperties.getClusterName(), instances);
}
}
// notice: 2.2.7-RELEASE ExtendBalancer.getHostByRandomWeight2(instancesToChoose)
return Balancer.getHostByRandomWeight(instancesToChoose);
});
}
use of org.springframework.cloud.client.loadbalancer.Request in project spring-cloud-gateway by spring-cloud.
the class ReactiveLoadBalancerClientFilterTests method shouldPassRequestToLoadBalancer.
@SuppressWarnings({ "rawtypes" })
@Test
void shouldPassRequestToLoadBalancer() {
String hint = "test";
when(loadBalancerProperties.getHint()).thenReturn(buildHints(hint));
when(clientFactory.getProperties(any())).thenReturn(loadBalancerProperties);
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get?a=b").build();
URI lbUri = URI.create("lb://service1?a=b");
ServerWebExchange serverWebExchange = mock(ServerWebExchange.class);
when(serverWebExchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).thenReturn(lbUri);
when(serverWebExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR)).thenReturn(new LinkedHashSet<>());
when(serverWebExchange.getRequest()).thenReturn(request);
RoundRobinLoadBalancer loadBalancer = mock(RoundRobinLoadBalancer.class);
when(loadBalancer.choose(any(Request.class))).thenReturn(Mono.just(new DefaultResponse(new DefaultServiceInstance("myservice1", "service1", "localhost", 8080, false))));
when(clientFactory.getInstance("service1", ReactorServiceInstanceLoadBalancer.class)).thenReturn(loadBalancer);
when(chain.filter(any())).thenReturn(Mono.empty());
filter.filter(serverWebExchange, chain);
verify(loadBalancer).choose(argThat((Request passedRequest) -> ((RequestDataContext) passedRequest.getContext()).getClientRequest().getUrl().equals(request.getURI()) && ((RequestDataContext) passedRequest.getContext()).getHint().equals(hint)));
}
use of org.springframework.cloud.client.loadbalancer.Request in project spring-cloud-gateway by spring-cloud.
the class ReactiveLoadBalancerClientFilterTests method mockExchange.
@SuppressWarnings({ "rawtypes", "unchecked" })
private ServerWebExchange mockExchange(ServiceInstance serviceInstance, LoadBalancerLifecycle lifecycleProcessor, boolean shouldThrowException) {
Response response;
when(lifecycleProcessor.supports(any(Class.class), any(Class.class), any(Class.class))).thenReturn(true);
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get?a=b").build();
URI lbUri = URI.create("lb://service1?a=b");
ServerWebExchange serverWebExchange = MockServerWebExchange.from(request);
if (serviceInstance == null) {
response = new EmptyResponse();
} else {
response = new DefaultResponse(serviceInstance);
}
serverWebExchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, lbUri);
serverWebExchange.getAttributes().put(GATEWAY_ORIGINAL_REQUEST_URL_ATTR, new LinkedHashSet<>());
serverWebExchange.getAttributes().put(GATEWAY_LOADBALANCER_RESPONSE_ATTR, response);
RoundRobinLoadBalancer loadBalancer = mock(RoundRobinLoadBalancer.class);
when(loadBalancer.choose(any(Request.class))).thenReturn(Mono.just(response));
when(clientFactory.getInstance("service1", ReactorServiceInstanceLoadBalancer.class)).thenReturn(loadBalancer);
Map<String, LoadBalancerLifecycle> lifecycleProcessors = new HashMap<>();
lifecycleProcessors.put("service1", lifecycleProcessor);
when(clientFactory.getInstances("service1", LoadBalancerLifecycle.class)).thenReturn(lifecycleProcessors);
if (shouldThrowException) {
when(chain.filter(any())).thenReturn(Mono.error(new UnsupportedOperationException()));
} else {
when(chain.filter(any())).thenReturn(Mono.empty());
}
return serverWebExchange;
}
Aggregations