基于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 ...
随机推荐
- Git Learning2 GitHub upload
1.在自己的github上创建一个仓库 2.git remote add [name] [link] 使用git来增加一个link的别名 3.git push [linkname] [分支名] 4.g ...
- C# WebClient实现文件上传
一.同步上传 文章 https://www.cnblogs.com/duanjt/p/6420172.html 里面有提到服务端通过WebApi如何实现文件上传,这里就只说客户端使用WebClient ...
- English trip EM2-MP4 Teacher:Taylor voiceless consonant 清辅音 & voiced consonant 浊辅音
课上内容(Lesson) # 区分 voiceless consonant 清辅音 & voiced consonant 浊辅音 清辅音 short # 轻快 浊辅音 long ...
- English Voice of <<Way Back Into Love>>
I have been living with a shadow overhead我一直生活在阴影中I have been sleeping with a cloud above my bed睡梦中床 ...
- 亚马逊VE账号运营
VE劲爆内幕大揭秘!“仿牌+Amazon VE”跟卖之路 Amazon Vendor Express 是Amazon.com2015年下旬推出的新的供应商平台,商家通过这个平台可以把产品卖给Amazo ...
- ajax提交不进入后台报415错误
Unsupported Media Type错误 问题所在为后台缺包和xml配置文档缺配置或配置不正确: Jackson的依赖问题,spring3.x和spring4.x是不同的: spring3.x ...
- springboot使用fastjson中文乱码解决方法 【转载】
以前使用fastjson替换jackson时,没有直接在页面打印过json,都是js使用没有出现乱码,偶然 打印出来出现了中文乱码 之前使用的配置方式 @Configuration public cl ...
- ReactJS之遍历对象的方法
const obj = { channel: “wevmvmklskdosll12k;0”, index:0 }; Object.keys(obj).map(key => console.log ...
- 读javascript高级程序设计-目录
javascript高级编程读书笔记系列,也是本砖头书.感觉js是一种很好上手的语言,不过本书细细读来发现了很多之前不了解的细节,受益良多.<br/>本笔记是为了方便日后查阅,仅作学习交流 ...
- rsyslog及loganalyzer
日志:历史日志 历史事件: 时间记录,事件的关键性程度,loglevel 系统日志服务: syslogd,守护进程 syslogd:system klogd:kernel rsyslog: sys ...