玩转Spring Boot 集成Dubbo

使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面引用事务,注入其他对象的时候,会有一些问题。于是我就果断放弃了注解了,使用的是XML,这里可能介绍的是Dubbo,但是如果使用Dubbox的话,基本上是兼容的。接下来,将说说使用XML的方式与Spring Boot在一起开发。
1.创建工程在pom.xml中加入依赖

创建工程名为: (1)springboot-dubbo-provide (2)springboot-dubbo-api (3)springboot-dubbo-consume springboot-dubbo-api工程主要是放一些service接口,用于提供给消费者使用 。springboot-dubbo-provide工程用于提供服务。 springboot-dubbo-consume工程为消费者。在springboot-dubbo-provide工程中打开pom.xml加入以下依赖,完整代码如下:
[html] view plain copy

4.0.0
com.chengli
springboot-dubbo-provide
0.0.1-SNAPSHOT
jar
springboot-dubbo-provide
http://maven.apache.org

org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE

UTF-8
1.8
2.5.3
3.4.6
0.1

com.chengli
springboot-dubbo-api
0.0.1-SNAPSHOT

org.springframework.boot
spring-boot-starter

com.alibaba
dubbo

org.springframework
spring

${com.alibaba.dubbo.version}

org.apache.zookeeper
zookeeper
${org.apache.zookeeper.version}

com.github.sgroschupf
zkclient
${com.github.sgroschupf.zkclient.version}

org.springframework.boot
spring-boot-maven-plugin

打开springboot-dubbo-consume工程,在pom.xml中加入以下依赖,完整代码如下:
[html] view plain copy

4.0.0
com.chengli
springboot-dubbo-consume
0.0.1-SNAPSHOT
jar
springboot-dubbo-consume
http://maven.apache.org

org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE

UTF-8
1.8
2.5.3
3.4.6
0.1

com.chengli
springboot-dubbo-api
0.0.1-SNAPSHOT

org.springframework.boot
spring-boot-starter-web

com.alibaba
dubbo

org.springframework
spring

${com.alibaba.dubbo.version}

org.apache.zookeeper
zookeeper
${org.apache.zookeeper.version}

com.github.sgroschupf
zkclient
${com.github.sgroschupf.zkclient.version}

org.springframework.boot
spring-boot-configuration-processor
true

org.springframework.boot
spring-boot-maven-plugin

2.Dubbo配置

2.1springboot-dubbo-provide服务提供者

