|
| 1 | +/* |
| 2 | + * Copyright 2013-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.loadbalancer.core; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.function.BiFunction; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +import org.apache.commons.logging.Log; |
| 25 | +import org.apache.commons.logging.LogFactory; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.ObjectProvider; |
| 28 | +import org.springframework.cloud.client.discovery.DiscoveryClient; |
| 29 | +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; |
| 30 | +import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties; |
| 31 | +import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager; |
| 32 | +import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig; |
| 33 | +import org.springframework.context.ConfigurableApplicationContext; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.web.reactive.function.client.WebClient; |
| 36 | + |
| 37 | +/** |
| 38 | + * A Builder for creating a {@link ServiceInstanceListSupplier} hierarchy to be used in |
| 39 | + * {@link ReactorLoadBalancer} configuration. |
| 40 | + * |
| 41 | + * @author Spencer Gibb |
| 42 | + * @author Olga Maciaszek-Sharma |
| 43 | + */ |
| 44 | +public final class ServiceInstanceListSupplierBuilder { |
| 45 | + |
| 46 | + private static final Log LOG = LogFactory |
| 47 | + .getLog(ServiceInstanceListSupplierBuilder.class); |
| 48 | + |
| 49 | + private Creator baseCreator; |
| 50 | + |
| 51 | + private DelegateCreator cachingCreator; |
| 52 | + |
| 53 | + private final List<DelegateCreator> creators = new ArrayList<>(); |
| 54 | + |
| 55 | + ServiceInstanceListSupplierBuilder() { |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Sets a blocking {@link DiscoveryClient}-based |
| 60 | + * {@link DiscoveryClientServiceInstanceListSupplier} as a base |
| 61 | + * {@link ServiceInstanceListSupplier} in the hierarchy. |
| 62 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 63 | + */ |
| 64 | + public ServiceInstanceListSupplierBuilder withBlockingDiscoveryClient() { |
| 65 | + if (baseCreator != null && LOG.isWarnEnabled()) { |
| 66 | + LOG.warn( |
| 67 | + "Overriding a previously set baseCreator with a blocking DiscoveryClient baseCreator."); |
| 68 | + } |
| 69 | + this.baseCreator = context -> { |
| 70 | + DiscoveryClient discoveryClient = context.getBean(DiscoveryClient.class); |
| 71 | + |
| 72 | + return new DiscoveryClientServiceInstanceListSupplier(discoveryClient, |
| 73 | + context.getEnvironment()); |
| 74 | + }; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sets a {@link ReactiveDiscoveryClient}-based |
| 80 | + * {@link DiscoveryClientServiceInstanceListSupplier} as a base |
| 81 | + * {@link ServiceInstanceListSupplier} in the hierarchy. |
| 82 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 83 | + */ |
| 84 | + public ServiceInstanceListSupplierBuilder withDiscoveryClient() { |
| 85 | + if (baseCreator != null && LOG.isWarnEnabled()) { |
| 86 | + LOG.warn( |
| 87 | + "Overriding a previously set baseCreator with a ReactiveDiscoveryClient baseCreator."); |
| 88 | + } |
| 89 | + this.baseCreator = context -> { |
| 90 | + ReactiveDiscoveryClient discoveryClient = context |
| 91 | + .getBean(ReactiveDiscoveryClient.class); |
| 92 | + |
| 93 | + return new DiscoveryClientServiceInstanceListSupplier(discoveryClient, |
| 94 | + context.getEnvironment()); |
| 95 | + }; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets a user-provided {@link ServiceInstanceListSupplier} as a base |
| 101 | + * {@link ServiceInstanceListSupplier} in the hierarchy. |
| 102 | + * @param supplier a user-provided {@link ServiceInstanceListSupplier} instance |
| 103 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 104 | + */ |
| 105 | + public ServiceInstanceListSupplierBuilder withBase( |
| 106 | + ServiceInstanceListSupplier supplier) { |
| 107 | + this.baseCreator = context -> supplier; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Adds a {@link HealthCheckServiceInstanceListSupplier} to the |
| 113 | + * {@link ServiceInstanceListSupplier} hierarchy. |
| 114 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 115 | + */ |
| 116 | + public ServiceInstanceListSupplierBuilder withHealthChecks() { |
| 117 | + DelegateCreator creator = (context, delegate) -> { |
| 118 | + LoadBalancerProperties properties = context |
| 119 | + .getBean(LoadBalancerProperties.class); |
| 120 | + WebClient.Builder webClient = context.getBean(WebClient.Builder.class); |
| 121 | + return new HealthCheckServiceInstanceListSupplier(delegate, |
| 122 | + properties.getHealthCheck(), webClient.build()); |
| 123 | + }; |
| 124 | + this.creators.add(creator); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Adds a {@link HealthCheckServiceInstanceListSupplier} that uses user-provided |
| 130 | + * {@link WebClient} instance to the {@link ServiceInstanceListSupplier} hierarchy. |
| 131 | + * @param webClient a user-provided {@link WebClient} instance |
| 132 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 133 | + */ |
| 134 | + public ServiceInstanceListSupplierBuilder withHealthChecks(WebClient webClient) { |
| 135 | + DelegateCreator creator = (context, delegate) -> { |
| 136 | + LoadBalancerProperties properties = context |
| 137 | + .getBean(LoadBalancerProperties.class); |
| 138 | + return new HealthCheckServiceInstanceListSupplier(delegate, |
| 139 | + properties.getHealthCheck(), webClient); |
| 140 | + }; |
| 141 | + this.creators.add(creator); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Adds a {@link ZonePreferenceServiceInstanceListSupplier} to the |
| 147 | + * {@link ServiceInstanceListSupplier} hierarchy. |
| 148 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 149 | + */ |
| 150 | + public ServiceInstanceListSupplierBuilder withZonePreference() { |
| 151 | + DelegateCreator creator = (context, delegate) -> { |
| 152 | + LoadBalancerZoneConfig zoneConfig = context |
| 153 | + .getBean(LoadBalancerZoneConfig.class); |
| 154 | + return new ZonePreferenceServiceInstanceListSupplier(delegate, zoneConfig); |
| 155 | + }; |
| 156 | + this.creators.add(creator); |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * If {@link LoadBalancerCacheManager} is available in the context, wraps created |
| 162 | + * {@link ServiceInstanceListSupplier} hierarchy with a |
| 163 | + * {@link CachingServiceInstanceListSupplier} instance to provide a caching mechanism |
| 164 | + * for service instances. Uses {@link ObjectProvider} to lazily resolve |
| 165 | + * {@link LoadBalancerCacheManager}. |
| 166 | + * @return the {@link ServiceInstanceListSupplierBuilder} object |
| 167 | + */ |
| 168 | + public ServiceInstanceListSupplierBuilder withCaching() { |
| 169 | + if (cachingCreator != null && LOG.isWarnEnabled()) { |
| 170 | + LOG.warn( |
| 171 | + "Overriding a previously set cachingCreator with a CachingServiceInstanceListSupplier-based cachingCreator."); |
| 172 | + } |
| 173 | + this.cachingCreator = (context, delegate) -> { |
| 174 | + ObjectProvider<LoadBalancerCacheManager> cacheManagerProvider = context |
| 175 | + .getBeanProvider(LoadBalancerCacheManager.class); |
| 176 | + if (cacheManagerProvider.getIfAvailable() != null) { |
| 177 | + return new CachingServiceInstanceListSupplier(delegate, |
| 178 | + cacheManagerProvider.getIfAvailable()); |
| 179 | + } |
| 180 | + if (LOG.isWarnEnabled()) { |
| 181 | + LOG.warn( |
| 182 | + "LoadBalancerCacheManager not available, returning delegate without caching."); |
| 183 | + } |
| 184 | + return delegate; |
| 185 | + }; |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Builds the {@link ServiceInstanceListSupplier} hierarchy. |
| 191 | + * @param context application context |
| 192 | + * @return a {@link ServiceInstanceListSupplier} instance on top of the delegate |
| 193 | + * hierarchy |
| 194 | + */ |
| 195 | + public ServiceInstanceListSupplier build(ConfigurableApplicationContext context) { |
| 196 | + Assert.notNull(baseCreator, "A baseCreator must not be null"); |
| 197 | + |
| 198 | + ServiceInstanceListSupplier supplier = baseCreator.apply(context); |
| 199 | + |
| 200 | + for (DelegateCreator creator : creators) { |
| 201 | + supplier = creator.apply(context, supplier); |
| 202 | + } |
| 203 | + |
| 204 | + if (this.cachingCreator != null) { |
| 205 | + supplier = this.cachingCreator.apply(context, supplier); |
| 206 | + } |
| 207 | + return supplier; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Allows creating a {@link ServiceInstanceListSupplier} instance based on provided |
| 212 | + * {@link ConfigurableApplicationContext}. |
| 213 | + */ |
| 214 | + public interface Creator extends |
| 215 | + Function<ConfigurableApplicationContext, ServiceInstanceListSupplier> { |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Allows creating a {@link ServiceInstanceListSupplier} instance based on provided |
| 221 | + * {@link ConfigurableApplicationContext} and another |
| 222 | + * {@link ServiceInstanceListSupplier} instance that will be used as a delegate. |
| 223 | + */ |
| 224 | + public interface DelegateCreator extends |
| 225 | + BiFunction<ConfigurableApplicationContext, ServiceInstanceListSupplier, ServiceInstanceListSupplier> { |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments