如何创建一个Dubbo的Demo

  • 2019 年 10 月 5 日
  • 筆記

首先祝所有的小伙伴中秋快乐,今天让我们来写一个简单的Dubbo-Demo,Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。这个Demo就是要创建两个应用,一个作为服务的提供者,一个作为服务的消费者。通过Dubbo来实现服务消费者远程调用服务提供者的方法。

首先,要开启zookeeper注册中心,这里使用Windows版zookeeper注册中心

下载地址:http://mirror.bit.edu.cn/apache/zookeeper/

下载完成加压到自己想要安装的目录盘符即可,找到zookeeper/bin目录的zkServer.cmd双击启动;

出现以下界面就是启动成功了;

然后让我们先写一个dubbo的服务者:dubbodemo_provider

步骤如下

  • 创建web项目: dubbodemo_provider
  • 添加pom.xml依赖
  • 编写web.xml
  • 编写dubbo-provider.xml
  • 编写服务接口
  • 编写服务实现
  • 启动tomcat,发布项目

创建maven-web项目,不知道怎么创建的小伙伴可以看我之前发表过的idea如何创建maven-web项目{IDEA配置Maven教程},里面有详细的介绍maven配置及如何创建一个web项目;

添加pom.xml依赖

  <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.0modelVersion>    <groupId>com.mobaijungroupId>    <artifactId>dubbodemo_providerartifactId>    <version>1.0-SNAPSHOTversion>    <packaging>warpackaging>    <properties>      <spring.version>5.0.2.RELEASEspring.version>    properties>    <dependencies>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-contextartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-beansartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-webmvcartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-jdbcartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-aspectsartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-jmsartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-context-supportartifactId>        <version>${spring.version}version>      dependency>        <dependency>        <groupId>com.alibabagroupId>        <artifactId>dubboartifactId>        <version>2.6.6version>      dependency>      <dependency>        <groupId>io.nettygroupId>        <artifactId>netty-allartifactId>        <version>4.1.32.Finalversion>      dependency>      <dependency>        <groupId>org.apache.curatorgroupId>        <artifactId>curator-frameworkartifactId>        <version>4.0.0version>        <exclusions>          <exclusion>            <groupId>org.apache.zookeepergroupId>            <artifactId>zookeeperartifactId>          exclusion>        exclusions>      dependency>      <dependency>        <groupId>org.apache.zookeepergroupId>        <artifactId>zookeeperartifactId>        <version>3.4.7version>      dependency>      <dependency>        <groupId>com.github.sgroschupfgroupId>        <artifactId>zkclientartifactId>        <version>0.1version>      dependency>    dependencies>  project>  

编写web.xml

  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns="http://java.sun.com/xml/ns/javaee"           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                               http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"           version="2.5">          <context-param>          <param-name>contextConfigLocationparam-name>          <param-value>classpath*:dubbo-provider.xmlparam-value>      context-param>      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>      listener>    web-app>

编写dubbo-provider.xml,添加日志配置文件

dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"         xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://code.alibabatech.com/schema/dubbo         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">        <!--指定服务提供者名称,通常为项目名称。可以随意定义,唯一即可。-->      <dubbo:application name="dubbodemo_provider"/>        <!--配置注册中心地址          IP:这里的IP地址配置zookeeper注册中心的IP地址          -->      <dubbo:registry address="zookeeper://192.168.0.0:2181"/>      <!--       配置请求协议              name 指定的是传输协议的名称,                   值列表范围如:dubbo rmi hessian webservice http              port:服务提供者的真实请求端口      -->      <dubbo:protocol name="dubbo" port="20881"/>        <!--配置dubbo服务提供者的包扫描-->      <dubbo:annotation package="com.mobaijun.service"/>    </beans>  

编写服务接口HelloDubboService.java

package com.mobaijun.service;    /**   * @author Auser·杰   */  public interface HelloDubboService {        String DubboHello(String name);  }  

编写服务实现。注意这里的@Service引入的包是阿里巴巴的。

package com.mobaijun.service.impl;    import com.alibaba.dubbo.config.annotation.Service;  import com.mobaijun.service.HelloDubboService;      /**   * @Author:Auser·杰   * @DATE:2019/9/12 15:18   */  @Service  public class HelloDubboServiceImpl implements HelloDubboService {      @Override      public String DubboHello(String name) {          return  "hello :" + name;      }  }  

部署tomcat,启动项目dubbodemo_provider

二:添加Dubbo的服务消费者:dubbodemo_consumer

