Windows下dubbo环境搭建及初步使用

一、dubbo简介

  Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。

二、环境准备

  本案例使用环境:jdk(1.8)、maven(3.2+)、zookeeper(3.4.10)、eclipse(Oxygen.3a Release (4.7.3a))、tomcat(tomcat8)

  a、搭建注册中心(jdk、maven、tomcat、eclipse集成可以自行搜索安装)

    1、本次实例使用zookeeper作为注册中心,下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz

    也可以去官网下载想要的版本,官网地址:http://zookeeper.apache.org/releases.html

    2、解压 zookeeper-3.4.10.tar.gz,进入解压后conf目录下,复制zoo_sample.cfg文件,重命名为zoo.cfg即可

    3、进入解压后bin目录,点击zkServer.cmd即可

  b、搭建dubbo-admin控制台(可选)

    1、下载源码安装 ,下载地址:https://github.com/apache/incubator-dubbo/tree/dubbo-2.5.8

    也可以选择下载对应的版本。

    2、解压后,打开cmd进入dubbo-admin目录下,如我解压的目录为:D:\Test\dubbo-master\dubbo-admin,在cmd下输入命令:mvn clean install

    

    3、在target目录下生成dubbo-admin-2.5.8.war,然后将该war包放入tomcat的webapps目录下,启动tomcat, 访问localhost:[端口]/dubbo-admin-2.5.8,输入默认的用户名和密码:root/root

    例如我访问的地址为:http://localhost:8088/dubbo-admin-2.5.8/

    

三、基于步骤二搭建的环境,使用ide开发JavaEE项目

  a、新建一个maven父项目dubbo-demo-master,和三个maven子模块dubbo-demo-interface、dubbo-demo-service、dubbo-demo-web(不知道如何新建百度搜索:eclipse搭建maven多module项目)

    1、完成后,添加相关项目依赖和插件,最后其dubbo-demo-master的pom.xml内容为(该maven工程packaging属性为pom),删除该工程的src目录(父工程不需要,只管理依赖):

复制代码
<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>org.moy.maven</groupId>
    <artifactId>dubbo-demo-master</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springVersion>4.3.0.RELEASE</springVersion>
        <dubboVersion>2.5.8</dubboVersion>
        <zkclientVersion>0.10</zkclientVersion>
        <zookeeperVersion>3.4.10</zookeeperVersion>
        <slf4jVersion>1.7.21</slf4jVersion>
        <mybatisVersion>3.4.4</mybatisVersion>
        <mybatisSpringVersion>1.3.0</mybatisSpringVersion>
        <mysqlVersion>6.0.5</mysqlVersion>
        <druidVersion>1.1.6</druidVersion>
        <junitVersion>4.12</junitVersion>
        <jacksonVersion>2.9.3</jacksonVersion>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junitVersion}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${springVersion}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatisSpringVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatisVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druidVersion}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysqlVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4jVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jacksonVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${springVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${springVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${springVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclientVersion}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeperVersion}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubboVersion}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <modules>
        <module>dubbo-demo-interface</module>
        <module>dubbo-demo-service</module>
        <module>dubbo-demo-web</module>
    </modules>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <charset>${project.build.sourceEncoding}</charset>
                    <server>tomcat7</server>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

    2、完成后,其dubbo-demo-interface的pom.xml文件内容为(该maven工程packaging属性jar):

复制代码
<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.moy.maven</groupId>
        <artifactId>dubbo-demo-master</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-demo-interface</artifactId>
    <name>dubbo-demo-interface Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    </dependencies>
    <build>
        <finalName>dubbo-demo-interface</finalName>
    </build>
</project>
复制代码

    3、完成后,添加dubbo-demo-interface依赖,其dubbo-demo-service的pom.xml文件内容为(该maven工程packaging属性为war):

