這篇文章將為大家詳細(xì)講解有關(guān)Spring Cloud Ribbon原理的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
10余年的橋東網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整橋東建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“橋東網(wǎng)站設(shè)計”,“橋東網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。簡介
Spring Cloud Ribbon 是一個基于Http和TCP的客服端負(fù)載均衡工具,它是基于Netflix Ribbon實現(xiàn)的。它不像服務(wù)注冊中心、配置中心、API網(wǎng)關(guān)那樣獨立部署,但是它幾乎存在于每個微服務(wù)的基礎(chǔ)設(shè)施中。包括前面的提供的聲明式服務(wù)調(diào)用也是基于該Ribbon實現(xiàn)的。理解Ribbon對于我們使用Spring Cloud來講非常的重要,因為負(fù)載均衡是對系統(tǒng)的高可用、網(wǎng)絡(luò)壓力的緩解和處理能力擴容的重要手段之一。在上節(jié)的例子中,我們采用了聲明式的方式來實現(xiàn)負(fù)載均衡。實際上,內(nèi)部調(diào)用維護了一個RestTemplate對象,該對象會使用Ribbon的自動化配置,同時通過@LoadBalanced開啟客戶端負(fù)載均衡。其實RestTemplate是Spring自己提供的對象,不是新的內(nèi)容。讀者不知道RestTemplate可以查看相關(guān)的文檔。
現(xiàn)象
前兩天碰到一個ribbon相關(guān)的問題,覺得值得記錄一下。表象是對外的接口返回內(nèi)部異常,這個是封裝的統(tǒng)
一錯誤信息,Spring的異常處理器catch到未捕獲異常統(tǒng)一返回的信息。因此到日志平臺查看實際的異常:
org.springframework.web.client.HttpClientErrorException: 404 null
這里介紹一下背景,出現(xiàn)問題的開放網(wǎng)關(guān),做點事情說白了就是轉(zhuǎn)發(fā)對應(yīng)的請求給后端的服務(wù)。這里用到了ribbon去做服務(wù)負(fù)載均衡、eureka負(fù)責(zé)服務(wù)發(fā)現(xiàn)。
這里出現(xiàn)404,首先看了下請求的url以及對應(yīng)的參數(shù),都沒有發(fā)現(xiàn)問題,對應(yīng)的后端服務(wù)也沒有收到請求。這就比較詭異了,開始懷疑是ribbon或者Eureka的緩存導(dǎo)致請求到了錯誤的ip或端口,但由于日志中打印的是Eureka的serviceId而不是實際的ip:port,因此先加了個日志:
@Slf4j public class CustomHttpRequestInterceptor implements ClientHttpRequestInterceptor { @Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { log.info("Request , url:{},method:{}.", request.getURI(), request.getMethod()); return execution.execute(request, body); } }
這里是通過給RestTemplate添加攔截器的方式,但要注意,ribbon也是通過給RestTemplate添加攔截器實現(xiàn)的解析serviceId到實際的ip:port,因此需要注意下優(yōu)先級添加到ribbon的 LoadBalancerInterceptor 之后,我這里是通過Spring的初始化完成事件的回調(diào)中添加的,另外也添加了另一條日志,在catch到這個異常的時候,利用Eureka的 DiscoveryClient#getInstances 獲取到當(dāng)前的實例信息。
之后在測試環(huán)境中復(fù)現(xiàn)了這個問題,看了下日志,eurek中緩存的實例信息是對的,但是實際調(diào)用的確實另外一個服務(wù)的地址,從而導(dǎo)致了接口404。
源碼解析
從上述的信息中可以知道,問題出在ribbon中,具體的原因后面會說,這里先講一下Spring Cloud Ribbon的初始化流程。
@Configuration @ConditionalOnClass({ IClient.class, RestTemplate.class, AsyncRestTemplate.class, Ribbon.class}) @RibbonClients @AutoConfigureAfter(name = "org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration") @AutoConfigureBefore({LoadBalancerAutoConfiguration.class, AsyncLoadBalancerAutoConfiguration.class}) @EnableConfigurationProperties({RibbonEagerLoadProperties.class, ServerIntrospectorProperties.class}) public class RibbonAutoConfiguration { }
注意這個注解 @RibbonClients , 如果想要覆蓋Spring Cloud提供的默認(rèn)Ribbon配置就可以使用這個注解,最終的解析類是:
public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { Mapattrs = metadata.getAnnotationAttributes( RibbonClients.class.getName(), true); if (attrs != null && attrs.containsKey("value")) { AnnotationAttributes[] clients = (AnnotationAttributes[]) attrs.get("value"); for (AnnotationAttributes client : clients) { registerClientConfiguration(registry, getClientName(client), client.get("configuration")); } } if (attrs != null && attrs.containsKey("defaultConfiguration")) { String name; if (metadata.hasEnclosingClass()) { name = "default." + metadata.getEnclosingClassName(); } else { name = "default." + metadata.getClassName(); } registerClientConfiguration(registry, name, attrs.get("defaultConfiguration")); } Map client = metadata.getAnnotationAttributes( RibbonClient.class.getName(), true); String name = getClientName(client); if (name != null) { registerClientConfiguration(registry, name, client.get("configuration")); } } private String getClientName(Map client) { if (client == null) { return null; } String value = (String) client.get("value"); if (!StringUtils.hasText(value)) { value = (String) client.get("name"); } if (StringUtils.hasText(value)) { return value; } throw new IllegalStateException( "Either 'name' or 'value' must be provided in @RibbonClient"); } private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object configuration) { BeanDefinitionBuilder builder = BeanDefinitionBuilder .genericBeanDefinition(RibbonClientSpecification.class); builder.addConstructorArgValue(name); builder.addConstructorArgValue(configuration); registry.registerBeanDefinition(name + ".RibbonClientSpecification", builder.getBeanDefinition()); } }
atrrs包含defaultConfiguration,因此會注冊RibbonClientSpecification類型的bean,注意名稱以 default. 開頭,類型是RibbonAutoConfiguration,注意上面說的RibbonAutoConfiguration被@RibbonClients修飾。
然后再回到上面的源碼:
public class RibbonAutoConfiguration { //上文中會解析被@RibbonClients注解修飾的類,然后注冊類型為RibbonClientSpecification的bean。 //主要有兩個: RibbonAutoConfiguration、RibbonEurekaAutoConfiguration @Autowired(required = false) private Listconfigurations = new ArrayList<>(); @Bean public SpringClientFactory springClientFactory() { //初始化SpringClientFactory,并將上面的配置注入進去,這段很重要。 SpringClientFactory factory = new SpringClientFactory(); factory.setConfigurations(this.configurations); return factory; } //其他的都是提供一些默認(rèn)的bean配置 @Bean @ConditionalOnMissingBean(LoadBalancerClient.class) public LoadBalancerClient loadBalancerClient() { return new RibbonLoadBalancerClient(springClientFactory()); } @Bean @ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate") @ConditionalOnMissingBean public LoadBalancedRetryPolicyFactory loadBalancedRetryPolicyFactory(SpringClientFactory clientFactory) { return new RibbonLoadBalancedRetryPolicyFactory(clientFactory); } @Bean @ConditionalOnMissingClass(value = "org.springframework.retry.support.RetryTemplate") @ConditionalOnMissingBean public LoadBalancedRetryPolicyFactory neverRetryPolicyFactory() { return new LoadBalancedRetryPolicyFactory.NeverRetryFactory(); } @Bean @ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate") @ConditionalOnMissingBean public LoadBalancedBackOffPolicyFactory loadBalancedBackoffPolicyFactory() { return new LoadBalancedBackOffPolicyFactory.NoBackOffPolicyFactory(); } @Bean @ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate") @ConditionalOnMissingBean public LoadBalancedRetryListenerFactory loadBalancedRetryListenerFactory() { return new LoadBalancedRetryListenerFactory.DefaultRetryListenerFactory(); } @Bean @ConditionalOnMissingBean public PropertiesFactory propertiesFactory() { return new PropertiesFactory(); } @Bean @ConditionalOnProperty(value = "ribbon.eager-load.enabled", matchIfMissing = false) public RibbonApplicationContextInitializer ribbonApplicationContextInitializer() { return new RibbonApplicationContextInitializer(springClientFactory(), ribbonEagerLoadProperties.getClients()); } @Configuration @ConditionalOnClass(HttpRequest.class) @ConditionalOnRibbonRestClient protected static class RibbonClientConfig { @Autowired private SpringClientFactory springClientFactory; @Bean public RestTemplateCustomizer restTemplateCustomizer( final RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory) { return new RestTemplateCustomizer() { @Override public void customize(RestTemplate restTemplate) { restTemplate.setRequestFactory(ribbonClientHttpRequestFactory); } }; } @Bean public RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory() { return new RibbonClientHttpRequestFactory(this.springClientFactory); } } //TODO: support for autoconfiguring restemplate to use apache http client or okhttp @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(OnRibbonRestClientCondition.class) @interface ConditionalOnRibbonRestClient { } private static class OnRibbonRestClientCondition extends AnyNestedCondition { public OnRibbonRestClientCondition() { super(ConfigurationPhase.REGISTER_BEAN); } @Deprecated //remove in Edgware" @ConditionalOnProperty("ribbon.http.client.enabled") static class ZuulProperty {} @ConditionalOnProperty("ribbon.restclient.enabled") static class RibbonProperty {} } }
注意這里的SpringClientFactory, ribbon默認(rèn)情況下,每個eureka的serviceId(服務(wù)),都會分配自己獨立的Spring的上下文,即ApplicationContext, 然后這個上下文中包含了必要的一些bean,比如: ILoadBalancer 、 ServerListFilter 等。而Spring Cloud默認(rèn)是使用RestTemplate封裝了ribbon的調(diào)用,核心是通過一個攔截器:
@Bean @ConditionalOnMissingBean public RestTemplateCustomizer restTemplateCustomizer( final LoadBalancerInterceptor loadBalancerInterceptor) { return new RestTemplateCustomizer() { @Override public void customize(RestTemplate restTemplate) { Listlist = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); } }; }
因此核心是通過這個攔截器實現(xiàn)的負(fù)載均衡:
public class LoadBalancerInterceptor implements ClientHttpRequestInterceptor { private LoadBalancerClient loadBalancer; private LoadBalancerRequestFactory requestFactory; @Override public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException { final URI originalUri = request.getURI(); //這里傳入的url是解析之前的,即http://serviceId/服務(wù)地址的形式 String serviceName = originalUri.getHost(); //解析拿到對應(yīng)的serviceId Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri); return this.loadBalancer.execute(serviceName, requestFactory.createRequest(request, body, execution)); } }
然后將請求轉(zhuǎn)發(fā)給LoadBalancerClient:
public class RibbonLoadBalancerClient implements LoadBalancerClient { @Override publicT execute(String serviceId, LoadBalancerRequest request) throws IOException { ILoadBalancer loadBalancer = getLoadBalancer(serviceId); //獲取對應(yīng)的LoadBalancer Server server = getServer(loadBalancer); //獲取服務(wù)器,這里會執(zhí)行對應(yīng)的分流策略,比如輪訓(xùn) //、隨機等 if (server == null) { throw new IllegalStateException("No instances available for " + serviceId); } RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server, serviceId), serverIntrospector(serviceId).getMetadata(server)); return execute(serviceId, ribbonServer, request); } }
而這里的LoadBalancer是通過上文中提到的SpringClientFactory獲取到的,這里會初始化一個新的Spring上下文,然后將Ribbon默認(rèn)的配置類,比如說: RibbonAutoConfiguration 、 RibbonEurekaAutoConfiguration 等添加進去, 然后將當(dāng)前spring的上下文設(shè)置為parent,再調(diào)用refresh方法進行初始化。
public class SpringClientFactory extends NamedContextFactory{ protected AnnotationConfigApplicationContext createContext(String name) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); if (this.configurations.containsKey(name)) { for (Class> configuration : this.configurations.get(name) .getConfiguration()) { context.register(configuration); } } for (Map.Entry entry : this.configurations.entrySet()) { if (entry.getKey().startsWith("default.")) { for (Class> configuration : entry.getValue().getConfiguration()) { context.register(configuration); } } } context.register(PropertyPlaceholderAutoConfiguration.class, this.defaultConfigType); context.getEnvironment().getPropertySources().addFirst(new MapPropertySource( this.propertySourceName, Collections. singletonMap(this.propertyName, name))); if (this.parent != null) { // Uses Environment from parent as well as beans context.setParent(this.parent); } context.refresh(); return context; } }
最核心的就在這一段,也就是說對于每一個不同的serviceId來說,都擁有一個獨立的spring上下文,并且在第一次調(diào)用這個服務(wù)的時候,會初始化ribbon相關(guān)的所有bean, 如果不存在 才回去父context中去找。
再回到上文中根據(jù)分流策略獲取實際的ip:port的代碼段:
public class RibbonLoadBalancerClient implements LoadBalancerClient { @Override publicT execute(String serviceId, LoadBalancerRequest request) throws IOException { ILoadBalancer loadBalancer = getLoadBalancer(serviceId); //獲取對應(yīng)的LoadBalancer Server server = getServer(loadBalancer); //獲取服務(wù)器,這里會執(zhí)行對應(yīng)的分流策略,比如輪訓(xùn) //、隨機等 if (server == null) { throw new IllegalStateException("No instances available for " + serviceId); } RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server, serviceId), serverIntrospector(serviceId).getMetadata(server)); return execute(serviceId, ribbonServer, request); } }
protected Server getServer(ILoadBalancer loadBalancer) { if (loadBalancer == null) { return null; } // 選擇對應(yīng)的服務(wù)器 return loadBalancer.chooseServer("default"); // TODO: better handling of key } public class ZoneAwareLoadBalancerextends DynamicServerListLoadBalancer { @Override public Server chooseServer(Object key) { if (!ENABLED.get() || getLoadBalancerStats().getAvailableZones().size() <= 1) { logger.debug("Zone aware logic disabled or there is only one zone"); return super.chooseServer(key); //默認(rèn)不配置可用區(qū),走的是這段 } Server server = null; try { LoadBalancerStats lbStats = getLoadBalancerStats(); Map zoneSnapshot = ZoneAvoidanceRule.createSnapshot(lbStats); logger.debug("Zone snapshots: {}", zoneSnapshot); if (triggeringLoad == null) { triggeringLoad = DynamicPropertyFactory.getInstance().getDoubleProperty( "ZoneAwareNIWSDiscoveryLoadBalancer." + this.getName() + ".triggeringLoadPerServerThreshold", 0.2d); } if (triggeringBlackoutPercentage == null) { triggeringBlackoutPercentage = DynamicPropertyFactory.getInstance().getDoubleProperty( "ZoneAwareNIWSDiscoveryLoadBalancer." + this.getName() + ".avoidZoneWithBlackoutPercetage", 0.99999d); } Set availableZones = ZoneAvoidanceRule.getAvailableZones(zoneSnapshot, triggeringLoad.get(), triggeringBlackoutPercentage.get()); logger.debug("Available zones: {}", availableZones); if (availableZones != null && availableZones.size() < zoneSnapshot.keySet().size()) { String zone = ZoneAvoidanceRule.randomChooseZone(zoneSnapshot, availableZones); logger.debug("Zone chosen: {}", zone); if (zone != null) { BaseLoadBalancer zoneLoadBalancer = getLoadBalancer(zone); server = zoneLoadBalancer.chooseServer(key); } } } catch (Exception e) { logger.error("Error choosing server using zone aware logic for load balancer={}", name, e); } if (server != null) { return server; } else { logger.debug("Zone avoidance logic is not invoked."); return super.chooseServer(key); } } //實際走到的方法 public Server chooseServer(Object key) { if (counter == null) { counter = createCounter(); } counter.increment(); if (rule == null) { return null; } else { try { return rule.choose(key); } catch (Exception e) { logger.warn("LoadBalancer [{}]: Error choosing server for key {}", name, key, e); return null; } } } }
也就是說最終會調(diào)用 IRule 選擇到一個節(jié)點,這里支持很多策略,比如隨機、輪訓(xùn)、響應(yīng)時間權(quán)重等:
public interface IRule{ public Server choose(Object key); public void setLoadBalancer(ILoadBalancer lb); public ILoadBalancer getLoadBalancer(); }
這里的LoadBalancer是在BaseLoadBalancer的構(gòu)造器中設(shè)置的,上文說過,對于每一個serviceId服務(wù)來說,當(dāng)?shù)谝淮握{(diào)用的時候會初始化對應(yīng)的spring上下文,而這個上下文中包含了所有ribbon相關(guān)的bean,其中就包括ILoadBalancer、IRule。
原因
通過跟蹤堆棧,發(fā)現(xiàn)不同的serviceId,IRule是同一個, 而上文說過,每個serviceId都擁有自己獨立的上下文,包括獨立的loadBalancer、IRule,而IRule是同一個,因此懷疑是這個bean是通過parent context獲取到的,換句話說應(yīng)用自己定義了一個這樣的bean。查看代碼果然如此。
這樣就會導(dǎo)致一個問題,IRule是共享的,而其他bean是隔離開的,因此后面的serviceId初始化的時候,會修改這個IRule的LoadBalancer, 導(dǎo)致之前的服務(wù)獲取到的實例信息是錯誤的,從而導(dǎo)致接口404。
public class BaseLoadBalancer extends AbstractLoadBalancer implements PrimeConnections.PrimeConnectionListener, IClientConfigAware { public BaseLoadBalancer() { this.name = DEFAULT_NAME; this.ping = null; setRule(DEFAULT_RULE); // 這里會設(shè)置IRule的loadbalancer setupPingTask(); lbStats = new LoadBalancerStats(DEFAULT_NAME); } }
解決方案
解決方法也很簡單,最簡單就將這個自定義的IRule的bean干掉,另外更標(biāo)準(zhǔn)的做法是使用RibbonClients注解,具體做法可以參考文檔。
關(guān)于“Spring Cloud Ribbon原理的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。