步骤如下

  • 创建web项目: dubbodemo_consumer
  • 添加依赖
  • 编写web.xml
  • 编写dubbo-consumer.xml
  • 编写服务接口 (客户端接口要与服务端接口一样:包类方法参数返回值)
  • 编写控制器,调用服务
  • 启动tomcat,测试,观察服务是否调用成功。

创建web项目: dubbodemo_consumer,添加pom.xml依赖

添加项目依赖

    <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.0modelVersion>    <groupId>com.mobaijungroupId>    <artifactId>dubbodemo_consumerartifactId>    <version>1.0-SNAPSHOTversion>    <packaging>warpackaging>    <properties>      <spring.version>5.0.2.RELEASEspring.version>    properties>    <dependencies>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-contextartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-beansartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-webmvcartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-jdbcartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-aspectsartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-jmsartifactId>        <version>${spring.version}version>      dependency>      <dependency>        <groupId>org.springframeworkgroupId>        <artifactId>spring-context-supportartifactId>        <version>${spring.version}version>      dependency>        <dependency>        <groupId>com.alibabagroupId>        <artifactId>dubboartifactId>        <version>2.6.6version>        <exclusions>          <exclusion>            <groupId>org.springframeworkgroupId>            <artifactId>spring-webartifactId>          exclusion>          <exclusion>            <groupId>org.springframeworkgroupId>            <artifactId>spring-beansartifactId>          exclusion>          <exclusion>            <groupId>org.springframeworkgroupId>            <artifactId>spring-contextartifactId>          exclusion>        exclusions>      dependency>      <dependency>        <groupId>io.nettygroupId>        <artifactId>netty-allartifactId>        <version>4.1.32.Finalversion>      dependency>      <dependency>        <groupId>org.apache.curatorgroupId>        <artifactId>curator-frameworkartifactId>        <version>4.0.0version>        <exclusions>          <exclusion>            <groupId>org.apache.zookeepergroupId>            <artifactId>zookeeperartifactId>          exclusion>        exclusions>      dependency>      <dependency>        <groupId>org.apache.zookeepergroupId>        <artifactId>zookeeperartifactId>        <version>3.4.7version>      dependency>      <dependency>        <groupId>com.github.sgroschupfgroupId>        <artifactId>zkclientartifactId>        <version>0.1version>      dependency>      <dependency>        <groupId>javax.servletgroupId>        <artifactId>servlet-apiartifactId>        <version>2.5version>      dependency>    dependencies>  project>

编写web.xml

  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns="http://java.sun.com/xml/ns/javaee"           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                               http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"           version="2.5">      <servlet>          <servlet-name>springmvcservlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>          <init-param>              <param-name>contextConfigLocationparam-name>              <param-value>classpath:dubbo-consumer.xmlparam-value>          init-param>          <load-on-startup>1load-on-startup>      servlet>      <servlet-mapping>          <servlet-name>springmvcservlet-name>          <url-pattern>*.dourl-pattern>      servlet-mapping>  web-app>

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"         xmlns:context="http://www.springframework.org/schema/context"         xmlns:mvc="http://www.springframework.org/schema/mvc"         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">        <!--开启SpringMVC注解扫描,扫描@Controller注解-->      <context:component-scan base-package="com.mobaijun.controller"/>      <!--SpringMVC注解驱动-->      <mvc:annotation-driven/>      <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->      <dubbo:application name="dubbodemo_consumer">          <dubbo:parameter key="qos.enable" value="false"/>      </dubbo:application>        <!--配置注册中心地址-->      <dubbo:registry address="zookeeper://192.168.0.0:2181"/>        <!--开启dubbo注解扫描(@Reference注解)-->      <dubbo:annotation package="com.mobaijun.web"/>  </beans>  

编写服务接口。 注意:这里的接口类名称要与服务端接口名称一致;路径也要一致。

package com.mobaijun.service;    /**   * @Author:Auser·杰   * @DATE:2019/9/13 15:31   */  public interface HelloDubboService {      String DubboHello(String name);  }

编写控制器,调用服务HelloDubboController

package com.mobaijun.controlle;  import com.alibaba.dubbo.config.annotation.Reference;  import com.mobaijun.service.HelloService;  import org.springframework.stereotype.Controller;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.ResponseBody;  /**   * @Author:Auser·杰   * @DATE:2019/9/12 15:32   */  @Controller  public class HelloDubboController {      @Reference      private HelloDubboService helloDubboService;      @RequestMapping("hello")      @ResponseBody      public String hello(){          String str  = helloDubboService.DubboHello("lisi");          System.out.println(str);          return str;      }  }

启动tomcat,测试,观察服务是否调用成功。