之前记录了构建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的更多相关文章

  1. 基于SpringBoot构建分模块项目

    前言 步骤过于详细,多图慎入!!! 假设一个场景,要开发一个4s店维修部的办公系统,其功能有:前台接待,维修抢单,财务结算,库存管理.于是我们创建一个项目balabalabala写完交工. 一段时间后 ...

  2. springboot与dubbo整合入门(三种方式)

    Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...

  3. [SpringBoot guides系列翻译]SpringBoot构建RESTful程序入门

    原文地址 构建一个RESTful的WebService 这个指南将带你用Spring创建一个RESTful的helloworld程序. 你将完成 在下面地址上创建一个接收http get请求的服务 h ...

  4. 基于vue-cli构建vue-router的入门级demo

    前言 本案列仅针对刚刚入门vue学习的伙伴,博主也是刚刚在学基于vue-cli搭建脚手架项目,对于前端大牛,可以移步. 快速搭建vue-cli环境 如何搭建基于vue-cli项目,这里不再叙述,如果不 ...

  5. 基于springboot的Dubbo的常规总结

    1.引入jar包: <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>com.alibaba.spring.b ...

  6. 基于SpringBoot的Swagger2快速入门

    1. Springboot 集成 Swagger2 1.1 导入Swagger2 依赖 <!-- https://mvnrepository.com/artifact/io.springfox/ ...

  7. Dubbo+Zookeeper 入门Demo

    1.Zookeeper安装及启动 可参考这篇文章https://www.cnblogs.com/geekdc/p/5948326.html 从下载到启动都描述的很详细,按照文章一步一步走即可. 2.D ...

  8. dubbo快速入门demo

    参考文章 https://blog.csdn.net/abcwanglinyong/article/details/81906027 该demo包含三个项目,分别是: 服务提供端项目:provider ...

  9. springboot整合dubbo+zookeeper最新详细

    引入 最近和小伙伴做一个比赛,处于开发阶段,因为涉及的服务比较多,且服务需要分开部署在不同的服务器上,讨论之后,打算采用分布式来做,之前学习springboot的时候,部分章节涉及到了springbo ...

随机推荐

  1. 解决来自美国IP的攻击过程

    1.因为最近接口文档confluence服务总是自动关闭.   解决方法: 1.查看阿里云上的报警提示,看到来自外国的Ip的攻击.这时我选择把攻击的IP加入黑名单. 加入黑名单的方法:https:// ...

  2. Js拾忆

    instanceof运算符:他是判断一个构造函数的prototype是否在对象的原型链上查找到 var a = new Array(); console.log(a instanceof Array) ...

  3. CentOS7服务管理

    1.在/usr/lib/systemd/system目录下建立服务启动文件,文件格式:[root@Centos7 ]# cat /usr/lib/systemd/system/nginx.servic ...

  4. 安装和启动json-server

    安装json-server JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源 npm i -g json-server ...

  5. vue.js+webpack在一个简单实例中的使用过程demo

    这里主要记录vue.js+webpack在一个简单实例中的使用过程 说明:本次搭建基于Win 7平台 Node.js 安装官网提供了支持多种平台的的LTS版本下载,我们根据需要来进行下载安装.对于Wi ...

  6. 跟踪mqttv3源码(二)

    对于spring-mqtt.xml中的标签: <int-mqtt:message-driven-channel-adapter> <int-mqtt:outbound-channel ...

  7. 一个空格引起的错误。 python

    'render_field' tag requires a form field followed by a list of attributes and values in the form att ...

  8. restore not found的错误(问题2)

    最近在写gan,那么就牵扯到在一个session中加载两个图,restore的时候会有问题.如这篇文章写的(http://blog.csdn.net/u014659656/article/detail ...

  9. There are multiple modules with names that only differ in casing. 黄色warning

    There are multiple modules with names that only differ in casing.有多个模块同名仅大小写不同This can lead to unexp ...

  10. js设置radio单选框值选中

    html页面: <div> <label><input type="radio" name="sex" value="m ...