-
Notifications
You must be signed in to change notification settings - Fork 715
Lb custom configuration methods #741
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...main/java/org/springframework/cloud/loadbalancer/annotation/configutil/LoadBalancers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2012-2019 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.annotation.configutil; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.cloud.client.ServiceInstance; | ||
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.support.LoadBalancerClientFactory; | ||
import org.springframework.core.env.Environment; | ||
|
||
/** | ||
* Utility class containing helper methods for setting up {@link ReactorLoadBalancer} | ||
* configuration in a more concise way. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 2.2.3 | ||
*/ | ||
public final class LoadBalancers { | ||
|
||
private LoadBalancers() { | ||
throw new IllegalStateException("Can't instantiate a utility class"); | ||
} | ||
|
||
public static ReactorLoadBalancer<ServiceInstance> roundRobin( | ||
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, | ||
String serviceId) { | ||
return new RoundRobinLoadBalancer(serviceInstanceListSupplierProvider, serviceId); | ||
} | ||
|
||
public static ReactorLoadBalancer<ServiceInstance> roundRobin( | ||
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider, | ||
Environment environment) { | ||
return new RoundRobinLoadBalancer(serviceInstanceListSupplierProvider, | ||
environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME)); | ||
} | ||
|
||
public static ReactorLoadBalancer<ServiceInstance> roundRobin( | ||
LoadBalancerClientFactory loadBalancerClientFactory, String serviceId) { | ||
return roundRobin( | ||
serviceInstanceListSupplierProvider(loadBalancerClientFactory, serviceId), | ||
serviceId); | ||
} | ||
|
||
public static ReactorLoadBalancer<ServiceInstance> roundRobin( | ||
LoadBalancerClientFactory loadBalancerClientFactory, | ||
Environment environment) { | ||
String serviceId = environment | ||
.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); | ||
return new RoundRobinLoadBalancer( | ||
serviceInstanceListSupplierProvider(loadBalancerClientFactory, serviceId), | ||
serviceId); | ||
} | ||
|
||
private static ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider( | ||
LoadBalancerClientFactory loadBalancerClientFactory, String serviceId) { | ||
return loadBalancerClientFactory.getLazyProvider(serviceId, | ||
ServiceInstanceListSupplier.class); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...pringframework/cloud/loadbalancer/annotation/configutil/ServiceInstanceListSuppliers.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2012-2019 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.annotation.configutil; | ||
|
||
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.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||
import org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier; | ||
import org.springframework.cloud.loadbalancer.core.HealthCheckServiceInstanceListSupplier; | ||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; | ||
import org.springframework.cloud.loadbalancer.core.ZonePreferenceServiceInstanceListSupplier; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
/** | ||
* Utility class containing helper methods for setting up | ||
* {@link ServiceInstanceListSupplier} configuration in a more concise way. | ||
* | ||
* @author Olga Maciaszek-Sharma | ||
* @since 2.2.3 | ||
*/ | ||
public final class ServiceInstanceListSuppliers { | ||
|
||
private ServiceInstanceListSuppliers() { | ||
throw new IllegalStateException("Can't instantiate a utility class"); | ||
} | ||
|
||
public static ServiceInstanceListSupplier cachedOrDelegate( | ||
ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider, | ||
ServiceInstanceListSupplier delegate) { | ||
if (cacheManagerProvider.getIfAvailable() != null) { | ||
return new CachingServiceInstanceListSupplier(delegate, | ||
cacheManagerProvider.getIfAvailable()); | ||
} | ||
return delegate; | ||
} | ||
|
||
public static ServiceInstanceListSupplier cachedOrDelegate(ApplicationContext context, | ||
ServiceInstanceListSupplier delegate) { | ||
return cachedOrDelegate(context.getBeanProvider(LoadBalancerCacheManager.class), | ||
delegate); | ||
} | ||
|
||
public static ServiceInstanceListSupplier discoveryClientBased( | ||
ReactiveDiscoveryClient discoveryClient, Environment environment) { | ||
return new DiscoveryClientServiceInstanceListSupplier(discoveryClient, | ||
environment); | ||
} | ||
|
||
public static ServiceInstanceListSupplier discoveryClientBased( | ||
DiscoveryClient discoveryClient, Environment environment) { | ||
return new DiscoveryClientServiceInstanceListSupplier(discoveryClient, | ||
environment); | ||
} | ||
|
||
public static ServiceInstanceListSupplier zonePreferenceBased( | ||
ServiceInstanceListSupplier delegate, LoadBalancerZoneConfig zoneConfig) { | ||
return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig); | ||
} | ||
|
||
public static ServiceInstanceListSupplier healthCheckBased( | ||
ServiceInstanceListSupplier delegate, | ||
LoadBalancerProperties.HealthCheck healthCheck, WebClient webClient) { | ||
return new HealthCheckServiceInstanceListSupplier(delegate, healthCheck, | ||
webClient); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one feels like a true builder is needed, since you will start with discovery client, then add zone preference or health chech and cache later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that each of these objects has only 2 fields and they are always required. I've started doing a builder approach here: master...lb-config-builders , but then we would still either be passing all the arguments in constructor or having to call all the builder methods one after another. I could add some methods like
withDiscoveryClientDelegate(discoveryClient, environment)
, but am not sure that it would be easier to use than those utility methods. Or where you thinking about implementing it yet another way? wdyt?