Ribbon为服务创建独立上下文的流程

kyaa111 2月前 ⋅ 106 阅读
  1. 从Feign接口开始
goodsFeignClient.getGoods(id)
  1. 进入代理类的拦截方法
feign.ReflectiveFeign.FeignInvocationHandler#invoke

feign.SynchronousMethodHandler#invoke
  1. 继续跳转, 默认使用ribbon包下的LoadBalancerFeignClient
feign.SynchronousMethodHandler#executeAndDecode

org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient#execute
  1. 为每个服务创建独立的配置
org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient#getClientConfig
// 如果接口有Options参数, 则使用Feign接口上的(后续仍会创建上下文); 没有的话直接走this.clientFactory.getClientConfig创建容器加载配置
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;
}
  1. 调用SpringClientFactory#getClientConfig方法, 一直跳转到
org.springframework.cloud.context.named.NamedContextFactory#createContext
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);
        }
    }
    // 将Specification注册到容器
    for (Map.Entry<String, C> entry : this.configurations.entrySet()) {
        if (entry.getKey().startsWith("default.")) {
            for (Class<?> configuration : entry.getValue().getConfiguration()) {
                context.register(configuration);
            }
        }
    }
    //注册 配置解析器 以及 RibbonClientConfiguration
    context.register(PropertyPlaceholderAutoConfiguration.class,
                     this.defaultConfigType);
    // 添加一个属性源 
    // 示例结构
    // {
    //     "ribbon": {
    //         "ribbon.client.name": "test-service"
    //     }
    // }
    context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
        this.propertySourceName,
        Collections.<String, Object>singletonMap(this.propertyName, name)));

    if (this.parent != null) {
        context.setParent(this.parent);
        context.setClassLoader(this.parent.getClassLoader());
    }
    context.setDisplayName(generateDisplayName(name));
    context.refresh();
    return context;
}
  1. 第5步里this.configurations.entrySet()加载了一些配置类, 比如
// 使用nacos
com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfiguration

这些是通过org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration 以及org.springframework.cloud.netflix.ribbon.RibbonClientConfigurationRegistrar初始化的

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();
			}
             // 读取RibbonClients注解上defaultConfiguration的值
			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 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());
	}
    // ignore others code   
}
@Configuration
@Conditional(RibbonAutoConfiguration.RibbonClassesConditions.class)
@RibbonClients
@AutoConfigureAfter(
        name = "org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration")
@AutoConfigureBefore({ LoadBalancerAutoConfiguration.class,
        AsyncLoadBalancerAutoConfiguration.class })
@EnableConfigurationProperties({ RibbonEagerLoadProperties.class,
        ServerIntrospectorProperties.class })
public class RibbonAutoConfiguration {

    //将标记了RibbonClients注解的类注入进来 就是上面动态注入的bean
    @Autowired(required = false)
    private List<RibbonClientSpecification> configurations = new ArrayList<>();


    @Bean
    @ConditionalOnMissingBean
    public SpringClientFactory springClientFactory() {
        SpringClientFactory factory = new SpringClientFactory();
        factory.setConfigurations(this.configurations);
        return factory;
    }
}
  1. 最后将创建的上下文缓存起来, 并返回创建好的bean

org.springframework.cloud.context.named.NamedContextFactory#getContext

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);
}