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>
另外还提供了一份properties文件:- 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);
- }
- }
可以看到,代码方面确实是零侵入,而在配置方面,则是增加了一些服务的声明,环境配置之类的(不可缺少)。对于开发者来说非常友好。那么dubbo是如何做到这点的呢。 这个demoService在consumer端明明没有具体的实现,为何能够正常的调用并获取到结果? 要了解原因,必须先了解spring开发的一个接口:FactoryBean。 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();
- }
-
- ........
- }
了解了FactoryBean后,仍然有困惑。在上面的配置里,并没有出现FactoryBean的实现类。这里需要了解另外一个spring的知识点:schema扩展。在dubbo中,所有namespace=dubbo的标签将被dubbo自己解析, 具体见com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler。其中reference对应的类为com.alibaba.dubbo.config.spring.ReferenceBean,ReferenceBean是一个FactoryBean,通过getObject()来产生代理类,我们的故事也是从此类开始。 ReferenceBean继承自ReferenceConfig,并实现了FactoryBean, ApplicationContextAware, InitializingBean, DisposableBean。
由于实现了InitializingBean,在初始化各个属性后会调用afterPropertiesSet, 实现比较简单,主要是对没有初始化的几个属性尝试用公共的默认配置进行初始化,这里就不再细讲:
- public void afterPropertiesSet() throws Exception {
- if (getConsumer() == null) {
-
- }
- if (getApplication() == null
- && (getConsumer() == null || getConsumer().getApplication() == null)) {
-
- }
- if (getModule() == null
- && (getConsumer() == null || getConsumer().getModule() == null)) {
-
- }
- if ((getRegistries() == null || getRegistries().size() == 0)
- && (getConsumer() == null || getConsumer().getRegistries() == null || getConsumer().getRegistries().size() == 0)
- && (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().size() == 0)) {
-
- }
- if (getMonitor() == null
- && (getConsumer() == null || getConsumer().getMonitor() == null)
- && (getApplication() == null || getApplication().getMonitor() == null)) {
-
- }
-
- Boolean b = isInit();
- if (b == null && getConsumer() != null) {
- b = getConsumer().isInit();
- }
- if (b != null && b.booleanValue()) {
- getObject();
- }
- }
由于实现了FactoryBean,当需要初始化或者应用中需要用到时,会调用getObject()方法获取实际的对象:- public Object getObject() throws Exception {
- return get();
- }
get方法在父类ReferenceConfig中, 其实现为:- public synchronized T get() {
-
- if (destroyed){
- throw new IllegalStateException("Already destroyed!");
- }
-
- if (ref == null) {
- init();
- }
- return ref;
- }
init方法比较大:- private void init() {
-
- if (initialized) {
- return;
- }
- initialized = true;
- if (interfaceName == null || interfaceName.length() == 0) {
- throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
- }
-
-
-
-
-
-
-
-
- checkDefault();
-
- appendProperties(this);
-
- 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);
- }
-
- checkInterfaceAndMethods(interfaceClass, methods);
- }
-
- 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.");
- }
- }
- }
-
-
- ......
-
- checkApplication();
-
- checkStubAndMock(interfaceClass);
-
- 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);
-
- 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);
- }
- }
-
- StaticContext.getSystemContext().putAll(attributes);
-
- ref = createProxy(map);
- }
可以看到init方法会先做大量的配置初始化和检查工作,并将生成的配置放入map中,通过map创建代理,下面看看创建代理的方法:- private T createProxy(Map<String, String> map) {
-
- URL tmpUrl = new URL("temp", "localhost", 0, map);
- final boolean isJvmRefer;
- if (isInjvm() == null) {
- if (url != null && url.length() > 0) {
- isJvmRefer = false;
- } else if (InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl)) {
-
- isJvmRefer = true;
- } else {
- isJvmRefer = false;
- }
- } else {
- isJvmRefer = isInjvm().booleanValue();
- }
-
- 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) {
- 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 {
- List<URL> us = loadRegistries(false);
- if (us != null && us.size() > 0) {
- for (URL u : us) {
-
- 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.");
- }
- }
-
-
- 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;
- }
- }
- if (registryURL != null) {
-
- URL u = registryURL.addParameter(Constants.CLUSTER_KEY, AvailableCluster.NAME);
- invoker = cluster.join(new StaticDirectory(u, invokers));
- } else {
- invoker = cluster.join(new StaticDirectory(invokers));
- }
- }
- }
-
-
- Boolean c = check;
- if (c == null && consumer != null) {
- c = consumer.isCheck();
- }
- if (c == null) {
- c = 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);
- }
可以看到这个创建代理的方法是通过调用proxyFactory.getProxy来创建代理。而创建代理之前需要先加载注册中心的配置,并生成monitor对应的key。接下来就是根据上面加载到的信息来创建invoker, invoker是dubbo中非常重要的一个概念,它代表一个可执行体,通过调用它的invoke方法来进行本地/远程调用,并获取结果。它的生成有两个分支,第一个分支是urls.size() == 1,从前面的代码可以知道当注册中心只有一个(单个或集群)时进入此分支,此处的refprotocal是由ExtensionLoader生成的代理类,其关键代码如下:- 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 = url.setProtocol(url.getParameter(Constants.REGISTRY_KEY, Constants.DEFAULT_REGISTRY)).removeParameter(Constants.REGISTRY_KEY);
-
-
- Registry registry = registryFactory.getRegistry(url);
- if (RegistryService.class.equals(type)) {
- return proxyFactory.getInvoker((T) registry, type, url);
- }
-
-
- 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) {
-
- 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));
-
-
- return cluster.join(directory);
- }
可以看到第一个分支在非group模式时返回的invoker为FailoverClusterInvoker,顾名思义,failover就是故障转移,即当对某个服务器调用失败时,选用其他的服务器重试。而在group模式时返回的invoker为MergeableClusterInvoker,作用就是将多个group的返回数据进行组合。不管是FailoverClusterInvoker还是MergeableClusterInvoker,其实现都比较复杂,所以细节先不展开,后面再单独讲。 再来看另一个分支urls.size() > 1, 此时针对每个url都会生成一个ClusterInvoker,然后将其放入StaticDirectory管理。对于StaticDirectory来说,其下的任意一个注册中心可用则其可用。
再回头看ReferenceConfig生成代理的最后一步:return (T) proxyFactory.getProxy(invoker); 这里默认的实现为com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory, 它的getProxy方法:
-
- public <T> T getProxy(Invoker<T> invoker) throws RpcException {
- Class<?>[] interfaces = null;
- String config = invoker.getUrl().getParameter("interfaces");
- if (config != null && config.length() > 0) {
-
- 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]);
- }
- }
- }
-
- if (interfaces == null) {
- interfaces = new Class<?>[] {invoker.getInterface(), EchoService.class};
- }
- return getProxy(invoker, interfaces);
- }
-
-
- public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
-
- return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
- }
这里需要注意的是,生成的consumer代理除了实现指定接口外,还需要实现EchoService(支持回声测深,用于检查服务是否可用)。接下来就是真正的代理类生成了,对于DemoService来说,其生成的类代码如下:- 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;
-
-
- public class proxy0 implements ClassGenerator.DC, EchoService, DemoService {
-
- 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(。。。)。