使用springBoot搭建dubbo的环境

写在前面: 所有代码见 https://github.com/123liudong/springBoot-dubbo 
如果您有收获,不要吝啬您的小星星哦!

学习记录

在学习过程中搭建一个脚手架可能会耗去很多的时间,虽然有官方的demo,但自己面临的情况总会与官网有出入,所以记录下自己在springBoot环境下搭建dubbo环境,实现远程调用的过程。

搭建环境

  1. jdk1.8
  2. springBoot v2.0.4.RELEASE
  3. dubbo 2.5.10
  4. zookeeper 3.4.13
  5. idea

搭建概述

  1. 创建一个项目(使用dubbo的项目)。
  2. 使用dubbo框架需要使用到maven多模块的知识,如果不熟悉,请到网上自行学习。
  3. 项目架构为一个父模块,两个serviceProvider模块,一个consumer,一个API提供端。

详细搭建过程

  1. 创建一个项目,创建过后的目录如下

idea创建项目后的初始文件
2. 删除src目录。
3. 创建一个公共的API模块,什么都不用勾选,因为这个模块只是用于声明接口(file -> new -> module -> 创建一个springBoot项目)。

公共接口的模块
4. 类似的,创建其余的两个provider模块和consumer模块。

在 consumer 这个模块中勾选 web ,因为我们使用http来测试是否成功调用服务。

勾选的模块如下

其余的两个 provider 模块就什么都不用勾选了,这里只是一个demo,如果需要其他的功能,自行斟酌。

整个项目的文件架构(每个模块都是springBoot项目)如下:
项目文件架构

项目创建完成,现在需要更改下每个模块pom.xml的配置

springboot_dubbo_project模块

  1. 声明所有的子模块。

添加子模块
2. 最后打包的形式不是 jar,也不是 war,需要改成 pom。因为这个模块作为所有模块的父模块。
3. 使用 dependencyManagement 管理 dubbo 和 zkclient的依赖

dubbo_api模块

  1. 声明父模块为 springboot_dubbo_project

dubbo_consumer模块

  1. 声明父模块为 springboot_dubbo_project
  2. 把 dubbo_api 作为依赖导进来。


3. 添加 dubbo 和zkclient的依赖,与父模块相对应,不用再写版本号

dubbo_provider模块

  1. 声明父模块为 springboot_dubbo_project
  2. 把 dubbo_api 作为依赖导进来。
  3. 添加 dubbo 和zkclient的以来,与父模块相对应,不用在写版本号

目前,大体的环境已经搭建好了,现在开始写demo

  1. 在api模块上添加接口。
  2. 两个provider模块实现接口,并使用一个xml文件配置关于dubbo的信息(有其他的方法,我喜欢这种就用这个了)。
  3. consumer调用两个服务,也需要一个xml文件配置关于dubbo的信息。
  4. 首先启动两个服务,然后启动consumer,最后测试。

对于各个模块,有如下结构

api模块

这个模块定义了两个接口,仅此而已!

consumer模块

这个模块是一个web项目(我这里是这样定义的),调用了两个服务。
其中关于dubbo的配置存放在 config/spring-dubbo.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="user-consumer"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://123.207.5.56:2181" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:reference interface="com.demo.dubbo_api.service.UserService" id="userService"/>
    <dubbo:reference interface="com.demo.dubbo_api.service.BookService" id="bookService"/>
</beans>

provider模块

这个模块,实现了api模块的某个或者多个接口。
关于dubbo的配置文件存放在 config/spring-dubbo.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="userService"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://123.207.5.56:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20881" />

    <!-- 声明需要暴露的服务接口,这里的bookService是bean的名称,我使用的是service注解(注意是 *spring* 的注解) -->
    <dubbo:service interface="com.demo.dubbo_api.service.UserService" ref="userService" />
</beans>

最后结果效果图

猜你喜欢

转载自blog.csdn.net/qq_38727626/article/details/81357296