美文网首页菜鸟要飞
Dubbo学习(三)HelloWorld

Dubbo学习(三)HelloWorld

作者: 万总有点菜 | 来源:发表于2017-10-26 11:45 被阅读34次

    准备

    1. 创建一个maven主工程
    1. 创建三个子模块,接口模块dubboapi、生产者模块dubboprovider、消费者模块dubboconsumer
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.maple.dubbodemo</groupId>
        <artifactId>dubbo-root</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>dubboapi</module>
            <module>dubboprovider</module>
            <module>dubboconsumer</module>
        </modules>
    </project>
    

    接口模块dubboapi

    1. 创建接口
    package com.maple.dubbodemo;
    
    /**
     * Created by maple on 2017-10-26.
     */
    public interface HelloService {
        public String sayHello(String content);
    }
    

    生产者模块dubboprovider

    1. 引入接口模块,在pom.xml配置
    <dependency>
           <groupId>com.maple.dubbodemo</groupId>
           <artifactId>dubbo-api</artifactId>
           <version>${project.parent.version}</version>
    </dependency>
    
    1. 引入dubbo以及spring的包,在pom.xml配置
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
    </dependency>
     <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
    <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.10</version>
                <type>pom</type>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
    <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
    </dependency>
    
    1. 实现接口
    package com.maple.dubbodemo.provider;
    
    import com.alibaba.dubbo.rpc.RpcContext;
    import com.maple.dubbodemo.HelloService;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
     * Created by maple on 2017-10-26.
     */
    public class HelloServiceImpl implements HelloService {
        public String sayHello(String content) {
            System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + content + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
            return "Hello " + content + ", response form provider: " + RpcContext.getContext().getLocalAddress();
        }
    }
    
    1. 生产者spring配置,路径\src\main\resources\META-INF\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://code.alibabatech.com/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-2.5.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="demo-provider"/>
    
        <!-- 使用注册中心暴露服务地址 -->
        <dubbo:registry id="myzk" address="127.0.0.1:2181" protocol="zookeeper"/>
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol id="mydubbo" name="dubbo" port="20880"/>
    
        <dubbo:provider registry="myzk" protocol="mydubbo"/>
    
        <!-- 和本地bean一样实现服务 -->
        <bean id="helloService" class="com.maple.dubbodemo.provider.HelloServiceImpl"/>
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.maple.dubbodemo.HelloService" ref="helloService"/>
    
    </beans>
    
    1. 创建生产者
    package com.maple.dubbodemo.provider;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by maple on 2017-10-26.
     */
    public class Provider {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
            context.start();
            System.in.read(); // 按任意键退出
        }
    }
    
    1. 运行main,查看dubbo-admin;
    1. 模块结构

    消费者模块dubboconsumer

    1. 引入接口模块,在pom.xml配置
    <dependency>
           <groupId>com.maple.dubbodemo</groupId>
           <artifactId>dubbo-api</artifactId>
           <version>${project.parent.version}</version>
    </dependency>
    
    1. 引入dubbo以及spring的包,在pom.xml配置
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
    </dependency>
     <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
    <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.10</version>
                <type>pom</type>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
    <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
    </dependency>
    
    1. 消费者spring配置,路径\src\main\resources\META-INF\spring下创建配置文件dubbo-demo-consumer.xml;
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="demo-consumer"/>
    
        <!-- 使用注册中心暴露发现服务地址 -->
        <dubbo:registry id="zk" address="127.0.0.1:2181" protocol="zookeeper"/>
    
        <dubbo:consumer registry="zk"/>
    
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="helloService" check="false" interface="com.maple.dubbodemo.HelloService"/>
    </beans>
    
    1. 创建消费者
    package com.maple.dubbodemo.consumer;
    
    import com.maple.dubbodemo.HelloService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by maple on 2017-10-26.
     */
    public class Consumer {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
            context.start();
    
            HelloService helloService = (HelloService) context.getBean("helloService"); // 获取远程服务代理
            String hello = helloService.sayHello("world"); // 执行远程方法
    
            System.out.println(hello); // 显示调用结果
        }
    }
    
    1. 运行main
      消费者调用到服务

    生产者服务被调用

    相关文章

      网友评论

        本文标题:Dubbo学习(三)HelloWorld

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