1. 程式人生 > >dubbo服務釋出過程

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進行類處理工作。

 
  1. public class DubboNamespaceHandler extends NamespaceHandlerSupport {

  2. static {

  3. Version.checkDuplicate(DubboNamespaceHandler.class);

  4. }

  5. public void init() {

  6. registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));

  7. registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));

  8. registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));

  9. registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));

  10. registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));

  11. registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));

  12. registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));

  13. registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));

  14. registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));

  15. registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());

  16. }

  17. }

 通過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&timestamp=1536507787716

1.3  為每一種不同的服務協議(預設的dubbo協議這是一種tcp協議)

 
  1. private void doExportUrls() {

  2. List<URL> registryURLs = loadRegistries(true);

  3. for (ProtocolConfig protocolConfig : protocols) {

  4. doExportUrlsFor1Protocol(protocolConfig, registryURLs);

  5. }

  6. }

1.4 暴露本地服務

 

 
  1. private void exportLocal(URL url) {

  2. if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {

  3. URL local = URL.valueOf(url.toFullString())

  4. .setProtocol(Constants.LOCAL_PROTOCOL)

  5. .setHost(LOCALHOST)

  6. .setPort(0);

  7. ServiceClassHolder.getInstance().pushServiceClass(getServiceClass(ref));

  8. Exporter<?> exporter = protocol.export(

  9. proxyFactory.getInvoker(ref, (Class) interfaceClass, local));

  10. exporters.add(exporter);

  11. logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry");

  12. }

  13. }

"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&timestamp=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&timestamp=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&timestamp=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&registry=zookeeper&timestamp=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&timestamp=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&timestamp=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&timestamp=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&timestamp=1535794806217], dubbo version: 2.0.0, current host: 192.168.20.102