Class StickyEndpointSelectionStrategy
- java.lang.Object
-
- com.linecorp.armeria.client.endpoint.StickyEndpointSelectionStrategy
-
- All Implemented Interfaces:
EndpointSelectionStrategy
public final class StickyEndpointSelectionStrategy extends Object implements EndpointSelectionStrategy
AnEndpointSelector
strategy which implements sticky load-balancing using user passedToLongFunction
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);
-
-
Field Summary
-
Fields inherited from interface com.linecorp.armeria.client.endpoint.EndpointSelectionStrategy
ROUND_ROBIN, WEIGHTED_ROUND_ROBIN
-
-
Constructor Summary
Constructors Constructor Description StickyEndpointSelectionStrategy(ToLongFunction<ClientRequestContext> requestContextHasher)
Creates a newStickyEndpointSelectionStrategy
with provided hash function to hash aClientRequestContext
to along
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EndpointSelector
newSelector(EndpointGroup endpointGroup)
Creates a newStickyEndpointSelectionStrategy.StickyEndpointSelector
.
-
-
-
Constructor Detail
-
StickyEndpointSelectionStrategy
public StickyEndpointSelectionStrategy(ToLongFunction<ClientRequestContext> requestContextHasher)
Creates a newStickyEndpointSelectionStrategy
with provided hash function to hash aClientRequestContext
to along
.- Parameters:
requestContextHasher
- The defaultToLongFunction
ofClientRequestContext
-
-
Method Detail
-
newSelector
public EndpointSelector newSelector(EndpointGroup endpointGroup)
Creates a newStickyEndpointSelectionStrategy.StickyEndpointSelector
.- Specified by:
newSelector
in interfaceEndpointSelectionStrategy
- Parameters:
endpointGroup
- anEndpointGroup
- Returns:
- a new
StickyEndpointSelectionStrategy.StickyEndpointSelector
-
-