vert.x笔记:5.vert.x集成dubbo服务
来源:互联网 发布:冒充淘宝客服诈骗 编辑:程序博客网 时间:2024/06/06 08:57
原文及更多文章请见个人博客: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://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上下文获取service this.service = (DubboService) ctx.getBean("dubboService"); } @Override public 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 { @Override public 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()); }}
阅读全文
0 0
- vert.x笔记:5.vert.x集成dubbo服务
- vert.x笔记:4.vert.x中调用spring服务
- vert.x笔记:1.vert.x介绍
- # vert.x学习笔记
- Vert.x
- Vert.x
- vert.x笔记:3.使用vert.x发布restful接口
- vert.x笔记:6.vert.x集群化部署
- Vert.x学习笔记(一) Vert.x 核心包
- Vert.x学习笔记(二) Vert.x Web Client
- vert.x笔记:2.hello vert.x--第一个vert.x hello world工程
- Vert.x 3---Vert.x 实现REST
- Vert.x开发指南
- Vert.x安装指南
- Vert.x特性
- vert.x event bus
- vert.x入门
- Verticle in Vert.x
- [typeScript语法3]interface
- 36 WebGL两个简单模型组合成一个具有交互效果的模型
- 《数据结构学习与实验指导》2-12:两个有序链表序列的交集
- ios 添加监听
- 集成U-Share 分享
- vert.x笔记:5.vert.x集成dubbo服务
- 【UGUI】按钮需要绑定带参数的事件或绑定多个事件
- linux等用过的命令
- [YTU]_2921( Shape系列-7)
- Tensorflow: Math变量初始化
- vert.x笔记:6.vert.x集群化部署
- 【ionic】我的第一个ionic程序
- 在springmvc中使用itext生成pdf文件
- 音乐客户端的计时 App