/
LoadBalancerProperties.java
541 lines (424 loc) · 13.8 KB
/
LoadBalancerProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* Copyright 2012-2023 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.client.loadbalancer;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import reactor.util.retry.RetryBackoffSpec;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.core.env.PropertyResolver;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedCaseInsensitiveMap;
/**
* The base configuration bean for Spring Cloud LoadBalancer.
*
* See {@link LoadBalancerClientsProperties} for the {@link ConfigurationProperties}
* annotation.
*
* @author Olga Maciaszek-Sharma
* @author Gandhimathi Velusamy
* @author Zhuozhi Ji
* @since 2.2.1
*/
public class LoadBalancerProperties {
/**
* Properties for <code>HealthCheckServiceInstanceListSupplier</code>.
*/
private HealthCheck healthCheck = new HealthCheck();
/**
* Allows setting the value of <code>hint</code> that is passed on to the LoadBalancer
* request and can subsequently be used in {@link ReactiveLoadBalancer}
* implementations.
*/
private Map<String, String> hint = new LinkedCaseInsensitiveMap<>();
/**
* Allows setting the name of the header used for passing the hint for hint-based
* service instance filtering.
*/
private String hintHeaderName = "X-SC-LB-Hint";
/**
* Properties for Spring-Retry and Reactor Retry support in Spring Cloud LoadBalancer.
*/
private Retry retry = new Retry();
/**
* Properties for LoadBalancer sticky-session.
*/
private StickySession stickySession = new StickySession();
/**
* If this flag is set to {@code true},
* {@code ServiceInstanceListSupplier#get(Request request)} method will be implemented
* to call {@code delegate.get(request)} in classes assignable from
* {@code DelegatingServiceInstanceListSupplier} that don't already implement that
* method, with the exclusion of {@code CachingServiceInstanceListSupplier} and
* {@code HealthCheckServiceInstanceListSupplier}, which should be placed in the
* instance supplier hierarchy directly after the supplier performing instance
* retrieval over the network, before any request-based filtering is done,
* {@code true} by default.
*/
private boolean callGetWithRequestOnDelegates = true;
/**
* Properties for
* {@link org.springframework.cloud.loadbalancer.core.SubsetServiceInstanceListSupplier}.
*/
private Subset subset = new Subset();
public HealthCheck getHealthCheck() {
return healthCheck;
}
public void setHealthCheck(HealthCheck healthCheck) {
this.healthCheck = healthCheck;
}
public Map<String, String> getHint() {
return hint;
}
public void setHint(Map<String, String> hint) {
this.hint = hint;
}
public Retry getRetry() {
return retry;
}
public void setRetry(Retry retry) {
this.retry = retry;
}
public StickySession getStickySession() {
return stickySession;
}
public void setStickySession(StickySession stickySession) {
this.stickySession = stickySession;
}
public String getHintHeaderName() {
return hintHeaderName;
}
public void setHintHeaderName(String hintHeaderName) {
this.hintHeaderName = hintHeaderName;
}
/**
* Enabling X-Forwarded Host and Proto Headers.
*/
private XForwarded xForwarded = new XForwarded();
public void setxForwarded(XForwarded xForwarded) {
this.xForwarded = xForwarded;
}
public XForwarded getXForwarded() {
return xForwarded;
}
public boolean isCallGetWithRequestOnDelegates() {
return callGetWithRequestOnDelegates;
}
public Subset getSubset() {
return subset;
}
public void setSubset(Subset subset) {
this.subset = subset;
}
public void setCallGetWithRequestOnDelegates(boolean callGetWithRequestOnDelegates) {
this.callGetWithRequestOnDelegates = callGetWithRequestOnDelegates;
}
public static class StickySession {
/**
* The name of the cookie holding the preferred instance id.
*/
private String instanceIdCookieName = "sc-lb-instance-id";
/**
* Indicates whether a cookie with the newly selected instance should be added by
* SC LoadBalancer.
*/
private boolean addServiceInstanceCookie = false;
public String getInstanceIdCookieName() {
return instanceIdCookieName;
}
public void setInstanceIdCookieName(String instanceIdCookieName) {
this.instanceIdCookieName = instanceIdCookieName;
}
public boolean isAddServiceInstanceCookie() {
return addServiceInstanceCookie;
}
public void setAddServiceInstanceCookie(boolean addServiceInstanceCookie) {
this.addServiceInstanceCookie = addServiceInstanceCookie;
}
}
public static class XForwarded {
/**
* To Enable X-Forwarded Headers.
*/
private boolean enabled = false;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
public static class HealthCheck {
/**
* Initial delay value for the HealthCheck scheduler.
*/
private Duration initialDelay = Duration.ZERO;
/**
* Interval for rerunning the HealthCheck scheduler.
*/
private Duration interval = Duration.ofSeconds(25);
/**
* Interval for refetching available service instances.
*/
private Duration refetchInstancesInterval = Duration.ofSeconds(25);
/**
* Path at which the health-check request should be made. Can be set up per
* <code>serviceId</code>. A <code>default</code> value can be set up as well. If
* none is set up, <code>/actuator/health</code> will be used.
*/
private Map<String, String> path = new LinkedCaseInsensitiveMap<>();
/**
* Port at which the health-check request should be made. If none is set, the port
* under which the requested service is available at the service instance.
*/
private Integer port;
/**
* Indicates whether the instances should be refetched by the
* <code>HealthCheckServiceInstanceListSupplier</code>. This can be used if the
* instances can be updated and the underlying delegate does not provide an
* ongoing flux.
*/
private boolean refetchInstances = false;
/**
* Indicates whether health checks should keep repeating. It might be useful to
* set it to <code>false</code> if periodically refetching the instances, as every
* refetch will also trigger a healthcheck.
*/
private boolean repeatHealthCheck = true;
/**
* Indicates whether the {@code healthCheckFlux} should emit on each alive
* {@link ServiceInstance} that has been retrieved. If set to {@code false}, the
* entire alive instances sequence is first collected into a list and only then
* emitted.
*/
private boolean updateResultsList = true;
public boolean getRefetchInstances() {
return refetchInstances;
}
public void setRefetchInstances(boolean refetchInstances) {
this.refetchInstances = refetchInstances;
}
public boolean getRepeatHealthCheck() {
return repeatHealthCheck;
}
public void setRepeatHealthCheck(boolean repeatHealthCheck) {
this.repeatHealthCheck = repeatHealthCheck;
}
public Duration getInitialDelay() {
return initialDelay;
}
public void setInitialDelay(Duration initialDelay) {
this.initialDelay = initialDelay;
}
public Duration getRefetchInstancesInterval() {
return refetchInstancesInterval;
}
public void setRefetchInstancesInterval(Duration refetchInstancesInterval) {
this.refetchInstancesInterval = refetchInstancesInterval;
}
public Map<String, String> getPath() {
return path;
}
public void setPath(Map<String, String> path) {
this.path = path;
}
public Duration getInterval() {
return interval;
}
public void setInterval(Duration interval) {
this.interval = interval;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public boolean isUpdateResultsList() {
return updateResultsList;
}
public void setUpdateResultsList(boolean updateResultsList) {
this.updateResultsList = updateResultsList;
}
}
public static class Retry {
private boolean enabled = true;
/**
* Indicates retries should be attempted on operations other than
* {@link HttpMethod#GET}.
*/
private boolean retryOnAllOperations = false;
/**
* Indicates retries should be attempted for all exceptions, not only those
* specified in {@code retryableExceptions}.
*/
private boolean retryOnAllExceptions = false;
/**
* Number of retries to be executed on the same <code>ServiceInstance</code>.
*/
private int maxRetriesOnSameServiceInstance = 0;
/**
* Number of retries to be executed on the next <code>ServiceInstance</code>. A
* <code>ServiceInstance</code> is chosen before each retry call.
*/
private int maxRetriesOnNextServiceInstance = 1;
/**
* A {@link Set} of status codes that should trigger a retry.
*/
private Set<Integer> retryableStatusCodes = new HashSet<>();
/**
* A {@link Set} of {@link Throwable} classes that should trigger a retry.
*/
private Set<Class<? extends Throwable>> retryableExceptions = new HashSet<>(
Arrays.asList(IOException.class, TimeoutException.class, RetryableStatusCodeException.class,
org.springframework.cloud.client.loadbalancer.reactive.RetryableStatusCodeException.class));
/**
* Properties for Reactor Retry backoffs in Spring Cloud LoadBalancer.
*/
private Backoff backoff = new Backoff();
/**
* Returns true if the load balancer should retry failed requests.
* @return True if the load balancer should retry failed requests; false
* otherwise.
*/
public boolean isEnabled() {
return this.enabled;
}
/**
* Sets whether the load balancer should retry failed requests.
* @param enabled Whether the load balancer should retry failed requests.
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isRetryOnAllOperations() {
return retryOnAllOperations;
}
public void setRetryOnAllOperations(boolean retryOnAllOperations) {
this.retryOnAllOperations = retryOnAllOperations;
}
public int getMaxRetriesOnSameServiceInstance() {
return maxRetriesOnSameServiceInstance;
}
public void setMaxRetriesOnSameServiceInstance(int maxRetriesOnSameServiceInstance) {
this.maxRetriesOnSameServiceInstance = maxRetriesOnSameServiceInstance;
}
public int getMaxRetriesOnNextServiceInstance() {
return maxRetriesOnNextServiceInstance;
}
public void setMaxRetriesOnNextServiceInstance(int maxRetriesOnNextServiceInstance) {
this.maxRetriesOnNextServiceInstance = maxRetriesOnNextServiceInstance;
}
public Set<Integer> getRetryableStatusCodes() {
return retryableStatusCodes;
}
public void setRetryableStatusCodes(Set<Integer> retryableStatusCodes) {
this.retryableStatusCodes = retryableStatusCodes;
}
public Set<Class<? extends Throwable>> getRetryableExceptions() {
return retryableExceptions;
}
public void setRetryableExceptions(Set<Class<? extends Throwable>> retryableExceptions) {
retryableExceptions
.add(org.springframework.cloud.client.loadbalancer.reactive.RetryableStatusCodeException.class);
this.retryableExceptions = retryableExceptions;
}
public Backoff getBackoff() {
return backoff;
}
public void setBackoff(Backoff backoff) {
this.backoff = backoff;
}
public boolean isRetryOnAllExceptions() {
return retryOnAllExceptions;
}
public void setRetryOnAllExceptions(boolean retryOnAllExceptions) {
this.retryOnAllExceptions = retryOnAllExceptions;
}
public static class Backoff {
/**
* Indicates whether Reactor Retry backoffs should be applied.
*/
private boolean enabled = false;
/**
* Used to set {@link RetryBackoffSpec#minBackoff}.
*/
private Duration minBackoff = Duration.ofMillis(5);
/**
* Used to set {@link RetryBackoffSpec#maxBackoff}.
*/
private Duration maxBackoff = Duration.ofMillis(Long.MAX_VALUE);
/**
* Used to set {@link RetryBackoffSpec#jitter}.
*/
private double jitter = 0.5d;
public Duration getMinBackoff() {
return minBackoff;
}
public void setMinBackoff(Duration minBackoff) {
this.minBackoff = minBackoff;
}
public Duration getMaxBackoff() {
return maxBackoff;
}
public void setMaxBackoff(Duration maxBackoff) {
this.maxBackoff = maxBackoff;
}
public double getJitter() {
return jitter;
}
public void setJitter(double jitter) {
this.jitter = jitter;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
public static class Subset {
/**
* Instance id of deterministic subsetting. If not set,
* {@link IdUtils#getDefaultInstanceId(PropertyResolver)} will be used.
*/
private String instanceId = "";
/**
* Max subset size of deterministic subsetting.
*/
private int size = 100;
public String getInstanceId() {
return instanceId;
}
public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
}