码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-02-09 15:06:40      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:import   mes   moni   imp   配置文件   start   sum   提供者   segment   

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启动的一些理解

标签:import   mes   moni   imp   配置文件   start   sum   提供者   segment   

原文地址:http://blog.51cto.com/woodpecker/2070546

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!