复制代码
<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.moy.maven</groupId>
        <artifactId>dubbo-demo-master</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-demo-service</artifactId>
    <packaging>war</packaging>
    <name>dubbo-demo-service Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.moy.maven</groupId>
            <artifactId>dubbo-demo-interface</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <finalName>dubbo-demo-service</finalName>
    </build>
</project>
复制代码

    4、完成后,添加dubbo-demo-interface依赖,其dubbo-demo-web的pom.xml文件内容为(该maven工程packaging属性为war):

复制代码
<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.moy.maven</groupId>
        <artifactId>dubbo-demo-master</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-demo-web</artifactId>
    <packaging>war</packaging>
    <name>dubbo-demo-web Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.moy.maven</groupId>
            <artifactId>dubbo-demo-interface</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <finalName>dubbo-demo-web</finalName>
    </build>
</project>
复制代码

    5、打开cmd进入dubbo-demo-master的pom.xml同级目录,执行mvn clean install ,其他子工程同理

  b、编辑dubbo-demo-interface工程,新增服务接口UserService和实体User,删除必要文件(如:webapp文件),修改完成后在pom.xml目录下使用命令:mvn clean install

     

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  c、更新dubbo-demo-service的maven项目,编辑dubbo-demo-service工程,webapp只留下web.xml,删除不必要的jsp文件

  1、BaseMapper

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  2、UserMapper

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  3、UserServiceImpl

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  4、UserMapper.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  5、app.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/whymoy?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC 
jdbc.username=xxx
jdbc.password=xxx

  6、import.sql

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  7、log4j.properties

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  8、mybatis-config.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  9、spring-dao.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  10、spring-dubbo-provider.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  11、spring-tx.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  12、spring.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  13、UserMapperTest.java

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  14、web.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  15、测试,创建app.properties配置的数据库和导入import.sql,运行UserMapperTest单元测试,如果出错就看日志定位。

  d、更新dubbo-demo-web的maven项目,编辑dubbo-demo-web工程,webapp只留下web.xml,删除不必要的jsp文件

  

  1、UserController.java

复制代码
package com.moy.maven.controller;

import java.util.List;

import org.moy.maven.domain.User;
import org.moy.maven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    @ResponseBody
    public List<User> listUser() {
        List<User> userList = userService.findAll();
        return userList;
    }
}
复制代码

  2、log4j.properties

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  3、spring-dubbo-consumer.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  4、spring-mvc.xml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  5、web.xml

复制代码
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- *** 编码拦截器开始(拦截器加载有顺序,放在最前面) *** -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- === 编码拦截器结束 === -->
    
    <!-- *** 初始化SpringMVC开始 *** -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- === 初始化SpringMVC结束 === -->
</web-app>
复制代码

  6、index.html

复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    hello world!
</body>
</html>
复制代码

四、集成测试

   1、确保zookeeper是启动的

   2、打开cmd进入dubbo-demo-master的pom.xml同级目录下,运行命令:mvn clean install

   3、打开cmd进入dubbo-demo-service的pom.xml同级目录下,运行命令:mvn clean  tomcat:run -Dmaven.tomcat.port=8098

   4、打开cmd进入dubbo-demo-web的pom.xml同级目录下,运行命令:mvn clean  tomcat:run -Dmaven.tomcat.port=8099

   5、打开浏览器访问:http://localhost:8099/dubbo-demo-web/users/list.do接口,(接口数据是运行UserMapperTest单元测试来的)

   6、dubbo-demo-service工程实现接口,运行时向zookeeper注册接口信息,dubbo-demo-web工程只需要依赖接口,在通过spring原有的方式调用接口,其中dubbo帮我们从zookeeper中查找相应接口信息,通过默认dubbo协议帮我们完成了rpc调用。可以通过控制台http://localhost:8088/dubbo-admin-2.5.8/查看对应服务,对服务进行治理。

 

 

 

yexiangyang

moyyexy@gmail.com

posted @ 2020-03-31 21:34  changwen666  阅读(568)  评论(0编辑  收藏  举报