Skip to content

Renames ServiceInstanceListSuppliers to ServiceInstanceListSupplierBu… #761

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

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/spring-cloud-commons.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ public class CustomLoadBalancerConfiguration {
@Bean
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder()
return ServiceInstanceListSupplier.builder()
.withDiscoveryClient()
.withHealthChecks()
.withCaching()
Expand All @@ -968,7 +968,7 @@ public class CustomLoadBalancerConfiguration {
}
----

TIP:: In order to make working on your own LoadBalancer configuration easier, we have added some utility methods in `ServiceInstanceListSuppliers` class.
TIP:: In order to make working on your own LoadBalancer configuration easier, we have added a `builder()` method to the `ServiceInstanceListSupplier` class.

TIP:: You can also use our alternative predefined configurations in place of the default ones by setting the value of `spring.cloud.loadbalancer.configurations` property to `zone-preference` to use `ZonePreferenceServiceInstanceListSupplier` with caching or to `health-check` to use `HealthCheckServiceInstanceListSupplier` with caching.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSuppliers;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -75,7 +74,7 @@ public static class ReactiveSupportConfiguration {
havingValue = "default", matchIfMissing = true)
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder().withDiscoveryClient()
return ServiceInstanceListSupplier.builder().withDiscoveryClient()
.withCaching().build(context);
}

Expand All @@ -86,7 +85,7 @@ public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
havingValue = "zone-preference")
public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder().withDiscoveryClient()
return ServiceInstanceListSupplier.builder().withDiscoveryClient()
.withZonePreference().withCaching().build(context);
}

Expand All @@ -97,7 +96,7 @@ public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceL
havingValue = "health-check")
public ServiceInstanceListSupplier healthCheckDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder().withDiscoveryClient()
return ServiceInstanceListSupplier.builder().withDiscoveryClient()
.withHealthChecks().withCaching().build(context);
}

Expand Down Expand Up @@ -132,7 +131,7 @@ public static class BlockingSupportConfiguration {
havingValue = "default", matchIfMissing = true)
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder().withBlockingDiscoveryClient()
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
.withCaching().build(context);
}

Expand All @@ -143,7 +142,7 @@ public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
havingValue = "zone-preference")
public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder().withBlockingDiscoveryClient()
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
.withZonePreference().withCaching().build(context);
}

