博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dubbo-spring-boot-starter
阅读量:7093 次
发布时间:2019-06-28

本文共 3096 字,大约阅读时间需要 10 分钟。

  hot3.png

dubbo-spring-boot-starter的maven项目托管在;同时也可以在上了解它的简单介绍。

dubbo-spring-boot-starter是dubbo的spring boot starter,它可以无缝地对接spring boot和dubbo,方便大家使用dubbo组件。

需要注意的是dubbo-spring-boot-starter支持的jdk版本为1.6或者1.6+。

如何发布dubbo服务

  • 添加spring-boot-starter-dubbo依赖:
  • com.alibaba.spring.boot
    dubbo-spring-boot-starter
    1.0.0

    在application.properties内添加dubbo的相关配置信息,demo配置如下:

  • # dubbo配置spring.dubbo.appname=provider-testspring.dubbo.protocol=dubbo# 此处也可使用其它协议,比如zookeeper://xxxx:xxspring.dubbo.registry=multicast://224.0.0.0:1111spring.dubbo.port=20800spring.dubbo.group=testspring.dubbo.version=1.0.0

    接下来在Spring Boot Application上添加@EnableDubboConfiguration,表示要开启dubbo功能。

  • /** * Provider启动类 
    * * 如果没有web容器,需要hold住服务,否则进程会退出,参考以下代码: * *
     * synchronized (DubboProviderLauncher.class) { *   while (true) { *     try { *       DubboProviderLauncher.class.wait(); *     } catch (InterruptedException e) { *       // swallow it *     } *   } * } * 
    * * @author xionghui * @since 1.0.0 */@SpringBootApplication@EnableDubboConfigurationpublic class DubboProviderLauncher { public static void main(String[] args) { SpringApplication.run(DubboProviderLauncher.class, args); }}

    编写hello服务,只需要在发布的服务实现上添加注解 ,其中interfaceClass是要发布服务的接口。

  • import com.alibaba.dubbo.config.annotation.Service;@Component@Service(interfaceClass = IHelloService.class)public class HelloServiceImpl implements IHelloService {  @Override  public String hello() {    return "hi, you!";  }}

    启动Spring Boot应用。

如何消费Dubbo服务

  • 添加依赖
  • com.alibaba.spring.boot
    dubbo-spring-boot-starter
    1.0.0

    在application.properties添加dubbo的相关配置信息,demo配置如下:

  • # dubbo配置spring.dubbo.appname=consumer-testspring.dubbo.protocol=dubbo# 此处也可使用其它协议,比如zookeeper://xxxx:xxspring.dubbo.registry=multicast://224.0.0.0:1111spring.dubbo.port=20801spring.dubbo.group=testspring.dubbo.version=1.0.0

    接下来在Spring Boot Application上添加@EnableDubboConfiguration,表示要开启dubbo功能。

  • /** * Consumer启动类 * * @author xionghui * @since 1.0.0 */@SpringBootApplication@EnableDubboConfigurationpublic class DubboConsumerLauncher {  public static void main(String[] args) {    SpringApplication.run(DubboConsumerLauncher.class, args);  }}

    通过注入需要使用的interface:

  • /** * Consumer Controller * * @author xionghui * @since 1.0.0 */@RestController@RequestMapping("/")public class ConsumerController {  // timeout表示dubbo超时时间,单位为ms  @Reference(timeout = 1000)  private IHelloService iHelloService;  @RequestMapping(value = "hello", method = RequestMethod.GET)  @ResponseBody  public String hello() {    return this.iHelloService.hello();  }}

    启动Spring Boot应用。

  • 可以通过http://localhost:port/health监控服务信息:
  • {	status: "UP",	dubbo: {		status: "UP",		ClassIdBean [clazz=interface org.test.IHelloService, group=test, version=1.0.0]: true	},	diskSpace: {		status: "UP",		total: 487955914752,		free: 455584600064,		threshold: 10485760	}}

    最后通过http://localhost:port/hello调用RPC服务,返回结果:

  • hi, you!

     

参考文档

  • dubbo: 
  • spring-boot: 
  • dubbo-spring-boot-starter: 

转载于:https://my.oschina.net/xionghui/blog/824945

你可能感兴趣的文章
分块矩阵和行列式
查看>>
陶哲轩实分析引理8.4.5
查看>>
WINCE 下载地址(转)
查看>>
Linux 小知识翻译 - 「单CD 的linux」
查看>>
Linux 小知识翻译 - 「RFC」
查看>>
20145234黄斐《信息安全系统设计基础》期中总结
查看>>
STM32F103 强制转换
查看>>
ANGULAR 开发用户选择器指令
查看>>
Java String编码转换(转自http://blog.csdn.net/okman1214/article/details/4397772)
查看>>
css3-巧用选择器 “:target”
查看>>
JPEG最优压缩参数试验【光影魔术手VS Image Optimizer】
查看>>
accp
查看>>
单例模式
查看>>
html02表格的使用
查看>>
【待续】【HTML5】用Canvas标签创建第一张条线图
查看>>
zookeeper之 zkServer.sh命令、zkCli.sh命令、四字命令
查看>>
Hbase shell 常用命令
查看>>
oracle之 v$sql_monitor 监视正在运行的SQL语句的统计信息
查看>>
SEO之优化代码
查看>>
【BZOJ4025】 二分图(线段树分治)
查看>>