Dubbo demo

2019-07-11  本文已影响0人  鸡龙

先来了解一下何为dubbo,与zookeeper有什么关系。

一、环境搭建

Window、idea2019.1.1、JDK8、zookeeper-3.5.0-alpha、dubbo2.5.4
Dubbo+Zookeeper 的下载地址
1、zookeeper-3.5.0-alpha
2、dubbo-admin.2.5.4
代码github地址

1、Zookeeper

zookeeper文件夹结构。配置文件在conf下,运行文件在bin下。


zookeeper

解压后对conf下的zoo_sample.cfg文件进行复制副本备份,再将其文件名更改为zoo.cfg后,进入修改配置


开放服务端口8081
保存配置后,进入bin,双击zkServer.cmd即可运行zookeeper。
由于某些原因可以导致报错导致出现闪退情况,在zkServer.cmd的末尾加上pause,可以在运行的时候查看异常栈
pause添加处

看到这行就代表启动成功了


启动成功

2、Dubbo

下载好dubbo后解压到tomcat的webapps下,将文件夹名字更改为ROOT,将ROOT默认文件替换。这样可以直接访问127.0.0.1:8080直接访问dubbo Admin。(当然也可以不设置为默认访问)。随后即可以访问http://127.0.0.1:8080输入root,root登录dubbo admin。


dubbo界面
注:第一第二步缺一不可。

3、搭建时遇到的迷之错误

启动Zookeeper时遇到:Hadoop Error: JAVA_HOME is incorrectly set问题。

因为路径中包含空格会导致报错,将环境变量中的JAVA_HOME有空格的路径用双引号括住即可以解决。


JAVA_HOME

二、创建项目

1、工程目录

Dubbo-parent为总工程,dubbo-provider,dubbo-consumer,dubbo-api都是子模块,dubbo-provider,dubbo-consumer又依赖于dubbo-api模块。一个工程三个模块。

依赖图 工程目录
2、创建Dubbo空工程
3、在空工程下创建dubbo-parent总模块
选择simple
完成创建

完成创建后,配置他的pom.xml,如果你的dubbo或zookeeper版本和本文连接中的不一样,需要在pom中自己修改。
maven 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.0</modelVersion>

  <groupId>com.dubbo.parent</groupId>
  <artifactId>dubbo-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>../dubbo-provider</module>
    <module>../dubbo-consumer</module>
    <module>../dubbo-api</module>
  </modules>
  <packaging>pom</packaging>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- spring版本号 -->
    <spring.version>4.2.5.RELEASE</spring.version>

    <!-- mybatis版本号 -->
    <mybatis.version>3.2.8</mybatis.version>

    <!-- mysql驱动版本号 -->
    <mysql-driver.version>5.1.29</mysql-driver.version>

    <!-- log4j日志包版本号 -->
    <slf4j.version>1.7.18</slf4j.version>
    <log4j.version>1.2.17</log4j.version>

  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 添加jstl依赖 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
    </dependency>

    <!-- 添加junit4依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <!-- 指定范围,在测试时才会加载 -->
      <scope>test</scope>
    </dependency>

    <!-- 添加spring核心依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- 添加mybatis依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>

    <!-- 添加mybatis/spring整合包依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- 添加mysql驱动依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql-driver.version}</version>
    </dependency>
    <!-- 添加数据库连接池依赖 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- 添加fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.22</version>
    </dependency>

    <!-- 添加日志相关jar包 -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <!-- log end -->
    <!-- 映入JSON -->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.0</version>
    </dependency>

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.9</version>
    </dependency>

    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.2.1</version>
    </dependency>

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-core</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-web</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-ehcache</artifactId>
      <version>1.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.5.0-alpha</version>
    </dependency>
    <!-- dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.4</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.101tec</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.10</version>
    </dependency>
    <dependency>
      <groupId>com.dubbo.api</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>

  </dependencies>
  <distributionManagement>
    <site>
      <id>website</id>
      <url>scp://webhost.company.com/www/website</url>
    </site>
  </distributionManagement>
  <build>
    <finalName>dubbo-parent</finalName>
  </build>
</project>

4、构建dubbo-api,dubbo-provider,dubbo-consumer模块



三个模块的创建方式都差不多,下面只放出三个模块的pom

