Dubbo2.7源码分析-如何发布服务 (3)

injvm://127.0.0.1/org.apache.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.124.1&bind.port=20880&dubbo=2.0.2&generic=false&interface=org.apache.dubbo.demo.DemoService&methods=sayHello&pid=3008&qos.port=22222&side=provider&timestamp=1536125473655

重点看这一句:

Exporter<?> exporter = protocol.export( proxyFactory.getInvoker(ref, (Class) interfaceClass, local));

其中涉及到ProxyFactory和Protocol,下面分别来看一看。

ProxyFactory

proxyFactory也是通过SPI加载的自适应类对象,类名为ProxyFactory$Adaptive,我们来看一下其class文件反编译后的源码

package org.apache.dubbo.rpc; import org.apache.dubbo.common.URL; import org.apache.dubbo.common.extension.ExtensionLoader; public class ProxyFactory$Adaptive implements ProxyFactory { public ProxyFactory$Adaptive() { } public Invoker getInvoker(Object var1, Class var2, URL var3) throws RpcException { if (var3 == null) { throw new IllegalArgumentException("url == null"); } else { String var5 = var3.getParameter("proxy", "javassist"); if (var5 == null) { throw new IllegalStateException("Fail to get extension(org.apache.dubbo.rpc.ProxyFactory) name from url(" + var3.toString() + ") use keys([proxy])"); } else { ProxyFactory var6 = (ProxyFactory)ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension(var5); return var6.getInvoker(var1, var2, var3); } } } public Object getProxy(Invoker var1, boolean var2) throws RpcException { if (var1 == null) { throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null"); } else if (var1.getUrl() == null) { throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null"); } else { URL var3 = var1.getUrl(); String var4 = var3.getParameter("proxy", "javassist"); if (var4 == null) { throw new IllegalStateException("Fail to get extension(org.apache.dubbo.rpc.ProxyFactory) name from url(" + var3.toString() + ") use keys([proxy])"); } else { ProxyFactory var5 = (ProxyFactory)ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension(var4); return var5.getProxy(var1, var2); } } } public Object getProxy(Invoker var1) throws RpcException { if (var1 == null) { throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null"); } else if (var1.getUrl() == null) { throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null"); } else { URL var2 = var1.getUrl(); String var3 = var2.getParameter("proxy", "javassist"); if (var3 == null) { throw new IllegalStateException("Fail to get extension(org.apache.dubbo.rpc.ProxyFactory) name from url(" + var2.toString() + ") use keys([proxy])"); } else { ProxyFactory var4 = (ProxyFactory)ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension(var3); return var4.getProxy(var1); } } } }

其中有三个方法,两个获取代理,一个获取Invoker。我们来看其中的getInvoker方法,默认获取名称为javassist的ProxyFactory。
由于本地服务URL中没有proxy参数,所以会调用JavassistProxyFactory的getInvoker(T proxy, Class type, URL url)方法,返回AbstractProxyInvoker的匿名类对象,此对象代理了服务对象(本示例中为DemoServiceImpl对象)。

其实(ProxyFactory)ExtensionLoader.getExtensionLoader(ProxyFactory.class).getExtension("javassist");获取到的并不是JavassistProxyFactory对象,而是StubProxyFactoryWrapper对象,为什么呢?我们可以看下ExtensionLoader的getExtension(String name)方法

public T getExtension(String name) { //检查name是否合法 if (name == null || name.length() == 0) throw new IllegalArgumentException("Extension name == null"); //如果name等于true,则加载SPI的默认插件 if ("true".equals(name)) { return getDefaultExtension(); } //从当前插件类的缓存实例对象中获取 Holder<Object> holder = cachedInstances.get(name); if (holder == null) { cachedInstances.putIfAbsent(name, new Holder<Object>()); holder = cachedInstances.get(name); } Object instance = holder.get(); if (instance == null) { synchronized (holder) { instance = holder.get(); if (instance == null) { //创建插件实例 instance = createExtension(name); holder.set(instance); } } } return (T) instance; } private T createExtension(String name) { //从文件目录中加载插件类 Class<?> clazz = getExtensionClasses().get(name); if (clazz == null) { throw findException(name); } //从已加载的所有插件实例集合中获取 try { T instance = (T) EXTENSION_INSTANCES.get(clazz); if (instance == null) { //实例化插件实例,并放入集合 EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance()); instance = (T) EXTENSION_INSTANCES.get(clazz); } //注入属性 injectExtension(instance); //插件的包裹类 Set<Class<?>> wrapperClasses = cachedWrapperClasses; if (wrapperClasses != null && !wrapperClasses.isEmpty()) { for (Class<?> wrapperClass : wrapperClasses) { instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance)); } } return instance; } catch (Throwable t) { throw new IllegalStateException("Extension instance(name: " + name + ", class: " + type + ") could not be instantiated: " + t.getMessage(), t); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxzjg.html