dubbo的服务提供者provider启动的一些理解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo的服务提供者provider启动的一些理解相关的知识,希望对你有一定的参考价值。

dubbo的demo中的provider实现很简单,
1.接口如下:

package com.alibaba.dubbo.demo;

public interface DemoService {

    String sayHello(String name);

}

2.实现类:

package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;
import com.alibaba.dubbo.rpc.RpcContext;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }

}

3.主要配置文件dubbo-demo-provider.xml内容如下:

<dubbo:application name="demo-provider"/>

    <!-- use multicast registry center to export service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

4.main类:

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws Exception {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();

        System.in.read(); // press any key to exit
    }

}

5.那么dubbo配置文件的解析是怎么解决的呢?是什么时候开始的呢?

1)在源码的dubbo-config下的dubo-config-spring中的META-INF下面有两个文件:
spring.schemas和spring.handlers

spring.schemas内容为:http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

解说:dubbo.xsd是dubbo在spring中可以使用的元素的定义,如protocol,registry等

spring.handlers内容为:
http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler

解说:DubboNamespaceHandler是注册dubbo中用到的各个元素的解析类,它的源码内容如下:

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 DubboBeanDefinitionParser(AnnotationBean.class, true));
    }

而DubboBeanDefinitionParser是具体解析的类,解析dubbo-demo-provider.xml中的配置,并封装相关的bean.
从而进行服务启动的一些准备,包括服务export,注册等。

2)什么开始的,通过下面的截图可以一目了然:

技术分享图片

注:1)dubbo的源码地址:https://github.com/alibaba/dubbo
2)spring schema的学习请看以下链接:https://segmentfault.com/a/1190000007047168

以上是关于dubbo的服务提供者provider启动的一些理解的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo记录-基本原理

Dubbo架构和原理

Dubbo 用户手册学习笔记 —— Dubbo架构

Dubbo服务提供者几种启动方式

Consul 实现服务提供者和服务消费者

Dubbo01