github上有一个开源项目spring-boot-starter-dubbo 提供了spring-boot与dubbo的集成功能,直接拿来用即可。(记得给作者点赞,以示感谢!)

下面是使用步骤,先看下工程的大致结构:

一、引入相关的依赖项

 subprojects {
buildscript {
ext {
springBootVersion = '1.5.3.RELEASE'
}
repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
} apply plugin: "java"
apply plugin: "maven"
apply plugin: 'idea' targetCompatibility = 1.8
sourceCompatibility = 1.8 repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
mavenCentral()
} configurations.all {
resolutionStrategy.cacheChangingModulesFor 1, "minutes"
} dependencies {
compile('io.dubbo.springboot:spring-boot-starter-dubbo:1.0.0')
compile('org.springframework.boot:spring-boot-starter-web:1.5.3.RELEASE')
}
}

这是最外层根目录下的build.gradle,关键地方就是最后dependencies引入的2个依赖项

二、service-api中定义接口

 package com.cnblogs.yjmyzz.service.api;

 /**
* Created by 菩提树下的杨过(http:/yjmyzz.cnblogs.com) on 2017/5/21.
*/
public interface DemoService {
String hello(String nickName);
}

这一步平淡无奇,没什么好说的

三、service-provider

3.1 提供接口实现

 package com.cnblogs.yjmyzz.service.impl;

 import com.alibaba.dubbo.config.annotation.Service;
import com.cnblogs.yjmyzz.service.api.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* Created by 菩提树下的杨过(http:/yjmyzz.cnblogs.com) on 2017/5/21.
*/
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService { Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class); public String hello(String nickName) {
logger.info(nickName + " call me!");
return String.format("hi , %s!", nickName);
}
}

常规套路,不用多说

3.2 编写ServiceProvider主类

 package com.cnblogs.yjmyzz.service;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* Created by yangjunming on 2017/5/21.
*/
@SpringBootApplication
public class ServiceProvider {
public static void main(String[] args) {
SpringApplication.run(ServiceProvider.class, args);
}
}

仍然是spring-boot的经典套路,跟dubbo也没任何关系

3.3 application.yml配置

 server:
port: 8001 spring:
dubbo:
scan: com.cnblogs.yjmyzz.service
application:
name: provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20880

这里是重点,指定了dubbo服务提供方启动所需的zk注册地址,协议类型及端口,包括扫描的包。

四、service-consumer

4.1 定义一个辅助用的Proxy

 package com.cnblogs.yjmyzz.service.proxy;

 import com.alibaba.dubbo.config.annotation.Reference;
import com.cnblogs.yjmyzz.service.api.DemoService;
import org.springframework.stereotype.Component; /**
* Created by yangjunming on 2017/5/21.
*/
@Component
public class ServiceProxy { @Reference(version = "1.0.0")
public DemoService demoService;
}

就是一个标准的spring组件(不管是叫proxy还是叫container都无所谓,随个人喜好),在该组件中持有对Service的引用实例,注意:如果指定了version,则该版本号要与service-provider中的版本号一致

4.2 调用服务

 package com.cnblogs.yjmyzz.service;

 import com.cnblogs.yjmyzz.service.proxy.ServiceProxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; /**
* Created by 菩提树下的杨过(http:/yjmyzz.cnblogs.com) on 2017/5/21.
*/
@SpringBootApplication
public class ServiceConsumer { public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(ServiceConsumer.class, args);
ServiceProxy proxy = ctx.getBean(ServiceProxy.class);
System.out.println(proxy.demoService.hello("菩提树下的杨过"));//调用服务
}
}

一看即明,不多解释。

4.3 application.yml配置

 server:
port: 8002 spring:
dubbo:
scan: com.cnblogs.yjmyzz.service
application:
name: consumer
registry:
address: zookeeper://127.0.0.1:2181

ok,搞定!

上述示例源代码,已托管至github,有需要的朋友自行下载:https://github.com/yjmyzz/spring-boot-dubbo-demo

