原文:https://blog.csdn.net/youaremoon/article/details/50601889
dubbo(官网地址)是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架。目前,阿里巴巴内部已经不再使用dubbo,但对很对未到一定量级的公司来说,dubbo依然是一个很好的选择。
之前在使用duubo的时候,对dubbo有了一些初步的了解,但没有深入,有些问题还是不清楚。所以准备静下心来看下dubbo源码。这里假设你对dubbo有一定的了解,不再详细的讲解dubbo的架构。如果没接触过dubbo,可以先从其官网了解。
dubbo号称通过spring的方式可以透明化接入应用,对应用没有任何api侵入。下面看看官方的consumer demo,其配置如下:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
- <bean class="com.alibaba.dubbo.demo.consumer.DemoAction" init-method="start">
- <property name="demoService" ref="demoService" />
- </bean>
- </beans>
- dubbo.container=log4j,spring
- dubbo.application.name=demo-consumer
- dubbo.application.owner=
- dubbo.registry.address=multicast://224.5.6.7:1234
- package com.alibaba.dubbo.demo.consumer;
- import com.alibaba.dubbo.demo.DemoService;
- public class DemoAction {
- private DemoService demoService;
- public void setDemoService(DemoService demoService) {
- this.demoService = demoService;
- }
- <pre name="code" class="java"> public void start() throws Exception {
- String hello = demoService.sayHello("who are you");
- System.out.println(hello);
- }
- }
spring中有两种bean,一种是普通的bean,一种是工厂bean,即FactoryBean。普通bean通过class字符串代表的类直接实例化对象,而工厂bean则是通过class字符串代表的工厂类的getObject()方法来实例化对象。如以下FactoryBean在spring中返回的是MyObject对象,而不是MyFactoryBean对象。 这里不过多介绍FactoryBean,有兴趣的同学可以自行google。
- class MyFactoryBean implements FactoryBean<Object> {
- public Object getObject() throws Exception {
- return new MyObject();
- }
- ........
- }
ReferenceBean继承自ReferenceConfig,并实现了FactoryBean, ApplicationContextAware, InitializingBean, DisposableBean。
由于实现了InitializingBean,在初始化各个属性后会调用afterPropertiesSet, 实现比较简单,主要是对没有初始化的几个属性尝试用公共的默认配置进行初始化,这里就不再细讲:
- public void afterPropertiesSet() throws Exception {
- if (getConsumer() == null) {
- // 如果没有配置consumer属性,则使用默认的consumer属性
- }
- if (getApplication() == null
- && (getConsumer() == null || getConsumer().getApplication() == null)) {
- // 如果没有配置application则使用默认的application
- }
- if (getModule() == null
- && (getConsumer() == null || getConsumer().getModule() == null)) {
- // 如果没有配置module则使用默认的module
- }
- if ((getRegistries() == null || getRegistries().size() == 0)
- && (getConsumer() == null || getConsumer().getRegistries() == null || getConsumer().getRegistries().size() == 0)
- && (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().size() == 0)) {
- // 如果没有配置registries则使用默认的registries
- }
- if (getMonitor() == null
- && (getConsumer() == null || getConsumer().getMonitor() == null)
- && (getApplication() == null || getApplication().getMonitor() == null)) {
- // 如果没有配置monitor则使用默认monitor
- }
- // 如果设置了init属性且为true,则初始化对象,默认是不初始化的
- Boolean b = isInit();
- if (b == null && getConsumer() != null) {
- b = getConsumer().isInit();
- }
- if (b != null && b.booleanValue()) {
- getObject();
- }
- }
- public Object getObject() throws Exception {
- return get();
- }
- public synchronized T get() {
- // 已经销毁则不能再获取
- if (destroyed){
- throw new IllegalStateException("Already destroyed!");
- }
- // 如果ref为空则初始化后再返回
- if (ref == null) {
- init();
- }
- return ref;
- }
- private void init() {
- // 先判断initialized标记,如果为true,表示已经初始化过,初始化过则不再初始化。 从这里看出如果第一次初始化失败了,则后续该consumer无法再使用
- if (initialized) {
- return;
- }
- initialized = true;
- if (interfaceName == null || interfaceName.length() == 0) {
- throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
- }
- // 再次检查consumer配置,同时通过appendProperties修改代码/xml中的配置。
- // appendProperties从System.getProperty中获取配置,如果有相应值则替换配置文件中的值。
- // 比如对于consumer来说,如果配置了timeout=5000, 可以通过:
- // 1、在启动参数中设置dubbo.consumer.timeout=3000来修改这个值;
- // 2、在启动参数中设置dubbo.consumer.com.alibaba.dubbo.config.ConsumerConfig.timeout=3000来修改这个值。
- // 其中第二种方式优先级高于第一种,修改其他属性只需要替换"timeout"。
- // 如果系统设置中没有,还可以在启动参数中设置dubbo.properties.file(如果没设置则默认为dubbo.properties), 加载文件中的配置
- // 其中系统设置的优先级高于文件设置
- checkDefault();
- // 与上面修改consumer一样,通过启动参数/系统设置/文件配置的方式修改代码/xml中的配置。
- appendProperties(this);
- // 泛化调用设置,如果是泛化调用则接口类为GeneicService,否则为配置的interfaceName
- if (getGeneric() == null && getConsumer() != null) {
- setGeneric(getConsumer().getGeneric());
- }
- if (ProtocolUtils.isGeneric(getGeneric())) {
- interfaceClass = GenericService.class;
- } else {
- try {
- interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
- .getContextClassLoader());
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
- // 1、检查是否是接口,2、如果有methods配置,检查methods中声明的方法在接口中是否存在
- checkInterfaceAndMethods(interfaceClass, methods);
- }
- // 用户可以通过系统属性的方式来指定interfaceName对应的url
- String resolve = System.getProperty(interfaceName);
- String resolveFile = null;
- if (resolve == null || resolve.length() == 0) {
- resolveFile = System.getProperty("dubbo.resolve.file");
- if (resolveFile == null || resolveFile.length() == 0) {
- File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties");
- if (userResolveFile.exists()) {
- resolveFile = userResolveFile.getAbsolutePath();
- }
- }
- if (resolveFile != null && resolveFile.length() > 0) {
- Properties properties = new Properties();
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(new File(resolveFile));
- properties.load(fis);
- } catch (IOException e) {
- throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e);
- } finally {
- try {
- if(null != fis) fis.close();
- } catch (IOException e) {
- logger.warn(e.getMessage(), e);
- }
- }
- resolve = properties.getProperty(interfaceName);
- }
- }
- if (resolve != null && resolve.length() > 0) {
- url = resolve;
- if (logger.isWarnEnabled()) {
- if (resolveFile != null && resolveFile.length() > 0) {
- logger.warn("Using default dubbo resolve file " + resolveFile + " replace " + interfaceName + "" + resolve + " to p2p invoke remote service.");
- } else {
- logger.warn("Using -D" + interfaceName + "=" + resolve + " to p2p invoke remote service.");
- }
- }
- }
- // 这里忽略掉使用各个默认值来初始化null值的代码
- ......
- // 检测application配置,同样也会调用appendProperties进行参数的修改
- checkApplication();
- // 检查local/stub/mock三个配置是否正确
- checkStubAndMock(interfaceClass);
- // 将所有配置加入到map中
- Map<String, String> map = new HashMap<String, String>();
- Map<Object, Object> attributes = new HashMap<Object, Object>();
- map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE);
- map.put(Constants.DUBBO_VERSION_KEY, Version.getVersion());
- map.put(Constants.TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
- if (ConfigUtils.getPid() > 0) {
- map.put(Constants.PID_KEY, String.valueOf(ConfigUtils.getPid()));
- }
- if (! isGeneric()) {
- String revision = Version.getVersion(interfaceClass, version);
- if (revision != null && revision.length() > 0) {
- map.put("revision", revision);
- }
- String[] methods = Wrapper.getWrapper(interfaceClass).getMethodNames();
- if(methods.length == 0) {
- logger.warn("NO method found in service interface " + interfaceClass.getName());
- map.put("methods", Constants.ANY_VALUE);
- }
- else {
- map.put("methods", StringUtils.join(new HashSet<String>(Arrays.asList(methods)), ","));
- }
- }
- map.put(Constants.INTERFACE_KEY, interfaceName);
- // 依次将application/module/consumer/reference的配置放入map
- appendParameters(map, application);
- appendParameters(map, module);
- appendParameters(map, consumer, Constants.DEFAULT_KEY);
- appendParameters(map, this);
- String prifix = StringUtils.getServiceKey(map);
- if (methods != null && methods.size() > 0) {
- for (MethodConfig method : methods) {
- appendParameters(map, method, method.getName());
- String retryKey = method.getName() + ".retry";
- if (map.containsKey(retryKey)) {
- String retryValue = map.remove(retryKey);
- if ("false".equals(retryValue)) {
- map.put(method.getName() + ".retries", "0");
- }
- }
- appendAttributes(attributes, method, prifix + "." + method.getName());
- checkAndConvertImplicitConfig(method, map, attributes);
- }
- }
- //attributes通过系统context进行存储.
- StaticContext.getSystemContext().putAll(attributes);
- // 使用map中的参数通过createProxy方法创建代理对象
- ref = createProxy(map);
- }
- private T createProxy(Map<String, String> map) {
- // 使用map创建一个URL对象,注意该URL是dubbo重新实现的URL
- URL tmpUrl = new URL("temp", "localhost", 0, map);
- final boolean isJvmRefer; // 是否是本地服务
- if (isInjvm() == null) {
- if (url != null && url.length() > 0) { //指定URL的情况下,不做本地引用
- isJvmRefer = false;
- } else if (InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl)) {
- //默认情况下如果本地有服务暴露,则引用本地服务.
- isJvmRefer = true;
- } else {
- isJvmRefer = false;
- }
- } else {
- isJvmRefer = isInjvm().booleanValue();
- }
- // 如果是本地服务,则url使用本地服务的协议形式
- if (isJvmRefer) {
- URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map);
- invoker = refprotocol.refer(interfaceClass, url);
- if (logger.isInfoEnabled()) {
- logger.info("Using injvm service " + interfaceClass.getName());
- }
- } else {
- if (url != null && url.length() > 0) { // 用户指定URL,指定的URL可能是对点对直连地址,也可能是注册中心URL
- String[] us = Constants.SEMICOLON_SPLIT_PATTERN.split(url);
- if (us != null && us.length > 0) {
- for (String u : us) {
- URL url = URL.valueOf(u);
- if (url.getPath() == null || url.getPath().length() == 0) {
- url = url.setPath(interfaceName);
- }
- if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
- urls.add(url.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
- } else {
- urls.add(ClusterUtils.mergeUrl(url, map));
- }
- }
- }
- } else { // 通过注册中心配置拼装URL, 如果没有注册信息会报错
- List<URL> us = loadRegistries(false);
- if (us != null && us.size() > 0) {
- for (URL u : us) {
- // 加载monitor配置,如果加载到配置则返回非空的monitorUrl
- URL monitorUrl = loadMonitor(u);
- if (monitorUrl != null) {
- map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
- }
- urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
- }
- }
- if (urls == null || urls.size() == 0) {
- throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
- }
- }
- // 此处refprotocol为RegistryProtocol,它的refer方法会生成一个Registry(如ZookeeperRegisty、MulticastRegistry),
- // 生成的Registry通过url中的注册中心地址与注册中心建立连接,并订阅相关信息,最终封装成一个invoker
- if (urls.size() == 1) {
- invoker = refprotocol.refer(interfaceClass, urls.get(0));
- } else {
- List<Invoker<?>> invokers = new ArrayList<Invoker<?>>();
- URL registryURL = null;
- for (URL url : urls) {
- invokers.add(refprotocol.refer(interfaceClass, url));
- if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
- registryURL = url; // 用了最后一个registry url
- }
- }
- if (registryURL != null) { // 有 注册中心协议的URL
- // 对有注册中心的Cluster 只用 AvailableCluster
- URL u = registryURL.addParameter(Constants.CLUSTER_KEY, AvailableCluster.NAME);
- invoker = cluster.join(new StaticDirectory(u, invokers));
- } else { // 不是 注册中心的URL
- invoker = cluster.join(new StaticDirectory(invokers));
- }
- }
- }
- // 检查check属性,如果check为空或未true,检测与服务提供方是否连接成功,连接失败则报错
- Boolean c = check;
- if (c == null && consumer != null) {
- c = consumer.isCheck();
- }
- if (c == null) {
- c = true; // default true
- }
- if (c && ! invoker.isAvailable()) {
- throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion());
- }
- if (logger.isInfoEnabled()) {
- logger.info("Refer dubbo service " + interfaceClass.getName() + " from url " + invoker.getUrl());
- }
- // 调用代理生成工厂创建服务代理
- return (T) proxyFactory.getProxy(invoker);
- }
- public class Protocol$Adpative implements Protocol {
- public Exporter export(Invoker invoker) throws com.alibaba.dubbo.rpc.RpcException {
- if (invoker == null || invoker.getUrl() == null) {
- throw new IllegalArgumentException("xxx");
- }
- URL url = invoker.getUrl();
- String extName = url.getProtocol() == null ? "dubbo" : url.getProtocol();
- if(extName == null) {
- throw new IllegalStateException("xxx");
- }
- Protocol extension = (Protocol)ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(extName);
- return extension.export(invoker);
- }
- public Invoker refer(Class cls, URL u) throws RpcException {
- if (u == null) {
- throw new IllegalArgumentException("url == null");
- }
- URL url = u;
- String extName = url.getProtocol() == null ? "dubbo" : url.getProtocol();
- if(extName == null) {
- throw new IllegalStateException("xxx");
- }
- Protocol extension = (Protocol)ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(extName);
- return extension.refer(cls, u);
- }
- }
这个代理类的实现比较简单,就是根据URL中的protocol加载实际的实现,并找到对应的wrapper class(即构造方法中含对应接口的类,如类A有构造方法A(B b)且在META-INF\dubbo目录下有配置,则A是B的wrapper class),通过wrapper class包装真正的Protocol实现返回,如果没有wrapper则直接返回其实现类。注意,ExtensionLoader.getExtensionLoader(XX.class).getAdaptiveExtension()生成的代理类思路都是一样的,后续出现这样的代码就不再给出了。前面的代码“refprotocol.refer(interfaceClass, urls.get(0));”中urls.get(0)的protocol为registry, 因此最终调用的是com.alibaba.dubbo.registry.integration.RegistryProtocol的refer方法:
- public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
- // 将url的protocal改为registry对应的协议,如multicast或者zookeeper
- url = url.setProtocol(url.getParameter(Constants.REGISTRY_KEY, Constants.DEFAULT_REGISTRY)).removeParameter(Constants.REGISTRY_KEY);
- // 此处的registryFactory也是个代理类,根据protocal可能是ZookeeperRegistryFactory、MulticastRegistryFactory等,
- // getRegistry方法根据url返回ZookeeperRegistry、MulticastRegistry对象等,各Registry对象在初始化的时候会根据url和注册中心建立连接
- Registry registry = registryFactory.getRegistry(url);
- if (RegistryService.class.equals(type)) {
- return proxyFactory.getInvoker((T) registry, type, url);
- }
- // group="a,b" or group="*"
- Map<String, String> qs = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY));
- String group = qs.get(Constants.GROUP_KEY);
- if (group != null && group.length() > 0 ) {
- if ( ( Constants.COMMA_SPLIT_PATTERN.split( group ) ).length > 1
- || "*".equals( group ) ) {
- return doRefer( getMergeableCluster(), registry, type, url );
- }
- }
- return doRefer(cluster, registry, type, url);
- }
- private <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type, URL url) {
- // 创建Directory服务,通过它可以进行消息的注册、订阅,同时负责注册中心变更时的消息接收及处理
- RegistryDirectory<T> directory = new RegistryDirectory<T>(type, url);
- directory.setRegistry(registry);
- directory.setProtocol(protocol);
- URL subscribeUrl = new URL(Constants.CONSUMER_PROTOCOL, NetUtils.getLocalHost(), 0, type.getName(), directory.getUrl().getParameters());
- if (! Constants.ANY_VALUE.equals(url.getServiceInterface())
- && url.getParameter(Constants.REGISTER_KEY, true)) {
- registry.register(subscribeUrl.addParameters(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY,
- Constants.CHECK_KEY, String.valueOf(false)));
- }
- directory.subscribe(subscribeUrl.addParameter(Constants.CATEGORY_KEY,
- Constants.PROVIDERS_CATEGORY
- + "," + Constants.CONFIGURATORS_CATEGORY
- + "," + Constants.ROUTERS_CATEGORY));
- // directory中url的cluster为null,则取默认值failover(默认值见接口Cluster的SPI注解),
- // 对应类为com.alibaba.dubbo.rpc.cluster.support.FailoverCluster,该类的join方法返回new FailoverClusterInvoker<T>(directory)
- return cluster.join(directory);
- }
再来看另一个分支urls.size() > 1, 此时针对每个url都会生成一个ClusterInvoker,然后将其放入StaticDirectory管理。对于StaticDirectory来说,其下的任意一个注册中心可用则其可用。
再回头看ReferenceConfig生成代理的最后一步:return (T) proxyFactory.getProxy(invoker); 这里默认的实现为com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory, 它的getProxy方法:
- // 继承自抽象父类AbstractProxyFactory
- public <T> T getProxy(Invoker<T> invoker) throws RpcException {
- Class<?>[] interfaces = null;
- String config = invoker.getUrl().getParameter("interfaces");
- if (config != null && config.length() > 0) {
- // 通过逗号拆分interfaces参数来获取所有的interface
- String[] types = Constants.COMMA_SPLIT_PATTERN.split(config);
- if (types != null && types.length > 0) {
- interfaces = new Class<?>[types.length + 2];
- interfaces[0] = invoker.getInterface();
- interfaces[1] = EchoService.class;
- for (int i = 0; i < types.length; i ++) {
- interfaces[i + 1] = ReflectUtils.forName(types[i]);
- }
- }
- }
- // 未指定interfaces则直接取invoker中的interface
- if (interfaces == null) {
- interfaces = new Class<?>[] {invoker.getInterface(), EchoService.class};
- }
- return getProxy(invoker, interfaces);
- }
- // JavassistProxyFactory
- public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
- // com.alibaba.dubbo.common.bytecode.Proxy
- return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
- }
- package com.alibaba.dubbo.common.bytecode;
- import com.alibaba.dubbo.demo.DemoService;
- import com.alibaba.dubbo.rpc.service.EchoService;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- // 见com.alibaba.dubbo.common.bytecode.Proxy.getProxy
- public class proxy0 implements ClassGenerator.DC, EchoService, DemoService {
- // methods包含proxy0实现的所有接口方法(去重)
- public static Method[] methods;
- private InvocationHandler handler;
- public String sayHello(String paramString) {
- Object[] arrayOfObject = new Object[1];
- arrayOfObject[0] = paramString;
- Object localObject = this.handler.invoke(this, methods[0], arrayOfObject);
- return (String)localObject;
- }
- public Object $echo(Object paramObject) {
- Object[] arrayOfObject = new Object[1];
- arrayOfObject[0] = paramObject;
- Object localObject = this.handler.invoke(this, methods[1], arrayOfObject);
- return (Object)localObject;
- }
- public proxy0() {
- }
- public proxy0(InvocationHandler paramInvocationHandler) {
- this.handler = paramInvocationHandler;
- }
- }
最终生成的类传入InvokerInvocationHandler,此handler包装了invoker,handler拦截了toString/hashCode/equals方法,其他的则是直接调用invoker.invoke(new RpcInvocation(method, args)).recreate(); 例如在代码中调用demoService.sayHello("hello world"), 实际的调用为invoker.invoke(new RpcInvocation(sayHelloMethod, new Object[] {"hello world"})).recreate();
到此客户端的代理生成完成,总结如下:
1、spring加载时拦截namespace=dubbo的标签进行解析,生成dubbo中的config;
2、consumer对应的直接配置为ReferenceConfig, reference的config加载完成后,分别使用ConsumerConfig、ApplicationConfig、ModuleConfig、RegistryConfig、MonitorConfig等的默认值来初始化ReferenceConfig;
3、在创建bean的对象时,如果已经创建过则不重复创建,否则进入创建流程,并将是否创建的标识设为true;
4、使用系统参数、配置文件覆盖api/xml中设置的配置,将所有配置项存入map;
5、获取注册中心配置,根据配置连接注册中心,并注册和订阅url的变动提醒;
6、生成cluster invoker;
7、根据接口生成代理类,并创建对象,创建时传入InvokerInvocationHandler,该handler封装了上面生成的invoker,最终的接口调用都是通过invoker.invoke(。。。)。