一、Dubbo的使用demo
1.服务构架
服务架构:一个api,一个服务提供者,一个服务消费者
dubbo-api:声明生产者服务接口。
dubbo-provider:提供服务,作为服务的生产者,注册到zookeeper上
dubbo-consumer:作为服务的消费者,注册到zookeeper上
2.项目Maven依赖图
dubbo-provider 依赖 dubbo-api 声明service 接口
dubbo-consumer 依赖 dubbo-api 声明service 接口通过dubbo调用provider服务
3.框架结构 Spring boot + dubbo +zookeeper
4.搭建demo
1.父级结构:
POM文件如下:
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文件如下:
5.运行及结果
1、启动zookeeper
2、启动服务生产者 ProviderApplication
3、启动服务消费者 ConsumerApplication
4、访问消费者Swagger地址
并能查询到数据结果
二、Spring Clouds 和 Dubbo 项目的区别
- Spring Clouds 注册中心是 Spring Eureka作为注册中心,dubbo使用Zookeeper作为集群管理
- 协议区别,Dubbo采用的是基于TCP协议的RPC通讯,Spring Clouds 之间采用的是http协议进行通讯,RPC通讯协议要比http小,解析更快
- 协议区别
RPC协议采用单一的长链接,
HTTP协议采用多连接的短连接