org.springframework.cloud.client.loadbalancer.reactive.Response Java Examples

The following examples show how to use org.springframework.cloud.client.loadbalancer.reactive.Response. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LoadBalancerTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
private void assertLoadBalancer(ReactorLoadBalancer<ServiceInstance> loadBalancer,
		List<String> hosts) {
	for (String host : hosts) {
		Mono<Response<ServiceInstance>> source = loadBalancer.choose();
		StepVerifier.create(source).consumeNextWith(response -> {
			then(response).isNotNull();
			then(response.hasServer()).isTrue();

			ServiceInstance instance = response.getServer();
			then(instance).isNotNull();
			then(instance.getHost()).as("instance host is incorrent %s", host)
					.isEqualTo(host);

			if (host.contains("secure")) {
				then(instance.isSecure()).isTrue();
			}
			else {
				then(instance.isSecure()).isFalse();
			}

			response.onComplete(new CompletionContext(Status.SUCCESSS));
		}).verifyComplete();
	}
}
 
Example #2
Source File: ConsumerSCLBApplication.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
	log.info("random spring cloud loadbalacer active -.-");
	ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
			.getIfAvailable(NoopServiceInstanceListSupplier::new);
	return supplier.get().next().map(this::getInstanceResponse);
}
 
Example #3
Source File: ConsumerSCLBApplication.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private Response<ServiceInstance> getInstanceResponse(
		List<ServiceInstance> instances) {
	if (instances.isEmpty()) {
		return new EmptyResponse();
	}
	ServiceInstance instance = instances.get(random.nextInt(instances.size()));

	return new DefaultResponse(instance);
}
 
Example #4
Source File: ReactiveLoadBalancerClientFilter.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
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);
	if (loadBalancer == null) {
		throw new NotFoundException("No loadbalancer available for " + uri.getHost());
	}
	return loadBalancer.choose(createRequest());
}
 
Example #5
Source File: RoundRobinLoadBalancer.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
// see original
// https://github.com/Netflix/ocelli/blob/master/ocelli-core/
// src/main/java/netflix/ocelli/loadbalancer/RoundRobinLoadBalancer.java
public Mono<Response<ServiceInstance>> choose(Request request) {
	ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
			.getIfAvailable(NoopServiceInstanceListSupplier::new);
	return supplier.get().next().map(this::getInstanceResponse);
}
 
Example #6
Source File: RoundRobinLoadBalancer.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
private Response<ServiceInstance> getInstanceResponse(
		List<ServiceInstance> instances) {
	if (instances.isEmpty()) {
		log.warn("No servers available for service: " + this.serviceId);
		return new EmptyResponse();
	}
	// TODO: enforce order?
	int pos = Math.abs(this.position.incrementAndGet());

	ServiceInstance instance = instances.get(pos % instances.size());

	return new DefaultResponse(instance);
}
 
Example #7
Source File: BlockingLoadBalancerClient.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public ServiceInstance choose(String serviceId) {
	ReactiveLoadBalancer<ServiceInstance> loadBalancer = loadBalancerClientFactory
			.getInstance(serviceId);
	if (loadBalancer == null) {
		return null;
	}
	Response<ServiceInstance> loadBalancerResponse = Mono.from(loadBalancer.choose())
			.block();
	if (loadBalancerResponse == null) {
		return null;
	}
	return loadBalancerResponse.getServer();
}
 
Example #8
Source File: LoadBalancerTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyHosts() {
	ResolvableType type = ResolvableType
			.forClassWithGenerics(ReactorLoadBalancer.class, ServiceInstance.class);
	ReactorLoadBalancer<ServiceInstance> loadBalancer = this.clientFactory
			.getInstance("unknownservice", type);

	then(loadBalancer).isInstanceOf(RoundRobinLoadBalancer.class);

	Mono<Response<ServiceInstance>> source = loadBalancer.choose();
	StepVerifier.create(source).consumeNextWith(response -> {
		then(response).isNotNull();
		then(response.hasServer()).isFalse();
	}).verifyComplete();
}
 
Example #9
Source File: LoadBalancerTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
	if (request.getContext() instanceof DefaultRequestContext) {
		DefaultRequestContext requestContext = (DefaultRequestContext) request
				.getContext();
		return Mono.just(new DefaultResponse(
				instance(requestContext.getHint(), "host", false)));
	}
	return Mono.empty();
}
 
Example #10
Source File: BlockingLoadBalancerClientTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Response<ServiceInstance>> choose() {
	List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
	if (instances.size() == 0) {
		return Mono.just(new EmptyResponse());
	}
	int instanceIdx = this.random.nextInt(instances.size());
	return Mono.just(new DefaultResponse(instances.get(instanceIdx)));
}
 
Example #11
Source File: ReactorLoadBalancer.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
default Mono<Response<T>> choose() {
	return choose(REQUEST);
}
 
Example #12
Source File: BlockingLoadBalancerClientTests.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
	return choose();
}
 
Example #13
Source File: ReactorLoadBalancer.java    From spring-cloud-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Choose the next server based on the load balancing algorithm.
 * @param request - an input request
 * @return - mono of response
 */
@SuppressWarnings("rawtypes")
Mono<Response<T>> choose(Request request);