Expand All @@ -154,7 +153,7 @@ public ServiceInstanceListSupplier zonePreferenceDiscoveryClientServiceInstanceL
havingValue = "health-check")
public ServiceInstanceListSupplier healthCheckDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSuppliers.builder().withBlockingDiscoveryClient()
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
.withHealthChecks().withCaching().build(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,32 @@ public interface ServiceInstanceListSupplier

String getServiceId();

static ServiceInstanceListSupplierBuilder builder() {
return new ServiceInstanceListSupplierBuilder();
}

static FixedServiceInstanceListSupplier.Builder fixed(Environment environment) {
return new FixedServiceInstanceListSupplier.Builder(environment);
}

static FixedServiceInstanceListSupplier.SimpleBuilder fixed(String serviceId) {
return new FixedServiceInstanceListSupplier.SimpleBuilder(serviceId);
}

class FixedServiceInstanceListSupplier implements ServiceInstanceListSupplier {

private final String serviceId;

private List<ServiceInstance> instances;

@Deprecated
public static Builder with(Environment env) {
return new Builder(env);
}

private FixedServiceInstanceListSupplier(Environment env,
private FixedServiceInstanceListSupplier(String serviceId,
List<ServiceInstance> instances) {
this.serviceId = env.getProperty(PROPERTY_NAME);
this.serviceId = serviceId;
this.instances = instances;
}

Expand All @@ -69,6 +78,43 @@ public Flux<List<ServiceInstance>> get() {
return Flux.just(instances);
}

@Deprecated
public static final class SimpleBuilder {

private final ArrayList<ServiceInstance> instances = new ArrayList<>();

private final String serviceId;

private SimpleBuilder(String serviceId) {
this.serviceId = serviceId;
}

public SimpleBuilder instance(ServiceInstance instance) {
instances.add(instance);
return this;
}

public SimpleBuilder instance(int port) {
return instance("localhost", port);
}

public SimpleBuilder instance(String host, int port) {
DefaultServiceInstance instance = new DefaultServiceInstance(
instanceId(serviceId, host, port), serviceId, host, port, false);
return instance(instance);
}

private String instanceId(String serviceId, String host, int port) {
return serviceId + ":" + host + ":" + port;
}

public FixedServiceInstanceListSupplier build() {
return new FixedServiceInstanceListSupplier(serviceId, instances);
}

}

@Deprecated
public static final class Builder {

private final Environment env;
Expand Down Expand Up @@ -99,7 +145,8 @@ private String instanceId(String serviceId, String host, int port) {
}

public FixedServiceInstanceListSupplier build() {
return new FixedServiceInstanceListSupplier(env, instances);
return new FixedServiceInstanceListSupplier(
env.getProperty(PROPERTY_NAME), instances);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright 2013-2020 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.loadbalancer.core;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.ObjectProvider;
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.config.LoadBalancerZoneConfig;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.client.WebClient;

/**
* A Builder for creating a {@link ServiceInstanceListSupplier} hierarchy to be used in
* {@link ReactorLoadBalancer} configuration.
*
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
*/
public final class ServiceInstanceListSupplierBuilder {

private static final Log LOG = LogFactory
.getLog(ServiceInstanceListSupplierBuilder.class);

private Creator baseCreator;

private DelegateCreator cachingCreator;

private final List<DelegateCreator> creators = new ArrayList<>();

ServiceInstanceListSupplierBuilder() {
}

/**
* Sets a blocking {@link DiscoveryClient}-based
* {@link DiscoveryClientServiceInstanceListSupplier} as a base
* {@link ServiceInstanceListSupplier} in the hierarchy.
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withBlockingDiscoveryClient() {
if (baseCreator != null && LOG.isWarnEnabled()) {
LOG.warn(
"Overriding a previously set baseCreator with a blocking DiscoveryClient baseCreator.");
}
this.baseCreator = context -> {
DiscoveryClient discoveryClient = context.getBean(DiscoveryClient.class);

return new DiscoveryClientServiceInstanceListSupplier(discoveryClient,
context.getEnvironment());
};
return this;
}

/**
* Sets a {@link ReactiveDiscoveryClient}-based
* {@link DiscoveryClientServiceInstanceListSupplier} as a base
* {@link ServiceInstanceListSupplier} in the hierarchy.
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withDiscoveryClient() {
if (baseCreator != null && LOG.isWarnEnabled()) {
LOG.warn(
"Overriding a previously set baseCreator with a ReactiveDiscoveryClient baseCreator.");
}
this.baseCreator = context -> {
ReactiveDiscoveryClient discoveryClient = context
.getBean(ReactiveDiscoveryClient.class);

return new DiscoveryClientServiceInstanceListSupplier(discoveryClient,
context.getEnvironment());
};
return this;
}

/**
* Sets a user-provided {@link ServiceInstanceListSupplier} as a base
* {@link ServiceInstanceListSupplier} in the hierarchy.
* @param supplier a user-provided {@link ServiceInstanceListSupplier} instance
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withBase(
ServiceInstanceListSupplier supplier) {
this.baseCreator = context -> supplier;
return this;
}

/**
* Adds a {@link HealthCheckServiceInstanceListSupplier} to the
* {@link ServiceInstanceListSupplier} hierarchy.
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withHealthChecks() {
DelegateCreator creator = (context, delegate) -> {
LoadBalancerProperties properties = context
.getBean(LoadBalancerProperties.class);
WebClient.Builder webClient = context.getBean(WebClient.Builder.class);
return new HealthCheckServiceInstanceListSupplier(delegate,
properties.getHealthCheck(), webClient.build());
};
this.creators.add(creator);
return this;
}

/**
* Adds a {@link HealthCheckServiceInstanceListSupplier} that uses user-provided
* {@link WebClient} instance to the {@link ServiceInstanceListSupplier} hierarchy.
* @param webClient a user-provided {@link WebClient} instance
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withHealthChecks(WebClient webClient) {
DelegateCreator creator = (context, delegate) -> {
LoadBalancerProperties properties = context
.getBean(LoadBalancerProperties.class);
return new HealthCheckServiceInstanceListSupplier(delegate,
properties.getHealthCheck(), webClient);
};
this.creators.add(creator);
return this;
}

/**
* Adds a {@link ZonePreferenceServiceInstanceListSupplier} to the
* {@link ServiceInstanceListSupplier} hierarchy.
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withZonePreference() {
DelegateCreator creator = (context, delegate) -> {
LoadBalancerZoneConfig zoneConfig = context
.getBean(LoadBalancerZoneConfig.class);
return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig);
};
this.creators.add(creator);
return this;
}

/**
* If {@link LoadBalancerCacheManager} is available in the context, wraps created
* {@link ServiceInstanceListSupplier} hierarchy with a
* {@link CachingServiceInstanceListSupplier} instance to provide a caching mechanism
* for service instances. Uses {@link ObjectProvider} to lazily resolve
* {@link LoadBalancerCacheManager}.
* @return the {@link ServiceInstanceListSupplierBuilder} object
*/
public ServiceInstanceListSupplierBuilder withCaching() {
if (cachingCreator != null && LOG.isWarnEnabled()) {
LOG.warn(
"Overriding a previously set cachingCreator with a CachingServiceInstanceListSupplier-based cachingCreator.");
}
this.cachingCreator = (context, delegate) -> {
ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context
.getBeanProvider(LoadBalancerCacheManager.class);
if (cacheManagerProvider.getIfAvailable() != null) {
return new CachingServiceInstanceListSupplier(delegate,
cacheManagerProvider.getIfAvailable());
}
if (LOG.isWarnEnabled()) {
LOG.warn(
"LoadBalancerCacheManager not available, returning delegate without caching.");
}
return delegate;
};
return this;
}

/**
* Builds the {@link ServiceInstanceListSupplier} hierarchy.
* @param context application context
* @return a {@link ServiceInstanceListSupplier} instance on top of the delegate
* hierarchy
*/
public ServiceInstanceListSupplier build(ConfigurableApplicationContext context) {
Assert.notNull(baseCreator, "A baseCreator must not be null");

ServiceInstanceListSupplier supplier = baseCreator.apply(context);

for (DelegateCreator creator : creators) {
supplier = creator.apply(context, supplier);
}

if (this.cachingCreator != null) {
supplier = this.cachingCreator.apply(context, supplier);
}
return supplier;
}

/**
* Allows creating a {@link ServiceInstanceListSupplier} instance based on provided
* {@link ConfigurableApplicationContext}.
*/
public interface Creator extends
Function<ConfigurableApplicationContext, ServiceInstanceListSupplier> {

}

/**
* Allows creating a {@link ServiceInstanceListSupplier} instance based on provided
* {@link ConfigurableApplicationContext} and another
* {@link ServiceInstanceListSupplier} instance that will be used as a delegate.
*/
public interface DelegateCreator extends
BiFunction<ConfigurableApplicationContext, ServiceInstanceListSupplier, ServiceInstanceListSupplier> {

}

}
Loading