您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Spring Cloud Ribbon原理的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
簡(jiǎn)介
Spring Cloud Ribbon 是一個(gè)基于Http和TCP的客服端負(fù)載均衡工具,它是基于Netflix Ribbon實(shí)現(xiàn)的。它不像服務(wù)注冊(cè)中心、配置中心、API網(wǎng)關(guān)那樣獨(dú)立部署,但是它幾乎存在于每個(gè)微服務(wù)的基礎(chǔ)設(shè)施中。包括前面的提供的聲明式服務(wù)調(diào)用也是基于該Ribbon實(shí)現(xiàn)的。理解Ribbon對(duì)于我們使用Spring Cloud來(lái)講非常的重要,因?yàn)樨?fù)載均衡是對(duì)系統(tǒng)的高可用、網(wǎng)絡(luò)壓力的緩解和處理能力擴(kuò)容的重要手段之一。在上節(jié)的例子中,我們采用了聲明式的方式來(lái)實(shí)現(xiàn)負(fù)載均衡。實(shí)際上,內(nèi)部調(diào)用維護(hù)了一個(gè)RestTemplate對(duì)象,該對(duì)象會(huì)使用Ribbon的自動(dòng)化配置,同時(shí)通過(guò)@LoadBalanced開啟客戶端負(fù)載均衡。其實(shí)RestTemplate是Spring自己提供的對(duì)象,不是新的內(nèi)容。讀者不知道RestTemplate可以查看相關(guān)的文檔。
現(xiàn)象
前兩天碰到一個(gè)ribbon相關(guān)的問(wèn)題,覺得值得記錄一下。表象是對(duì)外的接口返回內(nèi)部異常,這個(gè)是封裝的統(tǒng)
一錯(cuò)誤信息,Spring的異常處理器catch到未捕獲異常統(tǒng)一返回的信息。因此到日志平臺(tái)查看實(shí)際的異常:
org.springframework.web.client.HttpClientErrorException: 404 null
這里介紹一下背景,出現(xiàn)問(wèn)題的開放網(wǎng)關(guān),做點(diǎn)事情說(shuō)白了就是轉(zhuǎn)發(fā)對(duì)應(yīng)的請(qǐng)求給后端的服務(wù)。這里用到了ribbon去做服務(wù)負(fù)載均衡、eureka負(fù)責(zé)服務(wù)發(fā)現(xiàn)。
這里出現(xiàn)404,首先看了下請(qǐng)求的url以及對(duì)應(yīng)的參數(shù),都沒有發(fā)現(xiàn)問(wèn)題,對(duì)應(yīng)的后端服務(wù)也沒有收到請(qǐng)求。這就比較詭異了,開始懷疑是ribbon或者Eureka的緩存導(dǎo)致請(qǐng)求到了錯(cuò)誤的ip或端口,但由于日志中打印的是Eureka的serviceId而不是實(shí)際的ip:port,因此先加了個(gè)日志:
@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); } }
這里是通過(guò)給RestTemplate添加攔截器的方式,但要注意,ribbon也是通過(guò)給RestTemplate添加攔截器實(shí)現(xiàn)的解析serviceId到實(shí)際的ip:port,因此需要注意下優(yōu)先級(jí)添加到ribbon的 LoadBalancerInterceptor 之后,我這里是通過(guò)Spring的初始化完成事件的回調(diào)中添加的,另外也添加了另一條日志,在catch到這個(gè)異常的時(shí)候,利用Eureka的 DiscoveryClient#getInstances 獲取到當(dāng)前的實(shí)例信息。
之后在測(cè)試環(huán)境中復(fù)現(xiàn)了這個(gè)問(wèn)題,看了下日志,eurek中緩存的實(shí)例信息是對(duì)的,但是實(shí)際調(diào)用的確實(shí)另外一個(gè)服務(wù)的地址,從而導(dǎo)致了接口404。
源碼解析
從上述的信息中可以知道,問(wèn)題出在ribbon中,具體的原因后面會(huì)說(shuō),這里先講一下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 { }
注意這個(gè)注解 @RibbonClients , 如果想要覆蓋Spring Cloud提供的默認(rèn)Ribbon配置就可以使用這個(gè)注解,最終的解析類是:
public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { Map<String, Object> attrs = 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<String, Object> client = metadata.getAnnotationAttributes( RibbonClient.class.getName(), true); String name = getClientName(client); if (name != null) { registerClientConfiguration(registry, name, client.get("configuration")); } } private String getClientName(Map<String, Object> 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,因此會(huì)注冊(cè)RibbonClientSpecification類型的bean,注意名稱以 default. 開頭,類型是RibbonAutoConfiguration,注意上面說(shuō)的RibbonAutoConfiguration被@RibbonClients修飾。
然后再回到上面的源碼:
public class RibbonAutoConfiguration { //上文中會(huì)解析被@RibbonClients注解修飾的類,然后注冊(cè)類型為RibbonClientSpecification的bean。 //主要有兩個(gè): RibbonAutoConfiguration、RibbonEurekaAutoConfiguration @Autowired(required = false) private List<RibbonClientSpecification> configurations = new ArrayList<>(); @Bean public SpringClientFactory springClientFactory() { //初始化SpringClientFactory,并將上面的配置注入進(jìn)去,這段很重要。 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)情況下,每個(gè)eureka的serviceId(服務(wù)),都會(huì)分配自己獨(dú)立的Spring的上下文,即ApplicationContext, 然后這個(gè)上下文中包含了必要的一些bean,比如: ILoadBalancer 、 ServerListFilter 等。而Spring Cloud默認(rèn)是使用RestTemplate封裝了ribbon的調(diào)用,核心是通過(guò)一個(gè)攔截器:
@Bean @ConditionalOnMissingBean public RestTemplateCustomizer restTemplateCustomizer( final LoadBalancerInterceptor loadBalancerInterceptor) { return new RestTemplateCustomizer() { @Override public void customize(RestTemplate restTemplate) { List<ClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); } }; }
因此核心是通過(guò)這個(gè)攔截器實(shí)現(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(); //解析拿到對(duì)應(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)); } }
然后將請(qǐng)求轉(zhuǎn)發(fā)給LoadBalancerClient:
public class RibbonLoadBalancerClient implements LoadBalancerClient { @Override public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException { ILoadBalancer loadBalancer = getLoadBalancer(serviceId); //獲取對(duì)應(yīng)的LoadBalancer Server server = getServer(loadBalancer); //獲取服務(wù)器,這里會(huì)執(zhí)行對(duì)應(yīng)的分流策略,比如輪訓(xùn) //、隨機(jī)等 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是通過(guò)上文中提到的SpringClientFactory獲取到的,這里會(huì)初始化一個(gè)新的Spring上下文,然后將Ribbon默認(rèn)的配置類,比如說(shuō): RibbonAutoConfiguration 、 RibbonEurekaAutoConfiguration 等添加進(jìn)去, 然后將當(dāng)前spring的上下文設(shè)置為parent,再調(diào)用refresh方法進(jìn)行初始化。
public class SpringClientFactory extends NamedContextFactory<RibbonClientSpecification> { 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<String, C> 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.<String, Object> singletonMap(this.propertyName, name))); if (this.parent != null) { // Uses Environment from parent as well as beans context.setParent(this.parent); } context.refresh(); return context; } }
最核心的就在這一段,也就是說(shuō)對(duì)于每一個(gè)不同的serviceId來(lái)說(shuō),都擁有一個(gè)獨(dú)立的spring上下文,并且在第一次調(diào)用這個(gè)服務(wù)的時(shí)候,會(huì)初始化ribbon相關(guān)的所有bean, 如果不存在 才回去父context中去找。
再回到上文中根據(jù)分流策略獲取實(shí)際的ip:port的代碼段:
public class RibbonLoadBalancerClient implements LoadBalancerClient { @Override public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException { ILoadBalancer loadBalancer = getLoadBalancer(serviceId); //獲取對(duì)應(yīng)的LoadBalancer Server server = getServer(loadBalancer); //獲取服務(wù)器,這里會(huì)執(zhí)行對(duì)應(yīng)的分流策略,比如輪訓(xùn) //、隨機(jī)等 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; } // 選擇對(duì)應(yīng)的服務(wù)器 return loadBalancer.chooseServer("default"); // TODO: better handling of key } public class ZoneAwareLoadBalancer<T extends Server> extends DynamicServerListLoadBalancer<T> { @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<String, ZoneSnapshot> 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<String> 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); } } //實(shí)際走到的方法 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; } } } }
也就是說(shuō)最終會(huì)調(diào)用 IRule 選擇到一個(gè)節(jié)點(diǎn),這里支持很多策略,比如隨機(jī)、輪訓(xùn)、響應(yīng)時(shí)間權(quán)重等:
public interface IRule{ public Server choose(Object key); public void setLoadBalancer(ILoadBalancer lb); public ILoadBalancer getLoadBalancer(); }
這里的LoadBalancer是在BaseLoadBalancer的構(gòu)造器中設(shè)置的,上文說(shuō)過(guò),對(duì)于每一個(gè)serviceId服務(wù)來(lái)說(shuō),當(dāng)?shù)谝淮握{(diào)用的時(shí)候會(huì)初始化對(duì)應(yīng)的spring上下文,而這個(gè)上下文中包含了所有ribbon相關(guān)的bean,其中就包括ILoadBalancer、IRule。
原因
通過(guò)跟蹤堆棧,發(fā)現(xiàn)不同的serviceId,IRule是同一個(gè), 而上文說(shuō)過(guò),每個(gè)serviceId都擁有自己獨(dú)立的上下文,包括獨(dú)立的loadBalancer、IRule,而IRule是同一個(gè),因此懷疑是這個(gè)bean是通過(guò)parent context獲取到的,換句話說(shuō)應(yīng)用自己定義了一個(gè)這樣的bean。查看代碼果然如此。
這樣就會(huì)導(dǎo)致一個(gè)問(wèn)題,IRule是共享的,而其他bean是隔離開的,因此后面的serviceId初始化的時(shí)候,會(huì)修改這個(gè)IRule的LoadBalancer, 導(dǎo)致之前的服務(wù)獲取到的實(shí)例信息是錯(cuò)誤的,從而導(dǎo)致接口404。
public class BaseLoadBalancer extends AbstractLoadBalancer implements PrimeConnections.PrimeConnectionListener, IClientConfigAware { public BaseLoadBalancer() { this.name = DEFAULT_NAME; this.ping = null; setRule(DEFAULT_RULE); // 這里會(huì)設(shè)置IRule的loadbalancer setupPingTask(); lbStats = new LoadBalancerStats(DEFAULT_NAME); } }
解決方案
解決方法也很簡(jiǎn)單,最簡(jiǎn)單就將這個(gè)自定義的IRule的bean干掉,另外更標(biāo)準(zhǔn)的做法是使用RibbonClients注解,具體做法可以參考文檔。
關(guān)于“Spring Cloud Ribbon原理的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。