注:本文只是介绍我成功使用springboot dubbo 多模块项目的配置及核心代码,若问题没得到解决或需要可运行的源码,文章末尾有说明

springboot集成dubbo过程坑太多,dubbo提供者和消费者分别在单独的springboot程序中使用成功(各种坑),结果后面用springboot多模块开发,将dubbo提供者、消费者springboot程序分别作为一个模块集成近来,之前还可以的duboo消费者又出问题了,@Reference注解无效报空指针null问题!简直无语+崩溃。

又花了大半天网上找来找去加上自己摸索尝试终于解决问题,springboot多模块集成dubbo成功!赶紧做笔记记录下来。

springboot 集成 dubbo 提供者比较简单基本没什么问题,而消费者的@Reference注解就有一堆的坑,各种出幺蛾子。(当然,吐槽归吐槽,dubbo还是非常棒的,感谢阿里开发dubbo团队。)

关于dubbo版本依赖选择

网上见的比较多的2个版本dubbo依赖

        <!-- dubbo依赖 --><dependency><groupId>com.alibaba.spring.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.0.0</version></dependency>

     <!-- dubbo依赖 --><dependency><groupId>io.dubbo.springboot</groupId><artifactId>spring-boot-starter-dubbo</artifactId><version>1.0.0</version><!-- 避免依赖冲突,也就是避免阿里巴巴的dubbo中自带的spring依赖跟我们自己添加的依赖产生冲突 --><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion><exclusion><groupId>org.jboss.netty</groupId><artifactId>netty</artifactId></exclusion></exclusions></dependency>

不在springboot dubbo多模块项目中,上面2个版本的dubbo依赖我都成功使用过,但是在多模块项目中 spring-boot-starter-dubbo这个版本dubbo依赖死活不成功(当然是我没有找到正确的方法),后面改用 dubbo-spring-boot-starter 版本依赖在springboot dubbo多模块项目中成功启动运行.

下面介绍我在springboot dubbo多模块项目中集成dubbo的提供者、消费者 配置和核心代码

springboot集成dubbo的 提供者 和 消费者 的配置及部分核心代码

  • dubbo服务接口
package com.example.springbootapi.dubbo.service;/*** @author*/
public interface UserService {public String print(String s);
}
  • dubbo提供者

pom.xml (dubbo依赖、zookeeper依赖、zkclient依赖)

        <dependency><groupId>com.alibaba.spring.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.14</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion></exclusions></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version><scope>compile</scope></dependency>

springboot配置文件 application.properties

server.port=9991
#应用名
spring.dubbo.application.name=provider
#dubbo依赖版本为dubbo-spring-boot-starter时,dubbo提供者必须有这个server=true
spring.dubbo.server=true
#注册中心地址
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
#协议端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#扫描包,接口服务实现的包,根据个人接口服务类所在位置自己做修改
spring.dubbo.scan=com.example.springbootserver.service

dubbo实现接口服务类

package com.example.springbootserver.service;import com.alibaba.dubbo.config.annotation.Service;
import com.example.springbootapi.dubbo.service.UserService;
import org.springframework.stereotype.Component;@Service(interfaceClass = UserService.class)
@Component
public class UserServiceImpl implements UserService {@Overridepublic String print(String s) {return "say:" + s;}
}

springboot启动入口启动类

package com.example.springbootserver;import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableDubboConfiguration
@SpringBootApplication
public class SpringbootserverApplication {public static void main(String[] args) {SpringApplication.run(SpringbootserverApplication.class, args);}}
  • dubbo消费者

pom.xml (dubbo依赖、zookeeper依赖、zkclient依赖)

        <!-- dubbo依赖 --><dependency><groupId>com.alibaba.spring.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.14</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion></exclusions></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version><scope>compile</scope></dependency>

springboot配置文件 application.properties

server.port=9992
#dubbo消费者配置放在dubbo-consumer.xml中

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --><dubbo:application name="client"/><dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/><!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"--><dubbo:consumer check="false" /><!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired--><dubbo:reference id="UserService" check="false"  interface="com.example.springbootapi.dubbo.service.UserService"/><!-- 需要使用到提供者服务的包路径,多个包时用逗号隔开。注: 网上好多这里写错了 --><dubbo:annotation package="com.example.springbootclient.controller"/>
</beans>

dubbo远程服务获取

package com.example.springbootclient.controller;import com.example.springbootapi.dubbo.service.UserService;
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.ResponseBody;/*** @author*/
@Controller
public class UserController {//reference 在xml配置,这里需要用注解Autowired@Autowiredpublic UserService userService;@RequestMapping("/test")@ResponseBodypublic String test(){String s = userService.print("hhhhhhhhhhh");return s;}
}

springboot启动入口启动类

package com.example.springbootclient;import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;/*** 消费者: 先扫描dubbo 然后 扫描spring* @author*/
@EnableDubboConfiguration
@ImportResource(locations = {"classpath:dubbo-consumer.xml"})
@ComponentScan(basePackages = "com.example.springbootclient.controller")
@SpringBootApplication
public class SpringbootclientApplication {public static void main(String[] args) {SpringApplication.run(SpringbootclientApplication.class, args);}}

