目前的dubbo已支持和springboot集成,还是之前的例子,这次我们通过springboot容器来实现。借此了解一下基于springboot容器启动的dubbo的配置及使用。

1. 准备工作

创建一个Maven空项目,作为项目的父工程,此工程的子项目基于Spring Boot 2.0.5 实现

在父工程的pom.xml引入之后要创建的子工程

    <modules>
<module>gmall-interface</module>
<module>user-service-provider</module>
<module>order-service-consumer</module>
</modules>

可以提前看一下工程结构

下面分别来实现子工程:(子工程的实现方式都是在gmall工程下新建Module)

2. 公共API

项目中共用的接口和POJO类,代码和之前一样,这里不再展开

3. 服务提供者

工程结构如下

引入依赖

<!-- 引入公共API,以实现其接口 -->
<dependency>
<groupId>com.zang</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 引入spring-boot-starter以及dubbo和curator的依赖 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- Spring Boot相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

需要注意的是,根据jdk和Spring Boot版本的不同,dubbo-spring-boot-starter的版本需要有根据的选择

dubbo提供了@Service注解,可将类声明为提供方,省去了大量配置的麻烦

import com.alibaba.dubbo.config.annotation.Service;
import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.UserService;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.List; @Service //属于Dubbo的@Service注解,非Spring 作用:暴露服务
@Component
public class UserServiceImpl implements UserService { @Override
public List<UserAddress> getUserAddressList(String userId) {
//省略
}}

通过属性配置的方式设置application.properties

#当前服务/应用的名字
dubbo.application.name=user-service-provider
#注册中心的协议和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
#通信规则(通信协议和接口)
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#连接监控中心
dubbo.monitor.protocol=registry
#开启包扫描,可替代 @EnableDubbo 注解
##dubbo.scan.base-packages=com.zang.gmall

springboot容器根据配置启动服务提供方,这里需要添加 @EnableDubbo 注解

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; // 开启基于注解的dubbo功能(主要是包扫描@DubboComponentScan)
// 也可以在配置文件中使用dubbo.scan.base-package来替代 @EnableDubbo
@EnableDubbo @SpringBootApplication
public class UserServiceProviderApplication { public static void main(String[] args) {
SpringApplication.run(UserServiceProviderApplication.class, args);
}
}

提供方顺利启动

4. 服务消费者

消费者工程在初始化时设置为web项目,结构如下

引入和服务提供方同样的依赖,除此之外,添加springboot web模块的依赖。

    <!-- springboot web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

dubbo提供了@Reference注解,可替换@Autowired注解,用于引入远程服务

import com.alibaba.dubbo.config.annotation.Reference;
import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.OrderService;
import com.zang.gmall.service.UserService;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class OrderServiceImpl implements OrderService { @Reference
UserService userService; @Override
public List<UserAddress> initOrder(String userId) {
  //代码省略
}
}

配置文件application.properties

#避免和监控中心端口冲突,设为8081端口访问
server.port=8081 dubbo.application.name=order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry

启动类同样加上@EnableDubbo注解

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo
@SpringBootApplication
public class OrderServiceConsumerApplication { public static void main(String[] args) {
SpringApplication.run(OrderServiceConsumerApplication.class, args);
}
}

为查看调用是否成功,新增控制层用于访问

import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
public class OrderController { @Autowired
OrderService orderService; @ResponseBody //以json格式返回
@RequestMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("uid") String userId){ return orderService.initOrder(userId);
} }

5. 测试调用

启动消费方,在浏览器访问

调用成功

附:springboot也允许引用xml文件配置,方法是在启动类中加入如下注解

//@EnableDubbo
//引入配置信息
@ImportResource(locations="classpath:provider.xml")
@SpringBootApplication
public class UserServiceProviderApplication {
//略
}

Dubbo整合SpringBoot的更多相关文章

  1. Dubbo整合Springboot框架

    本文使用的是alibaba的Dubbo. Dubbo整合Springboot可以分为四步: 第一步:首先需要了解Dubbo官方给的建议,至少有三个工程: 接口工程:主要存实体bean和业务接口 服务提 ...

  2. dubbo入门学习(三)-----dubbo整合springboot

    springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...

  3. dubbo整合springboot最详细入门教程

    说明 目前互联网公司,大部分项目都是基于分布式,一个项目被拆分成几个小项目,这些小项目会分别部署在不同的计算机上面,这个叫做微服务.当一台计算机的程序需要调用另一台计算机代码的时候,就涉及远程调用.此 ...

  4. SpringBoot与Dubbo整合的三种方式

    1. 使用默认application.properties和注解的方式 导入dubbo-starter,在application.properties配置属性,使用@Service注解来暴露服务,使用 ...

  5. 手把手教你Dubbo与SpringBoot常用两种方式整合

    一.Dubbo整合SpringBoot的方式(1) 1)直奔主题,方式一: pom.xml中引入dubbo-starter依赖,在application.properties配置属性,使用@Servi ...

  6. SpringBoot与Dubbo整合下篇

    (1)pom.xml引入相关依赖jar包,如下: <dependency> <groupId>com.alibaba</groupId> <artifactI ...

  7. dubbo与springboot的三种整合方式

    SpringBoot与dubbo整合的三种方式:1.导入dubbo-starter,在application.properties配置属性,使用@Service暴露服务,使用@Reference引用服 ...

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

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

  9. Spring Boot和Dubbo整合

    provider端 POM依赖 <dependencies> <dependency> <groupId>org.springframework.boot</ ...

随机推荐

  1. Zindex和png

    Z轴在元素设置position为absolute或relative后被激活,起大小由z-index设置,z-index越大,元素位置越靠上.如果多个元素的z-index值相同,那么html标签中后出现 ...

  2. Reboot server stuck at “Press ESC in 1 seconds to skip startup.nsh”

    I have a Cisco C240 server, and everytime after reboot, it will got stuck at screen like below. To r ...

  3. uva 10712 - Count the Numbers(数位dp)

    题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...

  4. 解析oui.txt文件,通过MAC前缀获取Organization

    1.前言 OUI是指Organizationally unique identifier  (组织唯一标识符),签发给各类组织的唯一标识符.MAC地址共有6个字节48位组成,前3个字节体现了OUI,其 ...

  5. 红米除线刷的另外一种救砖方法fastboot

    原文来自:https://jingyan.baidu.com/article/48a42057e945bca9242504d7.html , 按照它操做了一下,虽然没有救活我的红米1,但是让我更好的了 ...

  6. [Spring Boot] Singleton and Prototype

    When we use Bean to do autowired, it actually use singleton, so even we create multi instanses, they ...

  7. 浮动闭合最佳方案:clearfix

    之前给大家介绍两种浮动闭合的办法CSS清除浮动 万能float闭合,得知很多同学都在使用下面的骨灰级解决办法: .clear{clear:both;height:0;overflow:hidden;} ...

  8. c数据库读写分离和负载均衡策略

    最近在学习数据库的读写分离和主从复制,采用的是一主多从策略,采用轮询的方式,读取从数据库的内容.但是,假如某一台从数据库宕机了,而客户端不知道,每次轮选到此从数据库,不都要报错?到网上查阅了资料,找到 ...

  9. Go语言中异常处理painc()和recover()的用法

    Go语言中异常处理painc()和recover()的用法 1.Painc用法是:用于抛出错误.Recover()用法是:将Recover()写在defer中,并且在可能发生panic的地方之前,先调 ...

  10. 通过实例看懂diff命令输出

    摘自:http://blog.sina.com.cn/s/blog_612144f30100nkpt.html ############################### 实例: 有这样两个文件: ...