Spring boot + Dubbo 的搭建

一、Dubbo的使用demo

1.服务构架

服务架构:一个api,一个服务提供者,一个服务消费者

dubbo-api:声明生产者服务接口。

dubbo-provider:提供服务,作为服务的生产者,注册到zookeeper上

dubbo-consumer:作为服务的消费者,注册到zookeeper上

image2019-4-12_9-32-37.png?version=1&modificationDate=1555032779826&api=v2

2.项目Maven依赖图

image2019-4-12_9-36-47.png?version=1&modificationDate=1555033030714&api=v2

dubbo-provider 依赖 dubbo-api 声明service 接口

dubbo-consumer 依赖 dubbo-api 声明service 接口通过dubbo调用provider服务

3.框架结构 Spring boot + dubbo +zookeeper

4.搭建demo

1.父级结构:

image2019-4-12_9-46-22.png?version=1&modificationDate=1555033605796&api=v2

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.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:

项目结构:

image2019-4-12_9-40-55.png?version=1&modificationDate=1555033278469&api=v2

service包:接口provider接口声明

pojo包:数据库实体对象

POM不做任何依赖

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

项目结构:

image2019-4-12_9-54-29.png?version=1&modificationDate=1555034092428&api=v2

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):

项目结构:

image2019-4-12_10-5-10.png?version=1&modificationDate=1555034733455&api=v2

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.运行及结果

1、启动zookeeper

2、启动服务生产者 ProviderApplication

3、启动服务消费者 ConsumerApplication

4、访问消费者Swagger地址

image2019-4-12_10-13-17.png?version=1&modificationDate=1555035220856&api=v2

并能查询到数据结果

二、Spring Clouds 和 Dubbo 项目的区别

  1. Spring Clouds 注册中心是 Spring Eureka作为注册中心,dubbo使用Zookeeper作为集群管理
  2. 协议区别,Dubbo采用的是基于TCP协议的RPC通讯,Spring Clouds 之间采用的是http协议进行通讯,RPC通讯协议要比http小,解析更快
  3. 协议区别
    RPC协议采用单一的长链接,
    HTTP协议采用多连接的短连接

转载于:https://my.oschina.net/u/2970507/blog/3035872

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值