上面介绍的配置、代码 均来自 我的博文里的 springboot dubbo系列第三篇

最后

本文只是简单将 在springboot 集成dubbo 的提供者和消费者 的配置及部分核心代码展示,以及一些建议。如果能解决读者问题,那是最好了,如果还没有解决的话,可以看下我前面写的springboot 入门系列文章,都有详细开发过程及可以运行的源码。第一、二篇简单入门上手,第三篇介绍springboot dubbo多模块项目开发附源码,第四篇介绍springboot dubbo多模块项目开发有模块既是消费者又是提供者附源码。

若哪里说的不对,请评论指出谢谢!

springboot dubbo 多模块项目dubbo提供者和消费者配置及代码相关推荐

  1. SpringBoot+Maven 多模块项目的构建、运行、打包实战

    https://www.jb51.net/article/140772.htm?proxy=1 这篇文章主要介绍了SpringBoot+Maven 多模块项目的构建.运行.打包实战,小编觉得挺不错的, ...

  2. 解决springboot maven多模块项目打包的时候某个被依赖的模块报错找不到main class

    springboot maven 多模块项目打包的时候某个被依赖的模块报错 [ERROR] Failed to execute goal org.springframework.boot:spring ...

  3. Unable to start embedded container和Consider defining a bean:SpringBoot搭建多模块项目错误

    SpringBoot搭建多模块项目错误,我是参照这个网址搭建的 然后搭建过程中就遇到了一些bug:https://blog.csdn.net/hanchao5272/article/details/8 ...

  4. Spring Boot集成Dubbo多模块项目创建与配置

    目录 概述 使用工具 环境搭建 1.父模块创建 2.创建子模块 多模块项目配置 一. 父模块pom配置 1.继承设置 2.使用dependencyManagement管理依赖版本号 3.使用prope ...

  5. 使用IDEA开发springboot多module模块项目的配置文件的共享读取问题

    版权声明:欢迎转载,注明作者和出处就好!如果不喜欢或文章存在明显的谬误,请留言说明原因再踩哦,谢谢,我也可以知道原因,不断进步! https://blog.csdn.net/csonst1017/ar ...

  6. SpringBoot创建多模块项目和所遇到的问题

    一:创建SpringBoot多模块项目(这里采用idea工具进行创建) 0.先说下这个项目的组织:这个项目有个以下几个模块,分别是common[公共],entity[实体类],dao[数据访问],se ...

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

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

  8. springboot项目层次结构_【SpringBoot】多模块项目结构搭建

    前言: 必需学会SpringBoot基础知识 简介: Takes an opinionated view of building production-ready Spring application ...

  9. SpringBoot+Mybatis多模块(module)项目搭建教程

    作者:枫本非凡 cnblogs.com/orzlin/p/9717399.html 编辑:Java知音 一.前言 最近公司项目准备开始重构,框架选定为SpringBoot+Mybatis,本篇主要记录 ...

最新文章

  1. php中连接两个值,php - 如何从两个表的连接中选择一个值? - SO中文参考 - www.soinside.com...
  2. linux ubuntu文件系统,Ubuntu Linux文件系统的目录及用途简析
  3. Asigra无代理备份:“云”数据保护的先行者
  4. EZ 2018 03 23 NOIP2018 模拟赛(五)
  5. BZOJ3555: [Ctsc2014]企鹅QQ
  6. JQuery. Parse XML children recursively. How? - Stack Overflow
  7. Zend Studio 4.0.2试用手迹
  8. android LocalActivityManager说明
  9. JAVA常用算法手册 第3版 pdf
  10. Linux工具篇 | Linux下安装repo工具
  11. 自然语言处理基础技术之成分句法分析
  12. Arduino时钟显示
  13. android fragmentpageradapter切换不更新,android – FragmentPagerAdapter不会在方向更改时重新创建片段吗?...
  14. 今天的天气是多么的晴朗
  15. c语言 读取TXT 去空格,C语言读取TXT文件,忽略文件空格,把内容写入数组中应该如何实现...
  16. 最短路小结(三种算法+各种常见变种)
  17. 面向对象分析与设计的底层逻辑
  18. 【bzoj4084】[Sdoi2015]bigyration hash
  19. qq文件怎么传到百度云_如何将手机百度网盘中的文件发送给微信或qq好友 看完就明白了...
  20. 车辆运动控制(6)考虑侧倾约束

热门文章

  1. Java Type类
  2. mysql请升级_MySQL升级
  3. PHP json获取相关对象值
  4. 定时器与计数器的区别
  5. OpenGL中平移函数glTranslatef()、旋转函数glRotatef()的理解(非常好的文章)
  6. 从失败中笑看数仓:探索构建数仓失败的原因
  7. linux golang安装
  8. 【C++每日一练】13.最小的k个数
  9. access数据库剔除重复项_如何处理access中重复内容去除?
  10. SSM之一点一滴:mybatis parameterType传入类型 resultType返回类型