provider pom配置
<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">
    <parent>
        <artifactId>dubbo-parent</artifactId>
        <groupId>com.dubbo.parent</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../dubbo-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dubbo.parent</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>dubbo-provider</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.dubbo.api</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>dubbo-provider</finalName>
    </build>
</project>
api pom
<?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.dubbo.api</groupId>
  <artifactId>dubbo-api</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>dubbo-api</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
consumer pom
<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">
  <parent>
    <artifactId>dubbo-parent</artifactId>
    <groupId>com.dubbo.parent</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../dubbo-parent/pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dubbo.consumer</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <packaging>war</packaging>

  <name>dubbo-consumer</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.dubbo.api</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>dubbo-consumer</finalName>
  </build>
</project>
5、编写接口

这里示例有两个接口

1)在api中需要定义的接口类

Userservice接口

package com.dubbo.api;
import java.util.List;
import java.util.Map;
public interface UserService {
    Map<String,Object> getUserInfo(int id);
    List getUserInfo(String str);
}

LoginService接口

package com.dubbo.api;
public interface LoginService {
    public String Login(String name,String password);
}
2)在provider中需要实现接口

UserServiceImpl类实现api中的UserService接口

package com.dubbo.provider;
import com.dubbo.api.UserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserServiceImpl implements UserService {
    @Override
    public Map<String, Object> getUserInfo(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("sex","男");
        map.put("id",id);
        return map;
    }
    @Override
    public List<String> getUserInfo(String str){
        List<String> list = new ArrayList<>();
        list.add(str);
        list.add("男");
        return list;
    }
}

LoginServiceImpl中实现api中的LoginService接口

package com.dubbo.provider;
import com.dubbo.api.LoginService;
public class LoginServiceImpl implements LoginService {
    @Override
    public String  Login(String name, String password) {
        if(name.equals("张三")&&password.equals("zhangsan")){
            return "正确";
        }
        return "错误";
    }
}

provider中应该要设置resoures,具体设置方式:
右键模块-->Open model settings


设置resoures文件夹

然后在设置的文件夹中创建provider.xml


provider.xml
provider中需要设置使用zookeeper暴露服务、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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
    <dubbo:service interface="com.dubbo.api.UserService" ref="userService" protocol="dubbo" />
    <dubbo:service interface="com.dubbo.api.LoginService" ref="LoginService" protocol="dubbo"/>
    <!--具体实现该接口的 bean-->
    <bean id="userService" class="com.dubbo.provider.UserServiceImpl"/>
    <bean id="LoginService" class="com.dubbo.provider.LoginServiceImpl"/>
</beans>

设置好xml和实现了接口后,需要一个入口Main来打开服务

package com.dubbo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

/**
 * Hello world!
 *
 */
public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context =
                 new ClassPathXmlApplicationContext("provider.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("服务已经启动...");
        System.in.read();
    }
}

配置完成后运行是可以正常启动的,并在dubbo admin中有该服务的信息



服务详情
3)在consumer中使用服务

因为要使用到服务中的接口,所以我们要先定义consumer.xml,和创建provider时一样,要先规定一个resoures,构建后如下图


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"
       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="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
    <dubbo:reference id="permissionService" interface="com.dubbo.api.UserService"/>
    <dubbo:reference id="permissionService1" interface="com.dubbo.api.LoginService"/>
</beans>
Consumer类调用接口测试
package com.dubbo.consumer;
import com.dubbo.api.UserService;
import com.dubbo.api.LoginService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
    public static void main(String [] args){
        ClassPathXmlApplicationContext context = 
                new  ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        System.out.println("consumer start");
        UserService userService = context.getBean(UserService.class);
        LoginService loginService = context.getBean(LoginService.class);
        System.out.println("consumer");
        System.out.println(userService.getUserInfo(1));
        System.out.println(userService.getUserInfo("张三"));
        System.out.println(loginService.Login("张三","zhangsan"));
    }
}
调用成功

对比之前无消费者,消费者正常显示


消费者正常

文章参考:

1、IntelliJ IDEA+Maven简单搭建dubbo框架
2、Dubbo与Zookeeper、SpringMVC整合和使用(入门级)
3、【MAVEN】maven系列--pom.xml标签详解
4、Maven项目创建后没有resource文件夹

上一篇下一篇

猜你喜欢

热点阅读