事情的原由是这样的,公司内部要实现基于Zuul网关的灰度路由,在上线时进行灰度测试,故须要配置业务微服务向Eureka注册的metadata元数据,和自定义Ribbon的负载规则达到只访问灰度服务的目的。这样就须要自定义Ribbon的IRule,实现灰度请求只会负载到带有灰度标签元数据的业务微服务上,当自定义IRule规则开发好后,问题是如何将这个IRule规则配置给某个Ribbon Client或者全局生效。html
本次使用Spring Cloud Dalston.SR5版本java
在其 官方文档 中其实已经给出了一些如何针对某个Client 或者 修改默认配置的方式,但没有说明为何这样使用spring
下面将按照这样的思路分析:apache
当前版本中的Netflix全部自动配置都在spring-cloud-netflix-core-xxx.jar
中,根据其META-INF/spring.factories
中的配置得知,Spring Cloud Ribbon的自动配置类为 RibbonAutoConfiguration
数组
@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) public class RibbonAutoConfiguration { // 全部针对某个RibbonClient指定的配置 @Autowired(required = false) private List<RibbonClientSpecification> configurations = new ArrayList<>(); // ribbon是否懒加载的配置文件 @Autowired private RibbonEagerLoadProperties ribbonEagerLoadProperties; // Spring会给每一个RibbonClient建立独立的ApplicationContext上下文 // 并在其上下文中建立RibbonClient对应的Bean:如IClient、ILoadbalancer等 @Bean public SpringClientFactory springClientFactory() { SpringClientFactory factory = new SpringClientFactory(); factory.setConfigurations(this.configurations); return factory; } // Spring建立的带负载均衡功能的Client,会使用SpringClientFactory建立对应的Bean和配置 @Bean @ConditionalOnMissingBean(LoadBalancerClient.class) public LoadBalancerClient loadBalancerClient() { return new RibbonLoadBalancerClient(springClientFactory()); } // 到Spring environment中加载针对某个Client的Ribbon的核心接口实现类 @Bean @ConditionalOnMissingBean public PropertiesFactory propertiesFactory() { return new PropertiesFactory(); } // 若是不是懒加载,启动时就使用RibbonApplicationContextInitializer加载并初始化客户端配置 @Bean @ConditionalOnProperty(value = "ribbon.eager-load.enabled", matchIfMissing = false) public RibbonApplicationContextInitializer ribbonApplicationContextInitializer() { return new RibbonApplicationContextInitializer(springClientFactory(), ribbonEagerLoadProperties.getClients()); } ...... }
上面RibbonAutoConfiguration
建立的Bean主要分如下几类:app
RibbonLoadBalancerClient
,并将springClientFactory注入,方便从中获取对应的配置及实现类,RibbonLoadBalancerClient
是Spring对LoadBalancerClient
接口的实现类,其execute()
方法提供客户端负载均衡能力能够看到默认启动流程中并无加载RibbonClient的上下文和配置信息,而是在使用时才加载,即懒加载负载均衡
既然是在使用时才会加载,那么以Zuul网关为例,在其RibbonRoutingFilter
中会建立RibbonCommand,其包含了Ribbon的负载均衡ide
//## RibbonRoutingFilter Zuul负责路由的Filter public class RibbonRoutingFilter extends ZuulFilter { @Override public Object run() { RequestContext context = RequestContext.getCurrentContext(); this.helper.addIgnoredHeaders(); try { RibbonCommandContext commandContext = buildCommandContext(context); ClientHttpResponse response = forward(commandContext); setResponse(response); return response; } catch (ZuulException ex) { throw new ZuulRuntimeException(ex); } catch (Exception ex) { throw new ZuulRuntimeException(ex); } } protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception { Map<String, Object> info = this.helper.debug(context.getMethod(), context.getUri(), context.getHeaders(), context.getParams(), context.getRequestEntity()); // 使用ribbonCommandFactory建立RibbonCommand RibbonCommand command = this.ribbonCommandFactory.create(context); try { ClientHttpResponse response = command.execute(); this.helper.appendDebug(info, response.getStatusCode().value(), response.getHeaders()); return response; } catch (HystrixRuntimeException ex) { return handleException(info, ex); } } }
在执行RibbonRoutingFilter#run()
进行路由时会执行forward()
方法,因为此处是在HystrixCommand内部执行Ribbon负载均衡调用,故使用ribbonCommandFactory建立RibbonCommand,Ribbon客户端的懒加载就在这个方法内,这里咱们看HttpClientRibbonCommandFactory
实现类微服务
//## org.springframework.cloud.netflix.zuul.filters.route.apache.HttpClientRibbonCommandFactory public class HttpClientRibbonCommandFactory extends AbstractRibbonCommandFactory { @Override public HttpClientRibbonCommand create(final RibbonCommandContext context) { ZuulFallbackProvider zuulFallbackProvider = getFallbackProvider(context.getServiceId()); final String serviceId = context.getServiceId(); // 经过SpringClientFactory获取IClient接口实例 final RibbonLoadBalancingHttpClient client = this.clientFactory.getClient( serviceId, RibbonLoadBalancingHttpClient.class); client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId)); return new HttpClientRibbonCommand(serviceId, client, context, zuulProperties, zuulFallbackProvider, clientFactory.getClientConfig(serviceId)); } }
建立RibbonLoadBalancingHttpClient
的逻辑在 SpringClientFactory#getClient(serviceId, RibbonLoadBalancingHttpClient.class)
,以下:测试
如上执行完毕RibbonClient就基本懒加载完成了,就能够到RibbonClient对应的ApplicationContext中继续获取其它核心接口的实现类了,这些实现类都是根据 默认/全局/Client自定义 配置建立的
//## org.springframework.cloud.netflix.ribbon.SpringClientFactory public class SpringClientFactory extends NamedContextFactory<RibbonClientSpecification> { static final String NAMESPACE = "ribbon"; public SpringClientFactory() { super(RibbonClientConfiguration.class, NAMESPACE, "ribbon.client.name"); } /** * Get the rest client associated with the name. * @throws RuntimeException if any error occurs */ public <C extends IClient<?, ?>> C getClient(String name, Class<C> clientClass) { return getInstance(name, clientClass); } // name表明当前Ribbon客户端,type表明要获取的实例类型,如IClient、IRule @Override public <C> C getInstance(String name, Class<C> type) { // 先从父类NamedContextFactory中直接从客户端对应的ApplicationContext中获取实例 // 若是没有就根据IClientConfig中的配置找到具体的实现类,并经过反射初始化后放到Client对应的ApplicationContext中 C instance = super.getInstance(name, type); if (instance != null) { return instance; } IClientConfig config = getInstance(name, IClientConfig.class); return instantiateWithConfig(getContext(name), type, config); } // 使用IClientConfig实例化 static <C> C instantiateWithConfig(AnnotationConfigApplicationContext context, Class<C> clazz, IClientConfig config) { C result = null; try { // 经过以IClientConfig为参数的构造建立clazz类实例 Constructor<C> constructor = clazz.getConstructor(IClientConfig.class); result = constructor.newInstance(config); } catch (Throwable e) { // Ignored } // 若是没建立成功,使用无惨构造 if (result == null) { result = BeanUtils.instantiate(clazz); // 调用初始化配置方法 if (result instanceof IClientConfigAware) { ((IClientConfigAware) result).initWithNiwsConfig(config); } // 处理自动织入 if (context != null) { context.getAutowireCapableBeanFactory().autowireBean(result); } } return result; } } //## 父类 org.springframework.cloud.context.named.NamedContextFactory public abstract class NamedContextFactory<C extends NamedContextFactory.Specification> implements DisposableBean, ApplicationContextAware { // 维护Ribbon客户端对应的ApplicationContext上下文 private Map<String, AnnotationConfigApplicationContext> contexts = new ConcurrentHashMap<>(); // 维护Ribbon客户端的@Configuration配置类 private Map<String, C> configurations = new ConcurrentHashMap<>(); private ApplicationContext parent; private Class<?> defaultConfigType; // 默认配置类为 RibbonClientConfiguration private final String propertySourceName; // 默认为 ribbon private final String propertyName; // 默认读取RibbonClient名的属性为ribbon.client.name public NamedContextFactory(Class<?> defaultConfigType, String propertySourceName, String propertyName) { this.defaultConfigType = defaultConfigType; this.propertySourceName = propertySourceName; this.propertyName = propertyName; } // 若是包含Client上下文直接返回 // 若是不包含,调用createContext(name),并放入contexts集合 protected AnnotationConfigApplicationContext getContext(String name) { if (!this.contexts.containsKey(name)) { synchronized (this.contexts) { if (!this.contexts.containsKey(name)) { this.contexts.put(name, createContext(name)); } } } return this.contexts.get(name); } // 建立名为name的RibbonClient的ApplicationContext上下文 protected AnnotationConfigApplicationContext createContext(String name) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // configurations集合中是否包含当前Client相关配置类,包含即注入到ApplicationContext if (this.configurations.containsKey(name)) { for (Class<?> configuration : this.configurations.get(name) .getConfiguration()) { context.register(configuration); } } //configurations集合中是否包含default.开头的经过@RibbonClients(defaultConfiguration=xxx)配置的默认配置类 for (Map.Entry<String, C> entry : this.configurations.entrySet()) { if (entry.getKey().startsWith("default.")) { for (Class<?> configuration : entry.getValue().getConfiguration()) { context.register(configuration); } } } // 注册PropertyPlaceholderAutoConfiguration、RibbonClientConfiguration context.register(PropertyPlaceholderAutoConfiguration.class, this.defaultConfigType); // 添加 ribbon.client.name=具体RibbonClient name的enviroment配置 context.getEnvironment().getPropertySources().addFirst(new MapPropertySource( this.propertySourceName, Collections.<String, Object> singletonMap(this.propertyName, name))); // 设置父ApplicationContext,这样可使得当前建立的子ApplicationContext可使用父上下文中的Bean if (this.parent != null) { // Uses Environment from parent as well as beans context.setParent(this.parent); } context.refresh(); //刷新Context return context; } public <T> T getInstance(String name, Class<T> type) { AnnotationConfigApplicationContext context = getContext(name); if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, type).length > 0) { return context.getBean(type); } return null; } }
上面比较重要的就是在建立每一个RibbonClient的ApplicationContext的createContext(name)
方法,其中包含了根据哪一个@Configuration配置类建立Ribbon核心接口的实现类的逻辑,故需重点分析(Ribbon核心接口讲解 参考)
那么在createContext(name)
方法建立当前Ribbon Client相关的上下文,并注入配置类时,除了默认配置类RibbonClientConfiguration
是写死的,其它的配置类,如default全局配置类,针对某个Ribbon Client的配置类,又是怎么配置的呢?
//## org.springframework.cloud.context.named.NamedContextFactory#createContext() protected AnnotationConfigApplicationContext createContext(String name) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // 一、注册专门为RibbonClient指定的configuration配置类,@RibbonClient注解 if (this.configurations.containsKey(name)) { for (Class<?> configuration : this.configurations.get(name) .getConfiguration()) { context.register(configuration); } } // 二、将为全部RibbonClient的configuration配置类注册到ApplicationContext for (Map.Entry<String, C> entry : this.configurations.entrySet()) { if (entry.getKey().startsWith("default.")) { for (Class<?> configuration : entry.getValue().getConfiguration()) { context.register(configuration); } } } // 三、注册defaultConfigType,即Spring的默认配置类 RibbonClientConfiguration 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; }
根据如上逻辑能够看出会从3个地方将Ribbon相关的Configuration配置类注册到专门为其准备的ApplicationContext上下文,并根据配置类建立Ribbon核心接口的实现类,即达到配置RibbonClient的目的
RibbonClientConfiguration
那么configurations这个Map里的配置类数据是从哪儿来的呢??下面逐步分析
//## RibbonAutoConfiguration @Autowired(required = false) private List<RibbonClientSpecification> configurations = new ArrayList<>(); @Bean public SpringClientFactory springClientFactory() { SpringClientFactory factory = new SpringClientFactory(); factory.setConfigurations(this.configurations); return factory; }
首先是在RibbonAutoConfiguration自动配置类建立SpringClientFactory
是设置的,这个configurations集合是@Autowired的Spring容器内的RibbonClientSpecification
集合,那么RibbonClientSpecification
集合是什么时候被注册的??
//## org.springframework.cloud.netflix.ribbon.RibbonClientConfigurationRegistrar public class RibbonClientConfigurationRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { // 一、@RibbonClients注解 Map<String, Object> attrs = metadata.getAnnotationAttributes( RibbonClients.class.getName(), true); // 1.1 value是RibbonClient[],遍历针对具体的RibbonClient配置的configuration配置类,并注册 if (attrs != null && attrs.containsKey("value")) { AnnotationAttributes[] clients = (AnnotationAttributes[]) attrs.get("value"); for (AnnotationAttributes client : clients) { registerClientConfiguration(registry, getClientName(client), client.get("configuration")); } } // 1.2 找到@RibbonClients注解的defaultConfiguration,即默认配置 // 注册成以default.Classname.RibbonClientSpecification为名的RibbonClientSpecification 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")); } // 二、@RibbonClient注解 // 注册某个具体Ribbon Client的configuration配置类 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()); } }
如上可知,configurations配置类集合是根据@RibbonClient
和 @RibbonClients
注解配置的,分别有 针对具体某个RibbonClient的配置 和 default默认配置
总结一下,Ribbon相关的@Configuration配置类是如何加载的
@RibbonClient
和 @RibbonClients
注解加载的configurations集合中找当前RibbonClient name对应的配置类,若有,就注册到上下文@RibbonClients
注解加载的 default.开头 的默认配置类,若有,就注册到上下文RibbonClientConfiguration
上面说是如何建立RibbonClient相关的ApplicationContext上下文及注册Ribbon Client相关的配置类的逻辑,在肯定配置类后,其中会用到Ribbon的IClientConfig
相关的客户端配置来加载Ribbon客户端相关的配置信息,如超时配置、具体建立哪一个核心接口的实现类等,能够从Spring Cloud默认注册的 RibbonClientConfiguration
来一探究竟
//## org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration @Import({OkHttpRibbonConfiguration.class, RestClientRibbonConfiguration.class, HttpClientRibbonConfiguration.class}) public class RibbonClientConfiguration { @Value("${ribbon.client.name}") private String name = "client"; // TODO: maybe re-instate autowired load balancers: identified by name they could be // associated with ribbon clients @Autowired private PropertiesFactory propertiesFactory; @Bean @ConditionalOnMissingBean public IClientConfig ribbonClientConfig() { DefaultClientConfigImpl config = new DefaultClientConfigImpl(); config.loadProperties(this.name); return config; } @Bean @ConditionalOnMissingBean public IRule ribbonRule(IClientConfig config) { if (this.propertiesFactory.isSet(IRule.class, name)) { return this.propertiesFactory.get(IRule.class, config, name); } ZoneAvoidanceRule rule = new ZoneAvoidanceRule(); rule.initWithNiwsConfig(config); return rule; }
上面只截取了一段代码,给出了Ribbon相关的 IClientConfig
客户端配置 和 某一个核心接口IRule
实现类 是如何加载配置并建立的
IClientConfig
IClientConfig
就是Ribbon客户端配置的接口,能够看到先是建立了DefaultClientConfigImpl
默认实现类,再config.loadProperties(this.name)
加载当前Client相关的配置
//## com.netflix.client.config.DefaultClientConfigImpl#loadProperties() /** * Load properties for a given client. It first loads the default values for all properties, * and any properties already defined with Archaius ConfigurationManager. */ @Override public void loadProperties(String restClientName){ enableDynamicProperties = true; setClientName(restClientName); // 一、使用Netflix Archaius的ConfigurationManager从Spring env中加载“ribbon.配置项”这类默认配置 // 如没加载到有默认静态配置 loadDefaultValues(); // 二、使用Netflix Archaius的ConfigurationManager从Spring env中加载“client名.ribbon.配置项”这类针对某个Client的配置信息 Configuration props = ConfigurationManager.getConfigInstance().subset(restClientName); for (Iterator<String> keys = props.getKeys(); keys.hasNext(); ){ String key = keys.next(); String prop = key; try { if (prop.startsWith(getNameSpace())){ prop = prop.substring(getNameSpace().length() + 1); } setPropertyInternal(prop, getStringValue(props, key)); } catch (Exception ex) { throw new RuntimeException(String.format("Property %s is invalid", prop)); } } }
根据如上注释,若是你没有在项目中指定ribbon相关配置,那么会使用DefaultClientConfigImpl
中的默认静态配置,若是Spring enviroment中包含“ribbon.配置项”这类针对全部Client的配置会被加载进来,有“client名.ribbon.配置项”这类针对某个Client的配置信息也会被加载进来
静态配置以下:
RibbonClient核心接口实现类配置加载及建立
上面说完IClientCOnfig
配置项是如何加载的,按道理说其中已经包含了当前RibbonClient使用哪一个核心接口实现类的配置,但Spring Cloud在此处定义了本身的实现逻辑
@Autowired private PropertiesFactory propertiesFactory; @Bean @ConditionalOnMissingBean public IRule ribbonRule(IClientConfig config) { // 查看propertiesFactory是否有关于当前接口的配置,若有就使用,并建立实例返回 if (this.propertiesFactory.isSet(IRule.class, name)) { return this.propertiesFactory.get(IRule.class, config, name); } // spring cloud 默认配置 ZoneAvoidanceRule rule = new ZoneAvoidanceRule(); rule.initWithNiwsConfig(config); return rule; }
下面看看PropertiesFactory
的逻辑
public class PropertiesFactory { @Autowired private Environment environment; private Map<Class, String> classToProperty = new HashMap<>(); public PropertiesFactory() { classToProperty.put(ILoadBalancer.class, "NFLoadBalancerClassName"); classToProperty.put(IPing.class, "NFLoadBalancerPingClassName"); classToProperty.put(IRule.class, "NFLoadBalancerRuleClassName"); classToProperty.put(ServerList.class, "NIWSServerListClassName"); classToProperty.put(ServerListFilter.class, "NIWSServerListFilterClassName"); } // 查看当前clazz是否在classToProperty管理的几个核心接口之一 // 如是,查看Spring environment中是否能找到 “clientName.ribbon.核心接口配置项”的配置信息 public boolean isSet(Class clazz, String name) { return StringUtils.hasText(getClassName(clazz, name)); } public String getClassName(Class clazz, String name) { if (this.classToProperty.containsKey(clazz)) { String classNameProperty = this.classToProperty.get(clazz); String className = environment.getProperty(name + "." + NAMESPACE + "." + classNameProperty); return className; } return null; } // 也是先调用getClassName()获取Spring enviroment中配置的核心接口实现类名 // 再使用IClientConfig配置信息建立其实例 @SuppressWarnings("unchecked") public <C> C get(Class<C> clazz, IClientConfig config, String name) { String className = getClassName(clazz, name); if (StringUtils.hasText(className)) { try { Class<?> toInstantiate = Class.forName(className); return (C) instantiateWithConfig(toInstantiate, config); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Unknown class to load "+className+" for class " + clazz + " named " + name); } } return null; } }
故以上面建立IRule
接口实现类的逻辑
DefaultClientConfigImpl
中的静态配置,而是使用Spring Cloud自定义的默认实现类,拿IRule
规则接口来讲是ZoneAvoidanceRule
总结:
首先会建立RibbonClient的ApplicationContext上下文,并肯定使用哪一个Configuration配置类
一、@RibbonClients注册的全局默认配置类
二、@RibbonClient注册的某个Client配置类
三、Spring Cloud 默认的RibbonClientConfiguration配置类
肯定配置类后就是加载Client相关的IClientConfig配置信息,并建立核心接口实现类
若是没有自定义全局/客户端配置类,那么就是使用
RibbonClientConfiguration
,而其规则是对于超时等配置(除核心接口实现类之外):使用Netflix的配置逻辑,经过 ribbon.xxx 做为默认配置,以 clientName.ribbon.xxx 做为客户端定制配置
对于核心接口实现类配置:客户端定制配置仍然使用 clientName.ribbon.xxx,但默认配置是Spring Cloud在
RibbonClientConfiguration
方法中写死的默认实现类
已经知道大概的逻辑了,下面就看看具体如何自定义Client配置、全局配置
这部分在Spring Cloud官方reference中有说明 16.2 Customizing the Ribbon Client
大体意思以下:
一部分配置(非核心接口实现类的配置)可使用Netflix原生API提供的方式,即便用如
com.netflix.client.config.CommonClientConfigKey
若是想比较全面的控制RibbonClient并添加一些额外配置,可使用 @RibbonClient
或 @RibbonClients
注解,并配置一个配置类,如上的 FooConfiguration
@RibbonClient(name = "foo", configuration = FooConfiguration.class) 是针对名为 foo 的RibbonClient的配置类,也可使用@RibbonClients({@RibbonClient数组}) 的形式给某几个RibbonClient设置配置类
@RibbonClients( defaultConfiguration = { xxx.class } ) 是针对全部RIbbonClient的默认配置
官方文档说 FooConfiguration配置类 必须是@Configuration的,这样就必须注意,SpringBoot主启动类不能扫描到FooConfiguration,不然针对某个RibbonClient的配置就会变成全局的,缘由是在建立每一个RibbonClient时会为其建立ApplicationContext上下文,其parent就是主启动类建立的ApplicationContext,子ApplicationContext中可使用父ApplicationContext中的Bean,且建立Bean时都使用了@ConditionalOnMissingBean
,因此FooConfiguration若是被主启动类的上下文加载,且建立了好比IRule的实现类,在某个RIbbonClient建立其子ApplicationContext并@Bean想建立其自定义IRule实现类时,会发现parent ApplicationContext已经存在,就不会建立了,配置就失效了
但在个人实验中,即便FooConfiguration不加@Configuration注解也能够加载为RibbonClient的配置,且因为没有@Configuration了,也不会被主启动类扫描到
因此主要分红2种配置:
(1)超时时间等静态配置,使用 ribbon.* 配置全部Client,使用
(2)使用哪一种核心接口实现类配置,使用@RibbonClients注解作默认配置,使用@RibbonClient作针对Client的配置(注意@Configuration不要被SpringBoot主启动类扫描到的问题)