dubbo服務釋出過程
1. dubbo的服務釋出過程
1.暴露本地服務 -- 是指在同一個jvm中不用通過zk來遠端通訊例如在一個服務裡自己呼叫自己的介面
Export dubbo service com.alibaba.dubbo.demo.DemoService to local registry, dubbo version: 2.0.0, current host: 192.168.20.102。
1.1 dubbo生產者提供服務入口 -- <dubbo:service interface="...">
標籤自定義<dubbo:service>是通過和spring 結合類DubboNamespaceHandler進行類處理工作。
-
public class DubboNamespaceHandler extends NamespaceHandlerSupport {
-
static {
-
Version.checkDuplicate(DubboNamespaceHandler.class);
-
}
-
public void init() {
-
registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
-
registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
-
registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
-
registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
-
registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
-
registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
-
registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
-
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
-
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
-
registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());
-
}
-
}
通過ServiceBean是主要處理服務。
1.2 獲取zk的服務註冊地址
<dubbo:registry address="zookeeper://106.14.123.227:2181" id="com.alibaba.dubbo.config.RegistryConfig" />
以及註冊資訊
{"path":"com.alibaba.dubbo.registry.RegistryService", "protocol":"dubbo","application":"demo-provider","qos.port":"22222","dubbo":"2.0.0","pid":"1172","timestamp":"1536507787716"}
拼接成多個(叢集zk)dubbo訪問請求的zookeeper路由地址,zookeeper://106.14.123.227:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.0&pid=1172&qos.port=22222×tamp=1536507787716
1.3 為每一種不同的服務協議(預設的dubbo協議這是一種tcp協議)
-
private void doExportUrls() {
-
List<URL> registryURLs = loadRegistries(true);
-
for (ProtocolConfig protocolConfig : protocols) {
-
doExportUrlsFor1Protocol(protocolConfig, registryURLs);
-
}
-
}
1.4 暴露本地服務
-
private void exportLocal(URL url) {
-
if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
-
URL local = URL.valueOf(url.toFullString())
-
.setProtocol(Constants.LOCAL_PROTOCOL)
-
.setHost(LOCALHOST)
-
.setPort(0);
-
ServiceClassHolder.getInstance().pushServiceClass(getServiceClass(ref));
-
Exporter<?> exporter = protocol.export(
-
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
-
exporters.add(exporter);
-
logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry");
-
}
-
}
"url": "dubbo://192.168.88.1:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.88.1&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=8136&qos.port=22222&side=provider×tamp=1536585127237"
介面proxyFactory是基於@SPI("javassit")的介面,@Adaptive註解在方法上表示代理。動態編譯類ProxyFactory$Adaptive
2.暴露遠端服務 -- 暴露給遠端客戶端的IP和埠號通過網路實現通訊
Export dubbo service com.alibaba.dubbo.demo.DemoService to url dubbo://192.168.20.102:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider & bind.ip =192.168.20.102&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=3068&qos.port=22222&side=provider×tamp=1535794806217, dubbo version: 2.0.0, current host: 192.168.20.102
Register dubbo service com.alibaba.dubbo.demo.DemoService url dubbo://192.168.20.102:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.20.102&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=3068&qos.port=22222&side=provider×tamp=1535794806217 to registry registry://106.14.123.227:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.0&pid=3068&qos.port=22222®istry=zookeeper×tamp=1535794806204, dubbo version: 2.0.0, current host: 192.168.20.102
3.啟動netty
Start NettyServer bind /0.0.0.0:20880, export /192.168.20.102:20880, dubbo version: 2.0.0, current host: 192.168.20.102
4.開啟zk
zookeeper.ClientCnxn: Opening socket connection to server 106.14.123.227/106.14.123.227:2181. Will not attempt to authenticate using SASL (unknown error)
5.註冊zk
Register: dubbo://192.168.20.102:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=3068&side=provider×tamp=1535794806217, dubbo version: 2.0.0, current host: 192.168.20.102
6.監聽zk
Subscribe:provider://192.168.20.102:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=3068&side=provider×tamp=1535794806217, dubbo version: 2.0.0, current host: 192.168.20.102
Notify urls for subscribe url provider://192.168.20.102:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=3068&side=provider×tamp=1535794806217, urls: [empty://192.168.20.102:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=3068&side=provider×tamp=1535794806217], dubbo version: 2.0.0, current host: 192.168.20.102