十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“spring cloud的FeignLoadBalancer怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring cloud的FeignLoadBalancer怎么用”吧!
成都创新互联公司是一家集网站建设,广灵企业网站建设,广灵品牌网站建设,网站定制,广灵网站建设报价,网络营销,网络优化,广灵网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
本文主要研究一下spring cloud的FeignLoadBalancer
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/ribbon/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继承了AbstractLoadBalancerAwareClient,它的构造器接收ILoadBalancer、IClientConfig、ServerIntrospector,设置的retryHandler为RetryHandler.DEFAULT
其execute方法首先构造Request.Options,然后通过request.client().execute来获取Response,最后返回RibbonResponse
FeignLoadBalancer还覆盖了getRequestSpecificRetryHandler方法,针对ribbon.isOkToRetryOnAllOperations()来构建不同的RequestSpecificRetryHandler;还覆盖了reconstructURIWithServer方法,它使用RibbonUtils的updateToSecureConnectionIfNeeded来构建URI
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/ribbon/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 .resolve(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继承了ClientRequest实现了Cloneable接口,它提供了toHttpRequest方法来将feign的Request转换为spring的HttpRequest
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/ribbon/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实现了IResponse接口,将feign的Response适配为netflix的IResponse
FeignLoadBalancer继承了AbstractLoadBalancerAwareClient,它的构造器接收ILoadBalancer、IClientConfig、ServerIntrospector,设置的retryHandler为RetryHandler.DEFAULT
其execute方法首先构造Request.Options,然后通过request.client().execute来获取Response,最后返回RibbonResponse
FeignLoadBalancer还覆盖了getRequestSpecificRetryHandler方法,针对ribbon.isOkToRetryOnAllOperations()来构建不同的RequestSpecificRetryHandler;还覆盖了reconstructURIWithServer方法,它使用RibbonUtils的updateToSecureConnectionIfNeeded来构建URI
到此,相信大家对“spring cloud的FeignLoadBalancer怎么用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!