Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 959216b

Browse filesBrowse files
committed
[A] 添加服务暴露“
1 parent 07102bc commit 959216b
Copy full SHA for 959216b

File tree

Expand file treeCollapse file tree

3 files changed

+114
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+114
-0
lines changed
Open diff view settings
Collapse file
+114Lines changed: 114 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
```
2+
package org.apache.dubbo.rpc;
3+
import org.apache.dubbo.common.extension.ExtensionLoader;
4+
public class Protocol$Adaptive implements org.apache.dubbo.rpc.Protocol {
5+
public void destroy() {
6+
throw new UnsupportedOperationException("The method public abstract void org.apache.dubbo.rpc.Protocol.destroy() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
7+
}
8+
public int getDefaultPort() {
9+
throw new UnsupportedOperationException("The method public abstract int org.apache.dubbo.rpc.Protocol.getDefaultPort() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
10+
}
11+
public org.apache.dubbo.rpc.Invoker refer(java.lang.Class arg0, org.apache.dubbo.common.URL arg1) throws org.apache.dubbo.rpc.RpcException {
12+
if (arg1 == null) throw new IllegalArgumentException("url == null");
13+
org.apache.dubbo.common.URL url = arg1;
14+
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
15+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
16+
org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
17+
return extension.refer(arg0, arg1);
18+
}
19+
public org.apache.dubbo.rpc.Exporter export(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
20+
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
21+
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
22+
org.apache.dubbo.common.URL url = arg0.getUrl();
23+
String extName = ( url.getProtocol() == null ? "dubbo" : url.getProtocol() );
24+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.Protocol) name from url (" + url.toString() + ") use keys([protocol])");
25+
// 此处传入的extName为dubbo,extension获得的扩展点实现类为ProtocolFilterWrapper
26+
org.apache.dubbo.rpc.Protocol extension = (org.apache.dubbo.rpc.Protocol)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.Protocol.class).getExtension(extName);
27+
return extension.export(arg0);
28+
}
29+
public java.util.List getServers() {
30+
throw new UnsupportedOperationException("The method public default java.util.List org.apache.dubbo.rpc.Protocol.getServers() of interface org.apache.dubbo.rpc.Protocol is not adaptive method!");
31+
}
32+
}
33+
```
34+
35+
```
36+
package org.apache.dubbo.rpc;
37+
import org.apache.dubbo.common.extension.ExtensionLoader;
38+
public class ProxyFactory$Adaptive implements org.apache.dubbo.rpc.ProxyFactory {
39+
public java.lang.Object getProxy(org.apache.dubbo.rpc.Invoker arg0) throws org.apache.dubbo.rpc.RpcException {
40+
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
41+
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
42+
org.apache.dubbo.common.URL url = arg0.getUrl();
43+
String extName = url.getParameter("proxy", "javassist");
44+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
45+
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
46+
return extension.getProxy(arg0);
47+
}
48+
public java.lang.Object getProxy(org.apache.dubbo.rpc.Invoker arg0, Boolean arg1) throws org.apache.dubbo.rpc.RpcException {
49+
if (arg0 == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument == null");
50+
if (arg0.getUrl() == null) throw new IllegalArgumentException("org.apache.dubbo.rpc.Invoker argument getUrl() == null");
51+
org.apache.dubbo.common.URL url = arg0.getUrl();
52+
String extName = url.getParameter("proxy", "javassist");
53+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
54+
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
55+
return extension.getProxy(arg0, arg1);
56+
}
57+
public org.apache.dubbo.rpc.Invoker getInvoker(java.lang.Object arg0, java.lang.Class arg1, org.apache.dubbo.common.URL arg2) throws org.apache.dubbo.rpc.RpcException {
58+
if (arg2 == null) throw new IllegalArgumentException("url == null");
59+
org.apache.dubbo.common.URL url = arg2;
60+
String extName = url.getParameter("proxy", "javassist");
61+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.rpc.ProxyFactory) name from url (" + url.toString() + ") use keys([proxy])");
62+
// 这里获取到的ProxyFactory的扩展点是StubProxyFactoryWrapper,StubProxyFactoryWrapper是ProxyFactory的实现类,@SPI("javassist")
63+
org.apache.dubbo.rpc.ProxyFactory extension = (org.apache.dubbo.rpc.ProxyFactory)ExtensionLoader.getExtensionLoader(org.apache.dubbo.rpc.ProxyFactory.class).getExtension(extName);
64+
// 所以去调用了StubProxyFactoryWrapper#getInvoker()方法,在该方法中调用的是JavassistProxyFactory的getInvoker方法,最终实际
65+
// 返回的是一个JavassistProxyFactory的一个匿名内部类:AbstractProxyInvoker
66+
return extension.getInvoker(arg0, arg1, arg2);
67+
}
68+
}
69+
```
70+
71+
```
72+
package org.apache.dubbo.remoting;
73+
import org.apache.dubbo.common.extension.ExtensionLoader;
74+
public class Transporter$Adaptive implements org.apache.dubbo.remoting.Transporter {
75+
public org.apache.dubbo.remoting.Client connect(org.apache.dubbo.common.URL arg0, org.apache.dubbo.remoting.ChannelHandler arg1) throws org.apache.dubbo.remoting.RemotingException {
76+
if (arg0 == null) throw new IllegalArgumentException("url == null");
77+
org.apache.dubbo.common.URL url = arg0;
78+
String extName = url.getParameter("client", url.getParameter("transporter", "netty"));
79+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.remoting.Transporter) name from url (" + url.toString() + ") use keys([client, transporter])");
80+
org.apache.dubbo.remoting.Transporter extension = (org.apache.dubbo.remoting.Transporter)ExtensionLoader.getExtensionLoader(org.apache.dubbo.remoting.Transporter.class).getExtension(extName);
81+
return extension.connect(arg0, arg1);
82+
}
83+
public org.apache.dubbo.remoting.RemotingServer bind(org.apache.dubbo.common.URL arg0, org.apache.dubbo.remoting.ChannelHandler arg1) throws org.apache.dubbo.remoting.RemotingException {
84+
if (arg0 == null) throw new IllegalArgumentException("url == null");
85+
org.apache.dubbo.common.URL url = arg0;
86+
String extName = url.getParameter("server", url.getParameter("transporter", "netty"));
87+
if(extName == null) throw new IllegalStateException("Failed to get extension (org.apache.dubbo.remoting.Transporter) name from url (" + url.toString() + ") use keys([server, transporter])");
88+
// 这里获取的扩展点实现类是netty4包下的NettyTransporter
89+
org.apache.dubbo.remoting.Transporter extension = (org.apache.dubbo.remoting.Transporter)ExtensionLoader.getExtensionLoader(org.apache.dubbo.remoting.Transporter.class).getExtension(extName);
90+
return extension.bind(arg0, arg1);
91+
}
92+
}
93+
```
94+
95+
对于Invoker对象是JavassistProxyFactory的匿名内部类,如下图所示
96+
![export01](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/Dubbo/export01.png)
97+
98+
99+
- testDubboProtocol()调用流程
100+
101+
1. 首先protocol和proxy变量分别在DubboProtocolTest.java类加载过程时就已经分别加载好了,这里的Protocol和ProxyFactory对象都生成的是代理对象,分别为Protocol$Adaptive和ProxyFactory$Adaptive对象,对象类代码在上面;
102+
2. 首先调用proxy#getInvoker(),调用的实际是ProxyFactory$Adaptive对象的getInvoker()方法,在上图可以看到具体逻辑,其方法逻辑中需要去ProxyFactory对象的扩展点实现类,经过调试ProxyFactory对象的扩展点实现类为StubProxyFactoryWrapper,最终会调用到JavassistProxyFactory的getInvoker()方法,它会最终生成一个AbstractProxyInvoker()匿名类对象,所以调试的时候可以发现invoker的引用是JavassistProxyFactory$1;
103+
3. 获取到invoker之后,就传入到Protocol的扩展点实现类中去调用export()方法,由上图的Protocol$Adaptive可以知道,在export()方法中会去生成Protocol的扩展点实现类,这里的实现类是ProtocolFilterWrapper(这里为什么是这个实现类呢?去调研一下)。经过代理类的传递包装,最终来到了ProtocolFilterWrapper#export()方法;
104+
4. 接着就是调用ProtocolFilterWrapper#buildInvocation()方法,构建调用链。就是先去获取Filter的扩展点实现类集合对象,然后倒叙遍历该集合对象然后将invoker对象和最后一个Filter对象封装为另一个Invoker,然后再继续传递到和上一个Filter对象继续封装成Invoker,以此往复封装。最终的结果就是将传入的invoker对象封装到了最后一个Filter之后,请求进来之后需要调用所有的Filter过后才会调用invoker对象,形成了一个Invoker链;
105+
106+
效果如下:
107+
![export02](https://github.com/coderbruis/JavaSourceCodeLearning/blob/master/note/images/Dubbo/export02.png)
108+
109+
还需要注意的是,在ProtocolFilterWrapper#buildInvokerChain()方法中,生成的是匿名对象,即将3中传递过来的JavassistProxyFactory$1又包装起来放在了ProtocolFilterWrapper对象中,所以你会发现此时的invoker对象是ProtocolFilterWrapper$1;
110+
111+
5. 在ProtocolFilterWrapper#export()中,会调用ProtocolListenerWrapper#export()对象,在ProtocolListenerWrapper#export()中会返回一个ListenerExporterWrapper的匿名内部类对象,生成该匿名内部类之前会调用DubboProtocol#export()方法。
112+
6. 千辛万苦,终于来到了DubboProtocol#export()的openServer()方法了,在openServer()方法中,会调用createServer(url)方法去创建Server;在DubboProtocol中会调用Exchangers#bind()去和Netty服务端进行绑定?
113+
7. 随后调用Exchangers#getExchanger()方法去获取Exchanger的扩展点实现类,他的扩展点实现类是HeaderExchanger,即调用的是HeaderExchagner#bind()方法。在其方法中会构造一个HeaderExchangeServer对象,但是在传入入参时需要调用Transporters#bind()方法,将返回的对象作为入参。
114+
8. 随后调用Transporters#getTransporter()方法,去获取Transporter的自适应扩展点实现类,这里实际获取的是一个代理类,是Transporter$Adaptive,类实现体在上面。接着就是调用的Transporter$Adaptive的bind()方法。最终去调用的netty4包下的NettyTransporter#bind()
Collapse file

‎note/images/Dubbo/export01.png‎

Copy file name to clipboard
23.5 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎note/images/Dubbo/export02.png‎

Copy file name to clipboard
95.1 KB
  • Display the source diff
  • Display the rich diff
Loading

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.