Dubbo学习(三)HelloWorld

准备

  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
    消费者调用到服务

生产者服务被调用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,987评论 6 342
  • 0 准备 安装注册中心:Zookeeper、Dubbox自带的dubbo-registry-simple;安装Du...
    七寸知架构阅读 14,027评论 0 88
  • 最近被挪威的剧吸引了——《skam》,不知当初是谁推荐的这部电视剧,所以早在它第四季上来之前就已经有看前几季了,...
    王先生ffff阅读 194评论 0 2
  • 断桥衫冷寒霜迹 孤山襟短椅云齐 残月荡空情未灭 玉壶倾尽神已迷 恍闻飞凤舞青影 又见惊鸿逐彩霓 偏有漏长叹永夜 觉...
    隐天痕阅读 248评论 0 0