Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the FeignLoadBalancer of spring cloud

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use FeignLoadBalancer of spring cloud". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use spring cloud's FeignLoadBalancer.

Order

This paper mainly studies the FeignLoadBalancer of spring cloud.

FeignLoadBalancer

SpringMurray cloudlyopenfeignMuvewaycoreMuray 2.2.0.M1ripsources.jarroomUniverse orgActionSpringframeworkGreater cloudGrease openFefeignOnRibbon Greater FeignLoadBalancer.Java

Public class FeignLoadBalancer extends AbstractLoadBalancerAwareClient {private final RibbonProperties ribbon; protected int connectTimeout; protected int readTimeout; protected IClientConfig clientConfig; protected ServerIntrospector serverIntrospector; public FeignLoadBalancer (ILoadBalancer lb, IClientConfig clientConfig, ServerIntrospector serverIntrospector) {super (lb, clientConfig); this.setRetryHandler (RetryHandler.DEFAULT); this.clientConfig = clientConfig This.ribbon = RibbonProperties.from (clientConfig); RibbonProperties ribbon = this.ribbon; this.connectTimeout = ribbon.getConnectTimeout (); this.readTimeout = ribbon.getReadTimeout (); this.serverIntrospector = serverIntrospector } @ Override public RibbonResponse execute (RibbonRequest request, IClientConfig configOverride) throws IOException {Request.Options options; if (configOverride! = null) {RibbonProperties override = RibbonProperties.from (configOverride) Options = new Request.Options (override.connectTimeout (this.connectTimeout), override.readTimeout (this.readTimeout));} else {options = new Request.Options (this.connectTimeout, this.readTimeout) } Response response = request.client () .execute (request.toRequest (), options); return new RibbonResponse (request.getUri (), response) } @ Override public RequestSpecificRetryHandler getRequestSpecificRetryHandler (RibbonRequest request, IClientConfig requestConfig) {if (this.ribbon.isOkToRetryOnAllOperations ()) {return new RequestSpecificRetryHandler (true, true, this.getRetryHandler (), requestConfig) } if (! request.toRequest () .httpMethod () .name () .equals ("GET")) {return new RequestSpecificRetryHandler (true, false, this.getRetryHandler (), requestConfig) } else {return new RequestSpecificRetryHandler (true, true, this.getRetryHandler (), requestConfig) } @ Override public URI reconstructURIWithServer (Server server, URI original) {URI uri = updateToSecureConnectionIfNeeded (original, this.clientConfig, this.serverIntrospector, server); return super.reconstructURIWithServer (server, uri);} /.}

FeignLoadBalancer inherits AbstractLoadBalancerAwareClient. Its constructor receives ILoadBalancer, IClientConfig, ServerIntrospector, and sets the retryHandler to RetryHandler.DEFAULT.

Its execute method first constructs Request.Options, then obtains Response through request.client (). Execute, and finally returns RibbonResponse

FeignLoadBalancer also overrides the getRequestSpecificRetryHandler method, building different RequestSpecificRetryHandler; for ribbon.isOkToRetryOnAllOperations () and overriding the reconstructURIWithServer method, which uses RibbonUtils's updateToSecureConnectionIfNeeded to build URI

RibbonRequest

SpringMurray cloudlyopenfeignMuvewaycoreMuray 2.2.0.M1ripsources.jarroomUniverse orgActionSpringframeworkGreater cloudGrease openFefeignOnRibbon Greater FeignLoadBalancer.Java

Protected static class RibbonRequest extends ClientRequest implements Cloneable {private final Request request; private final Client client; protected RibbonRequest (Client client, Request request, URI uri) {this.client = client; setUri (uri); this.request = toRequest (request) } private Request toRequest (Request request) {Map headers = new LinkedHashMap (request.headers ()); return Request.create (request.httpMethod (), getUri (). ToASCIIString (), headers, request.requestBody ()) } Request toRequest () {return toRequest (this.request);} Client client () {return this.client } HttpRequest toHttpRequest () {return new HttpRequest () {@ Override public HttpMethod getMethod () {return HttpMethod RibbonRequest.this.toRequest (). HttpMethod (). Name () } @ Override public String getMethodValue () {return getMethod () .name () } @ Override public URI getURI () {return RibbonRequest.this.getUri () } @ Override public HttpHeaders getHeaders () {Map headers = new HashMap () Map feignHeaders = RibbonRequest.this .toRequest () .headers () For (String key: feignHeaders.keySet ()) {headers.put (key, new ArrayList (feignHeaders.get (key);} HttpHeaders httpHeaders = new HttpHeaders () HttpHeaders.putAll (headers); return httpHeaders;}};} public Request getRequest () {return this.request } public Client getClient () {return this.client;} @ Override public Object clone () {return new RibbonRequest (this.client, this.request, getUri ());}}

RibbonRequest inherits ClientRequest and implements the Cloneable interface, which provides a toHttpRequest method to convert feign's Request to spring's HttpRequest.

RibbonResponse

SpringMurray cloudlyopenfeignMuvewaycoreMuray 2.2.0.M1ripsources.jarroomUniverse orgActionSpringframeworkGreater cloudGrease openFefeignOnRibbon Greater FeignLoadBalancer.Java

Protected static class RibbonResponse implements IResponse {private final URI uri; private final Response response; protected RibbonResponse (URI uri, Response response) {this.uri = uri; this.response = response } @ Override public Object getPayload () throws ClientException {return this.response.body ();} @ Override public boolean hasPayload () {return this.response.body ()! = null } @ Override public boolean isSuccess () {return this.response.status () = = 200;} @ Override public URI getRequestedURI () {return this.uri } @ Override public Map getHeaders () {return this.response.headers ();} Response toResponse () {return this.response } @ Override public void close () throws IOException {if (this.response! = null & & this.response.body ()! = null) {this.response.body () .close ();}

RibbonResponse implements IResponse interface, adapting Response of feign to IResponse of netflix.

Summary

FeignLoadBalancer inherits AbstractLoadBalancerAwareClient. Its constructor receives ILoadBalancer, IClientConfig, ServerIntrospector, and sets the retryHandler to RetryHandler.DEFAULT.

Its execute method first constructs Request.Options, then obtains Response through request.client (). Execute, and finally returns RibbonResponse

FeignLoadBalancer also overrides the getRequestSpecificRetryHandler method, building different RequestSpecificRetryHandler; for ribbon.isOkToRetryOnAllOperations () and overriding the reconstructURIWithServer method, which uses RibbonUtils's updateToSecureConnectionIfNeeded to build URI

At this point, I believe you have a deeper understanding of "how to use the FeignLoadBalancer of spring cloud". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report