(1)在springboot-dubbo-provide项目中创建入口启动类MainConfig,完整代码如下:
[java] view plain copy
package com.chengli.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainConfig {
public static void main(String[] args) {
SpringApplication.run(MainConfig.class, args);
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}

(2)创建Dubbo配置类
[java] view plain copy
package com.chengli.springboot.dubbo;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo/dubbo.properties")
@ImportResource({ "classpath:dubbo/*.xml" })
public class DubboConfig {

}

(3)创建Dubbo配置文件 在src/main/resources下新建文件夹dubbo,并加入以下配置: dubbo-provider.xml内容如下:
[html] view plain copy


注意:这里我发布的example服务是示例,具体的根据实际修改

(4)创建dubbo.properties
[html] view plain copy
#应用名称
dubbo.application.name=example-provider
#注册中心类型
dubbo.registry.protocol=zookeeper
#注册中心地址
dubbo.registry.address=127.0.0.1:2181
#暴露服务方式
dubbo.protocol.name=dubbo
#暴露服务端口
dubbo.protocol.port=20880

2.2springboot-dubbo-consume服务消费者

(1)创建入口启动类MainConfig
[java] view plain copy
package com.chengli.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainConfig {
public static void main(String[] args) {
SpringApplication.run(MainConfig.class, args);
}
}

(2)创建Dubbo配置类
[java] view plain copy
package com.chengli.springboot.dubbo;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo/dubbo.properties")
@ImportResource({ "classpath:dubbo/*.xml" })
public class DubboConfig {

}

(3)创建Dubbo配置文件 在src/main/resources下新建文件夹dubbo,并加入以下配置: dubbo-consume.xml内容如下:
[html] view plain copy

(4)创建dubbo.properties
[html] view plain copy
#应用名称
dubbo.application.name=example-consume
#注册中心类型
dubbo.registry.protocol=zookeeper
#注册中心地址
dubbo.registry.address=127.0.0.1:2181

到这里基本上就已经可以了,不过测试类的代码我就不贴上来了。只要在API中定义接口实现即可。使用Spring Boot 与Dubbo集成的时候,需要注意的是,不要使用Spring Boot提供的devtools热启动,因为devtools提供了两个ClassLoader,加载策略问题导致出现错误,无法启动。如果开发中需要热加载,那么使用Spring 提供的springloaded。
有兴趣的朋友可以加群探讨相互学习:

玩转Spring Boot 集成Dubbo的更多相关文章

  1. Spring boot 集成 Dubbo 快速搭建

    架构: 1.ZooKeeper:服务注册中心 2.api工程:提供对外暴露的服务API 3.provider:服务提供者 4.consumer:服务消费者 示例如下: (一)新建 Maven 项目 a ...

  2. spring boot集成dubbo

    spring-boot-start-dubbo spring-boot-start-dubbo,让你可以使用spring-boot的方式开发dubbo程序.使dubbo开发变得如此简单. 如何使用 1 ...

  3. Spring boot 集成Dubbo简单版,准备工作,

    一.GitHub上找寻Dubbo资源 阿里巴巴在其GitHub上已经写好一个Github案例所以我们只要进入其Git上就可以看到和clone这个项目 二.阿里巴巴GitHub使用 https://gi ...

  4. 一个spring boot集成dubbo的小例子

    请移步github,介绍和代码均在上面了:https://github.com/wuxun1997/voicebox 这里再多说两句.github上的这个小例子默认使用组播作为注册中心,你也可以把组播 ...

  5. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  6. 玩转spring boot——结合阿里云持续交付

    前言 在互联网项目中,项目测试.部署往往需要花费大量时间.传统方式是在本地打包.测试完毕程序,然后通过ftp上传至服务器,再把测试的配置文件修改为生产环境的配置文件,最后重新运行服务.这一过程如果交给 ...

  7. spring boot 集成 zookeeper 搭建微服务架构

    PRC原理 RPC 远程过程调用(Remote Procedure Call) 一般用来实现部署在不同机器上的系统之间的方法调用,使得程序能够像访问本地系统资源一样,通过网络传输去访问远程系统资源,R ...

  8. 玩转spring boot——war部署

    前言 之前部署spring boot应用是通过直接输入命令“java -jar”来实现的.而有些情况,由于部署环境的制约,只能把项目从jar转换成war才能部署,如新浪云sae的java环境容器.那怎 ...

  9. 玩转spring boot——结合docker

    前言 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 liunx机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有 ...

随机推荐

  1. POJ3278(KB1-C 简单搜索)

    Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and wants ...

  2. 小tip: CSS后代选择器可能的错误认识——张鑫旭

    一.关于类选择器的一个问题 假设有下面一个面试题,CSS代码如下: .red { color: red; } .green { color: green; } HTML如下: <div clas ...

  3. react解决roadhog buildDll 【转】

    本地删了 node module 目录,重新安装的时候,提示 找了找,可如下解决 ------- 转自: https://www.cnblogs.com/huhanhaha/p/7605722.htm ...

  4. 让input光标一直在最右边

    有时候,我们需要使的input输入框的在点击时光标一直居右边 例如:移动端,用手指去点击输入框,在输入框较小,手指又比较大,那么经常会在点击后,光标会在已有文字时,居左 我们的输入框文字肯定要居中的需 ...

  5. BitmapFactory.Options

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); bmpFactoryOptions.inSampleSiz ...

  6. Android上实现视频录制

    首先,我们肯定要用到摄像头,因此需要在Manifest文件中声明使用权限: <uses-permission android:name="android.permission.CAME ...

  7. 报表在vista和win7下无法浏览应用的解决办法

     对于vista和win7系统,报表工具有着良好的兼容性,无论是设计器还是实际应用.有些客户在安装报表设计报表的时候没有遇到问题,但是在这两种系统下会发现无法启动应用,或者打开设计器自带的ie浏览 ...

  8. mybatis大于小于的转义

    最近在使用mybatis,然后用到了小于等于,直接在XML中使用了<=,结果XML文件一直显示红色错误,如下: sum(case when p.pool_year <= '2014' th ...

  9. springboot学习入门之五---开发Web应用之JSP篇

    转载:http://tengj.top/2017/03/13/springboot5/ 1整体结构 整体的框架结构,跟前面介绍Thymeleaf的时候差不多,只是多了webapp这个用来存放jsp的目 ...

  10. 4.Spring——xml配置文件

    如果使用Maven构建项目,spring在加载xsd文件时总是先试图在本地查找xsd文件(spring的jar包中已经包含了所有版本的xsd文件), 如果没有找到,才会转向去URL指定的路径下载.ap ...