這篇文章主要講解了“spring cloud中CachingSpringLoadBalancerFactory的作用是什么”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“spring cloud中CachingSpringLoadBalancerFactory的作用是什么”吧!
我們一直強(qiáng)調(diào)成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)對(duì)于企業(yè)的重要性,如果您也覺(jué)得重要,那么就需要我們慎重對(duì)待,選擇一個(gè)安全靠譜的網(wǎng)站建設(shè)公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過(guò)程中的有力推手。專業(yè)的建站公司不一定是大公司,成都創(chuàng)新互聯(lián)作為專業(yè)的網(wǎng)絡(luò)公司選擇我們就是放心。
本文主要研究一下spring cloud的CachingSpringLoadBalancerFactory
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/ribbon/CachingSpringLoadBalancerFactory.java
public class CachingSpringLoadBalancerFactory { protected final SpringClientFactory factory; protected LoadBalancedRetryFactory loadBalancedRetryFactory = null; private volatile Mapcache = new ConcurrentReferenceHashMap<>(); public CachingSpringLoadBalancerFactory(SpringClientFactory factory) { this.factory = factory; } public CachingSpringLoadBalancerFactory(SpringClientFactory factory, LoadBalancedRetryFactory loadBalancedRetryPolicyFactory) { this.factory = factory; this.loadBalancedRetryFactory = loadBalancedRetryPolicyFactory; } public FeignLoadBalancer create(String clientName) { FeignLoadBalancer client = this.cache.get(clientName); if (client != null) { return client; } IClientConfig config = this.factory.getClientConfig(clientName); ILoadBalancer lb = this.factory.getLoadBalancer(clientName); ServerIntrospector serverIntrospector = this.factory.getInstance(clientName, ServerIntrospector.class); client = this.loadBalancedRetryFactory != null ? new RetryableFeignLoadBalancer(lb, config, serverIntrospector, this.loadBalancedRetryFactory) : new FeignLoadBalancer(lb, config, serverIntrospector); this.cache.put(clientName, client); return client; } }
CachingSpringLoadBalancerFactory的構(gòu)造器接收SpringClientFactory、LoadBalancedRetryFactory;它提供了create方法用于創(chuàng)建FeignLoadBalancer;其create方法使用ConcurrentReferenceHashMap來(lái)緩存每個(gè)client對(duì)應(yīng)的FeignLoadBalancer
spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/ribbon/LoadBalancerFeignClient.java
public class LoadBalancerFeignClient implements Client { static final Request.Options DEFAULT_OPTIONS = new Request.Options(); private final Client delegate; private CachingSpringLoadBalancerFactory lbClientFactory; private SpringClientFactory clientFactory; public LoadBalancerFeignClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory, SpringClientFactory clientFactory) { this.delegate = delegate; this.lbClientFactory = lbClientFactory; this.clientFactory = clientFactory; } static URI cleanUrl(String originalUrl, String host) { String newUrl = originalUrl; if (originalUrl.startsWith("https://")) { newUrl = originalUrl.substring(0, 8) + originalUrl.substring(8 + host.length()); } else if (originalUrl.startsWith("http")) { newUrl = originalUrl.substring(0, 7) + originalUrl.substring(7 + host.length()); } StringBuffer buffer = new StringBuffer(newUrl); if ((newUrl.startsWith("https://") && newUrl.length() == 8) || (newUrl.startsWith("http://") && newUrl.length() == 7)) { buffer.append("/"); } return URI.create(buffer.toString()); } @Override public Response execute(Request request, Request.Options options) throws IOException { try { URI asUri = URI.create(request.url()); String clientName = asUri.getHost(); URI uriWithoutHost = cleanUrl(request.url(), clientName); FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest( this.delegate, request, uriWithoutHost); IClientConfig requestConfig = getClientConfig(options, clientName); return lbClient(clientName) .executeWithLoadBalancer(ribbonRequest, requestConfig).toResponse(); } catch (ClientException e) { IOException io = findIOException(e); if (io != null) { throw io; } throw new RuntimeException(e); } } IClientConfig getClientConfig(Request.Options options, String clientName) { IClientConfig requestConfig; if (options == DEFAULT_OPTIONS) { requestConfig = this.clientFactory.getClientConfig(clientName); } else { requestConfig = new FeignOptionsClientConfig(options); } return requestConfig; } protected IOException findIOException(Throwable t) { if (t == null) { return null; } if (t instanceof IOException) { return (IOException) t; } return findIOException(t.getCause()); } public Client getDelegate() { return this.delegate; } private FeignLoadBalancer lbClient(String clientName) { return this.lbClientFactory.create(clientName); } static class FeignOptionsClientConfig extends DefaultClientConfigImpl { FeignOptionsClientConfig(Request.Options options) { setProperty(CommonClientConfigKey.ConnectTimeout, options.connectTimeoutMillis()); setProperty(CommonClientConfigKey.ReadTimeout, options.readTimeoutMillis()); } @Override public void loadProperties(String clientName) { } @Override public void loadDefaultValues() { } } }
LoadBalancerFeignClient的lbClient方法使用CachingSpringLoadBalancerFactory的create方法來(lái)創(chuàng)建FeignLoadBalancer;而execute方法則調(diào)用了lbClient方法來(lái)獲取FeignLoadBalancer
CachingSpringLoadBalancerFactory的構(gòu)造器接收SpringClientFactory、LoadBalancedRetryFactory;它提供了create方法用于創(chuàng)建FeignLoadBalancer;其create方法使用ConcurrentReferenceHashMap來(lái)緩存每個(gè)client對(duì)應(yīng)的FeignLoadBalancer
感謝各位的閱讀,以上就是“spring cloud中CachingSpringLoadBalancerFactory的作用是什么”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)spring cloud中CachingSpringLoadBalancerFactory的作用是什么這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!