基于springboot构建dubbo的入门demo
之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo;众所周知,springboot极大的简化了我们的日常开发中的配置工作,所以使用springboot来构建dubbo也变的尤为简单;需求与上篇文章的一样,步骤总结为以下几点;
一、在application.properties编写dubbo的配置文件关键代码如下:
提供者:
# 当前服务的名称
dubbo.application.name=boot-user-service-provider # 注册中心的地址 这里注册中心用的是zookeeper
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper # 指定通信规则(通信协议:通信端口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20883 # 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心
dubbo.monitor.protocol=registry
消费者:
server.port=8888 # 当前服务的名称
dubbo.application.name=boot-order-service-consumer # 注册中心的地址 这里注册中心用的是zookeeper
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper # 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心
dubbo.monitor.protocol=registry
二、在springboot项目的启动类上添加@EnableDubbo注解表示开启基于注解的dubbo模型,如无其他特殊需求,提供者和消费一样,代码如下:
@SpringBootApplication
@EnableDubbo // 开启基于注解的dubbo模型
public class BootUserServiceProviderApplication { public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}
三、对于提供者里面的各个实现类我们需要添加@Service标签来暴露服务,对于消费者我们需要在引用提供者的时候添加@Reference来引用远程的提供者
与普通maven项目相比,基于springboot的dubbo项目配置文件的位置变化了也更倾向于注解而非xml来配置了,当然,对于习惯于xml配置的人来说,springboot也是可以实现基于xml的配置的
提供者需要暴露的服务:
package com.darling.boot.service; import com.alibaba.dubbo.config.annotation.Service;
import com.darling.pubIn.bean.User;
import com.darling.pubIn.service.UserService;
import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.List; /**
* @author 董琳琳
* @date 2018/10/8 11:20
* @description
*/
@Service // 暴露服务
@Component
public class UserServiceImpl implements UserService {
@Override
public List<User> getUserAddressList(String userId) {
ArrayList list = new ArrayList();
list.add(new User(3,"韦德3","男",36,"迈阿密"));
list.add(new User(23,"詹姆斯23","男",34,"洛杉矶"));
list.add(new User(24,"科比24","男",39,"洛杉矶"));
return list;
} @Override
public void sayHello() {
System.out.println("HELLO YSXLXSH");
} }
消费者调用服务:
package com.darling.boot.order.service; import com.alibaba.dubbo.config.annotation.Reference;
import com.darling.pubIn.bean.User;
import com.darling.pubIn.service.OrderService;
import com.darling.pubIn.service.UserService;
import org.springframework.stereotype.Service; import java.util.List; /**
* @author 董琳琳
* @date 2018/10/8 11:42
* @description
*/
@Service
public class OrderServiceimpl implements OrderService { @Reference // 引用远程服务
UserService service; @Override
public List<User> initOrder(String userId) {
return service.getUserAddressList(userId);
}
}
至此基于springboot搭建dubbo入门demo的关键代码已写完,需要注意的是这里我的测试不是像之前基于maven构建时的 单元测试,而是给消费者搭建成一个web项目通过从浏览器发送请求来测试是否调用成功,消费者的controller如下:
package com.darling.boot.order.controller; import com.darling.pubIn.bean.User;
import com.darling.pubIn.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; /**
* @author 董琳琳
* @date 2018/10/8 11:44
* @description 订单Controller
*/
@RestController
public class OrderController { @Autowired
OrderService service; @RequestMapping(value = "/getUserList/{userId}")
public List<User> getUserList(@PathVariable String userId){
return service.initOrder(userId); }
}
至此,基于springboot搭建dubbo的入门案例基本完成,关于dubbo案例的所有代码全部上传至GitHub,地址如下:
公共接口服务:https://github.com/darling2047/pub-interfence
基于springboot的提供者:https://github.com/darling2047/boot-user-service-provider
基于springboot的消费者:https://github.com/darling2047/boot-order-service-consumer
基于普通maven项目的提供者:https://github.com/darling2047/user-service-provider
基于普通maven项目的消费者:https://github.com/darling2047/order-service-consumer
以后有时间再记录下dubbo的各项常用配置
基于springboot构建dubbo的入门demo的更多相关文章
- 基于SpringBoot构建分模块项目
前言 步骤过于详细,多图慎入!!! 假设一个场景,要开发一个4s店维修部的办公系统,其功能有:前台接待,维修抢单,财务结算,库存管理.于是我们创建一个项目balabalabala写完交工. 一段时间后 ...
- springboot与dubbo整合入门(三种方式)
Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...
- [SpringBoot guides系列翻译]SpringBoot构建RESTful程序入门
原文地址 构建一个RESTful的WebService 这个指南将带你用Spring创建一个RESTful的helloworld程序. 你将完成 在下面地址上创建一个接收http get请求的服务 h ...
- 基于vue-cli构建vue-router的入门级demo
前言 本案列仅针对刚刚入门vue学习的伙伴,博主也是刚刚在学基于vue-cli搭建脚手架项目,对于前端大牛,可以移步. 快速搭建vue-cli环境 如何搭建基于vue-cli项目,这里不再叙述,如果不 ...
- 基于springboot的Dubbo的常规总结
1.引入jar包: <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>com.alibaba.spring.b ...
- 基于SpringBoot的Swagger2快速入门
1. Springboot 集成 Swagger2 1.1 导入Swagger2 依赖 <!-- https://mvnrepository.com/artifact/io.springfox/ ...
- Dubbo+Zookeeper 入门Demo
1.Zookeeper安装及启动 可参考这篇文章https://www.cnblogs.com/geekdc/p/5948326.html 从下载到启动都描述的很详细,按照文章一步一步走即可. 2.D ...
- dubbo快速入门demo
参考文章 https://blog.csdn.net/abcwanglinyong/article/details/81906027 该demo包含三个项目,分别是: 服务提供端项目:provider ...
- springboot整合dubbo+zookeeper最新详细
引入 最近和小伙伴做一个比赛,处于开发阶段,因为涉及的服务比较多,且服务需要分开部署在不同的服务器上,讨论之后,打算采用分布式来做,之前学习springboot的时候,部分章节涉及到了springbo ...
随机推荐
- 'telnet' 不是内部或外部命令,也不是可运行的程序
1.打开控制面板 2. 3. 4.这样就好可,重新打开cmd命令.
- [原]osg模型动画|骨骼动画
参考源码:osg的官方例子:osganimationviewer 首先制作一个带骨骼动画的模型 demo.FBX 这里面我们做了两个骨骼动画:1.open 2.close 下面开始在osg中使用 ...
- Rails6新增rails db:system:change更换数据库
rails db:system:change --to=postgresql rails db:system:change --to=mysql rails db:system:change --to ...
- mtcnn
1.widerface样本标签处理 图片名 x1 y1 x2 y2 x11 y11 x22 y22 多人脸框 # -*- coding: utf- -*- ""&qu ...
- XV Open Cup named after E.V. Pankratiev. GP of Central Europe (AMPPZ-2014)--B.Petrol
多源最短路+并查集 #include <bits/stdc++.h> using namespace std; #define rep(i, j, k) for (int i = int( ...
- C#异步(下)
上篇主要分析了async\await之前的一些异步模式,今天说异步的主要是指C#5的async\await异步.在此为了方便的表述,我们称async\await之前的异步为“旧异步”,async\aw ...
- VMware下Debian开发环境部署之常见问题记录
本文讲介绍windows作为宿主机,linux虚拟机作为编译环境的开发环境搭建中最常用到的三个问题,详细描述了解决过程. 目录: 1.网路配置: 2.分辨率设置: 3.共享网盘设置: 1.网络设置,V ...
- 从零开始学Python 一
一.安装 1.进入Python官网下载环境:https://www.python.org 2.根据自己的电脑选择安装版本,然后安装即可. 二.运行第一个程序 1.安装完Python,会自带一个编辑器, ...
- require 4种引入方式的区别
以下四种引入方式的区别: 自己创建的包里面封装了一些方法,只是把aa文件夹放在了node_modules文件夹里,所以在引用时,不需要写上相对路径,也不能在网上下载 这是网上别人封装好了的包,下载好了 ...
- 构建web应用之——文件上传
我们通过使用multipart请求数据接收和处理二进制信息(如文件).DispatcherServlet并没有实现任何解析multipart请求数据的功能,它将该任务委托给了Spring中的multi ...