public final class StickyEndpointSelectionStrategy extends Object implements EndpointSelectionStrategy
EndpointSelector strategy which implements sticky load-balancing using
user passed ToLongFunction to compute hashes for consistent hashing.
This strategy can be useful when all requests that qualify some given criterias must be sent to the same backend server. A common use case is to send all requests for the same logged-in user to the same backend, which could have a local cache keyed by user id.
In below example, created strategy will route all HttpRequest which have the same value for key
"cookie" of its header to the same server:
ToLongFunction<ClientRequestContext> hasher = (ClientRequestContext ctx) -> {
return ((HttpRequest) ctx.request()).headers().get(HttpHeaderNames.COOKIE).hashCode();
};
final StickyEndpointSelectionStrategy strategy = new StickyEndpointSelectionStrategy(hasher);
ROUND_ROBIN, WEIGHTED_ROUND_ROBIN| Constructor and Description |
|---|
StickyEndpointSelectionStrategy(ToLongFunction<ClientRequestContext> requestContextHasher)
Creates a new
StickyEndpointSelectionStrategy
with provided hash function to hash a ClientRequestContext to a long. |
| Modifier and Type | Method and Description |
|---|---|
EndpointSelector |
newSelector(EndpointGroup endpointGroup)
Creates a new
StickyEndpointSelector. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitroundRobin, weightedRoundRobinpublic StickyEndpointSelectionStrategy(ToLongFunction<ClientRequestContext> requestContextHasher)
StickyEndpointSelectionStrategy
with provided hash function to hash a ClientRequestContext to a long.requestContextHasher - The default ToLongFunction of ClientRequestContextpublic EndpointSelector newSelector(EndpointGroup endpointGroup)
StickyEndpointSelector.newSelector in interface EndpointSelectionStrategyendpointGroup - an EndpointGroupStickyEndpointSelectorCopyright © 2020 LeanCloud. All rights reserved.