vert.x笔记:5.vert.x集成dubbo服务

原文及更多文章请见个人博客:http://heartlifes.com
vert.x

基础介绍:

dubbo是阿里巴巴内部的rpc远程调用框架,和spring无缝对接,自带loadbalance,是用来搭建soa服务架构的利器,可惜听说在阿里内部斗争中,已经被hsf干掉了。但是,对于我们这种小企业来说,dubbo还是搭建高可用服务的不二选择。dubbo官方地址:http://dubbo.io

vert.x+dubbo可以搭建一个逼格很高的微服务架构,即vert.x用于发布服务,通过事件总线,调用后端的dubbo业务处理服务。从而完成rest服务与业务代码的完美解耦。

本章基用到前序章节的所有知识,并且这里将不会介绍dubbo服务的开发,默认你会玩dubbo服务。

pom中加入以下依赖:

<dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.3.6</version><exclusions><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version>
</dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.41</version>
</dependency>
<dependency><groupId>org.jboss.netty</groupId><artifactId>netty</artifactId><version>3.2.5.Final</version>
</dependency>

引用一个dubbo服务:

所谓集成dubbo,从本质来讲就是配置dubbo服务,并且以xml形式集成spring。
我们假设后台有这么一个dubbo服务,现在要在vert.x中调用,那么我们的做法和普通的dubbo consumer一样,申明一个spring配置文件,并引用该服务。配置文件如下:

<?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"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd "><!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 --><context:component-scan base-package="com"/><dubbo:application name="demo-consumer" /><dubbo:registry address="multicast://224.5.6.7:1234" /><dubbo:protocol name="dubbo" host="127.0.0.1" port="20802"serialization="hessian2" threadpool="cached" threads="1000" /><!-- dubbo引用的服务 --><dubbo:reference id="dubboService"interface="com.heartlifes.dubbo.DubboService" />
</beans> 

创建SpringVerticle:

package com.heartlifes.vertx.demo.dubbo;import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;import org.springframework.context.ApplicationContext;public class SpringVerticle extends AbstractVerticle {private DubboService service;public static final String PRINT_MSG_SERVICE_ADDRESS = "print_msg_service_address";public static final String GET_MSG_SERVICE_ADDRESS = "get_msg_service_address";public SpringVerticle(ApplicationContext ctx) {// 从spring上下文获取servicethis.service = (DubboService) ctx.getBean("dubboService");}@Overridepublic void start() throws Exception {// 唤起事件总线,注册一个事件处理者,或者直译叫事件消费者vertx.eventBus().<String> consumer(PRINT_MSG_SERVICE_ADDRESS).handler(msg -> {// 获取事件内容后,调用service服务// 这里是非阻塞式调用service.printMsg("Asynchronous call dubbo service!!!");msg.reply("success");});vertx.eventBus().<String> consumer(GET_MSG_SERVICE_ADDRESS, printMsg());}// 模拟dubbo服务要从后台数据库获取数据,所以这里就是vert.x中的阻塞式调用// vert.x中规定,所有调用不可以阻塞其eventloop,所以当有数据库调用、thread.sleep等可能会阻塞线程的服务调动时// 需要使用vertx接口中的阻塞式调用接口private Handler<Message<String>> printMsg() {return msg -> {System.out.println("bus msg body is:" + msg.body());// 阻塞式接口调用vertx.<String> executeBlocking(future -> {// 通过future等待调用返回结果String dubboMsg = "";try {dubboMsg = this.service.getMsg();} catch (Exception e) {e.printStackTrace();future.fail(e);}// 把结果放到result中future.complete(dubboMsg);}, result -> {// 判断接口调用结果,成功的话讲结果放到事件总线的msg中传递给server端展示if (result.succeeded()) {System.out.println("msg from dubbo service is: "+ result.result());msg.reply(result.result());}if (result.failed()) {msg.fail(400, result.cause().getMessage());}});};}}

创建SpringVerticle:

package com.heartlifes.vertx.demo.dubbo;import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;/*** 基本代码注释,请参见vert.x笔记:3.使用vert.x发布restful接口* * @author john**/
public class ServerVerticle extends AbstractVerticle {@Overridepublic void start() throws Exception {Router router = Router.router(vertx);router.route().handler(BodyHandler.create());router.route("/dubbo/get").handler(// 唤起vert.x的事件总线,并发送一个简单消息ctx -> vertx.eventBus().<String> send(SpringVerticle.GET_MSG_SERVICE_ADDRESS,// 消息地址"event bus calls dubbo service",// 消息内容result -> {// 异步结果处理if (result.succeeded()) {// 成功的话,返回处理结果给前台,这里的处理结果就是service返回的一段字符串ctx.response().putHeader("content-type","application/json").end(result.result().body());} else {ctx.response().setStatusCode(400).end(result.cause().toString());}}));router.route("/dubbo/print").handler(// 唤起vert.x的事件总线,并发送一个简单消息ctx -> vertx.eventBus().<String> send(SpringVerticle.PRINT_MSG_SERVICE_ADDRESS,// 消息地址"event bus calls dubbo service",// 消息内容result -> {// 异步结果处理if (result.succeeded()) {// 成功的话,返回处理结果给前台ctx.response().putHeader("content-type","application/json").end("success");} else {ctx.response().setStatusCode(400).end(result.cause().toString());}}));vertx.createHttpServer().requestHandler(router::accept).listen(8080);}
}

创建启动器:

package com.heartlifes.vertx.demo.dubbo;import io.vertx.core.Vertx;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringMain {public static void main(String[] args) {// 配置文件方式ApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-consumer.xml");Vertx vertx = Vertx.vertx();// 部署spring模块vertx.deployVerticle(new SpringVerticle(ctx));// 部署服务器模块vertx.deployVerticle(new ServerVerticle());}}

Vert.x集成dubbo服务相关推荐

  1. Dubbo——SpringBoot集成Dubbo(@Autowired和@Reference的区别、Dubbo的服务治理)

    Dubbo--原生API实现远程调用_Strine的博客-CSDN博客 在上一篇文章中我们讲了如何使用原生API发起远程调用,显然这种方式肯定是非常麻烦的,因此我们这里就讲如何使用SpringBoot ...

  2. nacos集成dubbo实现远程服务调用多服务端2

    文章目录 一.版本选取.需求和项目简述 1. 版本选取 2. 项目模块说明 2. 需求说明 二.需求实战-依赖初始化 2.1. 创建maven父工程EShopParent 2.2. 创建子模块Dubb ...

  3. 一文教你 Dubbo 服务性能压测(with JMeter)

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:为什么魂斗罗只有 128 KB却可以实现那么长的剧情?个人原创+1博客:点击前往,查看更多 原文地址:https ...

  4. SpringCloud Greenwich(七)集成dubbo先启动消费者(check=false),然后启动提供者无法自动发现注册

    SpringCloud Greenwich集成dubbo先启动消费者(check=false),然后启动提供者无法自动发现注册问题. 官方说明:修复bug的提交时间 spring-cloud-star ...

  5. springboot dubbo引入包_spring boot 集成 dubbo 企业完整版

    一.什么是Spring Boot ? 现阶段的 Spring Boot 可谓是太火了,为什么呢?因为使用方便.配置简洁.上手快速,那么它是什么?从官网上我们可以看到,它是 Spring 开源组织下的一 ...

  6. SpringCloud集成Dubbo实现RPC调用

    SpringCloud轻松集成Dubbo实现RPC调用 很久之前在做微服务架构选型的时候就听说阿里的微服务RPC框架dubbo,当时与Spring Cloud以http协议调用的架构做对比.发现dub ...

  7. dubbo 服务压测_Dubbo高性能网关--Flurry介绍

    1.背景 什么是API网关,它的作用是什么,产生的背景是啥? 从架构的角度来看,API网关暴露http接口服务,其本身不涉及业务逻辑,只负责包括请求路由.负载均衡.权限验证.流量控制.缓存等等功能.其 ...

  8. java如何集成dubbo_boot集成dubbo踩过的坑

    这里只阐述如何集成生产者端,消费者端差不多雷同就不多叙述了: 1.首先引入相关依赖 io.dubbo.springboot spring-boot-starter-dubbo 1.0.0 2.然后配置 ...

  9. SpringBoot集成Dubbo

    #博学谷IT学习技术支持# 文章目录 1. 构建SpringBoot环境 1.1 创建一个dubbo-parent项目 1.2 引入依赖坐标 2. Linux 环境配置 2.1 安装docker 2. ...

最新文章

  1. 强化学习vs遗传算法-人工智能在模拟领域的应用
  2. .Net 转战 Android 4.4 日常笔记(3)--目录结构分析
  3. python的中文含义-python中的 * 和 ** 作用含义
  4. fedora 20 PIL
  5. linux中的定时,linux中的定时任务
  6. LINUX系统中动态链接库的创建与使用
  7. vo listVO paggerHelper mapper使用原则
  8. 7. 整数反转 golang
  9. 深圳linux测试题库,Linux认证考试题库及答案
  10. luogu P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
  11. 认真学习系列:Linux原理——《趣谈linux》学习笔记
  12. QT制作一个串口调试助手出现乱码问题
  13. 期货市场的大户黑手(最大的是华尔街 高盛之流)
  14. SEO与爱情,十字路口中的抉择
  15. Html5网页录音,js录音mp3
  16. leetcode 1567. 乘积为正数的最长子数组长度python
  17. 人生一定要知道的十大“博弈”!
  18. 至多删三个字符 (35分)
  19. 个人博客系统(Vue实现)的主页布局设计
  20. 字符编码笔记:ASCII,Unicode 和 UTF-8(转帖、留着自己学习)

热门文章

  1. Python - 爬虫 - Xpath定位之starts-with()和string()函数的简单使用
  2. 还摆个屁的烂?用Python画如此漂亮的专业插图 ?简直So easy!
  3. Mac系统下 brew 更换清华源
  4. Very fast template matching(非常快的模板匹配)
  5. 去除Echarts雷达图初始时的一条竖线
  6. java毕业生设计信息学院网站分析计算机源码+系统+mysql+调试部署+lw
  7. python余数定理mul_inv
  8. Android应用加固原理
  9. 学习笔记——初学差分放大电路
  10. 【Apache web服务器安全加固】