美文网首页
Dubbo入门学习三—xml配置方式

Dubbo入门学习三—xml配置方式

作者: 哪吒小子 | 来源:发表于2018-12-24 22:54 被阅读0次

    在我看来Dubbo最为方便的还是通过Spring 容器来加载Dubbo的Bean配置。而最便捷的学习途径就是看官方文档和代码。这里贴出我的代码:NEZHA的GitHub代码仓库

    这里主要分成三部分:

    1. API
    2. Provider
    3. Consumer
    

    1、API的设计

    package com.nezha.demo.dubbo;
    
    public interface DemoService {
        String sayHello(String name);
    }
    

    2、Provide的代码

    1.api接口的实现类

    package com.nezha.demo.dubbo;
    
    import com.alibaba.dubbo.rpc.RpcContext;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DemoServiceImpl implements DemoService {
        @Override
        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 from provider: " + RpcContext.getContext().getLocalAddress();
        }
    }
    

    2.Dubbo的配置文件

    文件位于resources包下面:/resources/spring/dubbo-demo-provider.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- provider's application name, used for tracing dependency relationship -->
        <dubbo:application name="demo-provider"/>
    
        <!-- use multicast registry center to export service -->
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    
        <!-- 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.nezha.demo.dubbo.DemoServiceImpl"/>
    
        <!-- declare the service interface to be exported -->
        <dubbo:service version="1.0.0" interface="com.nezha.demo.dubbo.DemoService" ref="demoService"/>
    
    </beans>
    

    3.Provider服务提供者

    package com.nezha.demo.dubbo;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Provider {
    
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/dubbo-demo-provider.xml"});
            context.start();
            System.in.read(); // press any key to exit
        }
    }
    

    3、Consumer的代码

    1.Dubbo的xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
        don't set it same as provider -->
        <dubbo:application name="demo-consumer"/>
    
        <!-- use multicast registry center to discover service -->
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    
        <!-- generate proxy for the remote service, then demoService can be used in the same way as the
        local regular interface -->
        <dubbo:reference scope="remote" version="1.0.0" id="demoService" check="false" interface="com.nezha.demo.dubbo.DemoService"/>
    
    </beans>
    

    2.Consumer的代码

    package com.nezha.demo.dubbo;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Consumer {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/dubbo-demo-consumer.xml"});
            context.start();
            // get remote service proxy
            DemoService demoService = (DemoService) context.getBean("demoService");
            while (true) {
                try {
                    Thread.sleep(1000);
                    // call remote method
                    String hello = demoService.sayHello("world");
                    // get result
                    System.out.println(hello);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Dubbo入门学习三—xml配置方式

          本文链接:https://www.haomeiwen.com/subject/mhgtlqtx.html