Spring boot + Dubbo 的搭建

1、Dubbo的使用demo

1.服務構架

服務架構:一個api,一個服務提供者,一個服務消費者java

dubbo-api:聲明生產者服務接口。mysql

dubbo-provider:提供服務,做爲服務的生產者,註冊到zookeeper上web

dubbo-consumer:做爲服務的消費者,註冊到zookeeper上spring

2.項目Maven依賴圖

dubbo-provider 依賴 dubbo-api 聲明service 接口sql

dubbo-consumer 依賴 dubbo-api 聲明service 接口經過dubbo調用provider服務數據庫

3.框架結構 Spring boot + dubbo +zookeeper

4.搭建demo

1.父級結構:apache

POM文件以下:api

<?xml version="1.0" encoding="UTF-8"?>mybatis

<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.zx.dubbo</groupId>

  <artifactId>dubbo-root</artifactId>

  <packaging>pom</packaging>

  <version>1.0-SNAPSHOT</version>

 

  <modules>

    <module>dubbo-consumer</module>

    <module>dubbo-api</module>

    <module>dubbo-zx-provider</module>

  </modules>

 

  <properties>

    <dubbo-spring-boot>1.0.0</dubbo-spring-boot>

      <java.version>1.8</java.version>

  </properties>

 

    <!-- Spring Boot 啓動父依賴 -->

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.5.RELEASE</version>

    </parent>

 

  <dependencies>

 

      <!-- 引入spring-boot-starter以及dubbo和curator的依賴 -->

      <dependency>

          <groupId>com.alibaba.boot</groupId>

          <artifactId>dubbo-spring-boot-starter</artifactId>

          <version>0.2.0</version>

      </dependency>

 

        <!-- Spring Boot Web 依賴 -->

        <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

 

        <!-- Spring Boot Test 依賴 -->

        <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-test</artifactId>

          <scope>test</scope>

        </dependency>

 

        <!-- Junit -->

        <dependency>

          <groupId>junit</groupId>

          <artifactId>junit</artifactId>

          <version>4.12</version>

        </dependency>

  </dependencies>

 

  <build>

    <plugins>

      <plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

      </plugin>

    </plugins>

  </build>

 

</project>

 

2.dubbo-api:

項目結構:

service包:接口provider接口聲明

pojo包:數據庫實體對象

POM不作任何依賴

3.dubbo-provider(服務端口8080):

項目結構:

Controller包:項目啓動文件ProviderApplication和mybatis配置類

Mapper包:mybatis生成Dao文件

Service包:api接口實現層

resources: 配置文件所在目錄

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">

    <parent>

        <artifactId>dubbo-root</artifactId>

        <groupId>com.zx.dubbo</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <artifactId>dubbo-zx-provider</artifactId>

 

    <dependencies>

        <!-- 添加spring boot 和mybatis依賴 -->

        <dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>1.3.1</version>

        </dependency>

 

        <!-- 添加MySQL依賴 -->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

        </dependency>

        <!-- 添加mysql-generator -->

        <dependency>

            <groupId>org.mybatis.generator</groupId>

            <artifactId>mybatis-generator-core</artifactId>

            <version>1.3.5</version>

        </dependency>

 

        <dependency>

            <groupId>com.zx.dubbo</groupId>

            <artifactId>dubbo-api</artifactId>

            <version>1.0-SNAPSHOT</version>

        </dependency>

    </dependencies>

 

    <build>

        <plugins>

            <!-- mybatis generator 自動生成代碼插件 -->

            <plugin>

                <groupId>org.mybatis.generator</groupId>

                <artifactId>mybatis-generator-maven-plugin</artifactId>

                <version>1.3.5</version>

                <configuration>

                    <configurationFile>${basedir}/src/main/resources/mybatis-generator.xml</configurationFile>

                    <overwrite>true</overwrite>

                    <verbose>true</verbose>

                </configuration>

            </plugin>

        </plugins>

 

        <resources>

            <resource>

                <directory>${basedir}/src/main/resources</directory>

                <includes>

                    <include>**/*.*</include>

                </includes>

            </resource>

        </resources>

    </build>

</project>

4.dubbo-consumer(端口8081):

項目結構:

Controller包:項目啓動文件ConsumerApplication和測試Controller層 以及swagger2的配置類

demo包:向上給Controller提供服務service類所在包

resources: 配置文件所在目錄

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">

    <parent>

        <artifactId>dubbo-root</artifactId>

        <groupId>com.zx.dubbo</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

    <artifactId>dubbo-consumer</artifactId>

 

    <dependencies>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

            <version>2.8.0</version>

        </dependency>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

            <version>2.8.0</version>

        </dependency>

 

        <dependency>

            <groupId>com.zx.dubbo</groupId>

            <artifactId>dubbo-api</artifactId>

            <version>1.0-SNAPSHOT</version>

        </dependency>

    </dependencies>

</project>

5.運行及結果

一、啓動zookeeper

二、啓動服務生產者 ProviderApplication

三、啓動服務消費者 ConsumerApplication

四、訪問消費者Swagger地址

並能查詢到數據結果

2、Spring Clouds 和 Dubbo 項目的區別

  1. Spring Clouds 註冊中心是 Spring Eureka做爲註冊中心,dubbo使用Zookeeper做爲集羣管理
  2. 協議區別,Dubbo採用的是基於TCP協議的RPC通信,Spring Clouds 之間採用的是http協議進行通信,RPC通信協議要比http小,解析更快
  3. 協議區別 RPC協議採用單一的長連接, HTTP協議採用多鏈接的短鏈接
相關文章
相關標籤/搜索