spring-boot 速成(7) 集成dubbo的更多相关文章

  1. Java框架Spring Boot & 服务治理框架Dubbo & 应用容器引擎Docker 实现微服务发布

    微服务系统架构实践 开发语言Java 8 框架使用Spring boot 服务治理框架Dubbo 容器部署Docker 持续集成Gitlab CI 持续部署Piplin 注册中心Zookeeper 服 ...

  2. spring boot / cloud (三) 集成springfox-swagger2构建在线API文档

    spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...

  3. Spring Boot HikariCP 一 ——集成多数据源

    其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...

  4. Spring Boot系列——如何集成Log4j2

    上篇<Spring Boot系列--日志配置>介绍了Spring Boot如何进行日志配置,日志系统用的是Spring Boot默认的LogBack. 事实上,除了使用默认的LogBack ...

  5. 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作

    spring boot 2.X集成ES 进行CRUD操作  完整版 内容包括: ============================================================ ...

  6. 15、Spring Boot 2.x 集成 Swagger UI

    1.15.Spring Boot 2.x 集成 Swagger UI 完整源码: Spring-Boot-Demos 1.15.1 pom文件添加swagger包 <swagger2.versi ...

  7. 14、Spring Boot 2.x 集成 Druid 数据源

    14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos

  8. 12、Spring Boot 2.x 集成 MongoDB

    1.12 Spring Boot 2.x 集成 MongoDB 完整源码: Spring-Boot-Demos

  9. 11、Spring Boot 2.x 集成 HBase

    1.11 Spring Boot 2.x 集成 HBase 完整源码: Spring-Boot-Demos

  10. 10、Spring Boot 2.x 集成 Log4j2

    1.10 Spring Boot 2.x 集成 Log4j2 完整源码: Spring-Boot-Demos

随机推荐

  1. 20145234黄斐《Java程序设计》第八周

    教材学习内容总结 第十四章-NIO与NIO2 NIO与IO的区别 NIO Channel继承框架 想要取得Channel的操作对象,可以使用Channels类,它定义了静态方法newChannel() ...

  2. Kali社会工程学攻击--powershell 攻击(无视防火墙)

    1.打开setoolkit 输入我们反弹shell的地址与端口 2.修改我的shellcode 3.攻击成功

  3. 2017/05/21 java 基础 随笔

    工具类:所有的方法都是静态的,如果一个类中所有的方法都是静态的,需要再多做一步,私有构造方法,不让其他类创建本类对象. 生成文档: java.lang 包不用导入 常见代码块的应用    * a:局部 ...

  4. springMVC初次搭建,产生错误

    七月 11, 2016 11:12:58 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server version: Ap ...

  5. 关于NotificationListenerService监听时有失败的处理

    关于NotificationListenerService监听时有失败的处理 问题由来 去年进入一家专业做智能穿戴设备的公司,在项目中需要监听系统通知栏变化(主要是IM类app的信息获取到后推送到用户 ...

  6. Selenium2+python自动化62-jenkins持续集成环境搭建【转载】

    前言 selenium脚本写完之后,一般是集成到jenkins环境了,方便一键执行. 一.环境准备 小编环境: 1.win10 64位 2.JDK 1.8.0_66 3.tomcat 9.0.0.M4 ...

  7. Nginx安装及常用命令

    一.选定源码目录 cd /usr/local/src 可以是任何目录,本文选定的是/usr/local/src 二.安装依赖库 yum install gcc yum install pcre-dev ...

  8. 4.类型设计规范《.NET设计规范》

    类是引用类型的一般情况,占了框架中的大多情况,类的流行归于它支持面向对象的特征,以及它的普遍的适用性,基类和抽象类是两个特殊的逻辑分组,它们与扩张性有关. 由于CLR不支持多继承,接口类型可以用来模拟 ...

  9. LINUX的STRACE命令用法 [转]

    调用:strace [ -dffhiqrtttTvxx ] [ -acolumn ] [ -eexpr ] ...[ -ofile ] [ -ppid ] ... [ -sstrsize ] [ -u ...

  10. xgboost 实践

    xgboost 安装:xgboost:Scalable and Flexible Gradient Boosting github:  eXtreme Gradient Boosting 中文教程:可 ...