Vert.x 3: SockJS 服务代理
Vert.x 3: 服务代理
Vert.x 3: Node和浏览器端事件总线重连html服务代理提供了在事件总线上暴露服务, 而且减小调用服务所要求的代码的一种方式. 服务代理帮助你解析事件总线的消息结构和接口方法的映射关系, 好比方法名称, 参数等等. 本质上是一种RPC. 它经过代码生成的方式建立服务代理.java
所以须要实现两个Verticle, 一个为 QrcodeService
, 提供二维码生成服务. 另外一个 Verticle 用于服务注册.git
Verticle 运行在一个两节点的集群环境中中github
要提供一个服务, 须要一个服务接口, 一个实现和一个Verticlenpm
服务接口: QrcodeService
, 服务接口, 定义了二维码生成和代理建立接口.编程
服务实现: QrcodeServiceImpl
, 服务实现类, 实际的二维码生成函数在这里实现json
服务注册: QrcodeServiceVerticle
, 用于注册服务segmentfault
服务接口是经过 @ProxyGen
标注的接口, 例如:浏览器
package com.totorotec.service.qrcode; import io.vertx.codegen.annotations.ProxyGen; import io.vertx.codegen.annotations.VertxGen; import io.vertx.core.AsyncResult; import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.json.JsonObject; import com.totorotec.service.qrcode.impl.QrcodeServiceImpl; @ProxyGen @VertxGen public interface QrcodeService { public static final String SERVICE_ADDRESS = "com.totorotec.servicefactory.qrcode-service"; static QrcodeService create(Vertx vertx, JsonObject config) { return new QrcodeServiceImpl(vertx, config); } static QrcodeService createProxy(Vertx vertx, String address) { return new QrcodeServiceVertxEBProxy(vertx, address); } void getQrcode(String text, int imageSize, String imageType, String outputType, String filePatten, Handler<AsyncResult<JsonObject>> resultHandler); }
注意:
java.lang.IllegalStateException: Cannot find proxyClass
, 把`插件的版本升级到
3.7.0`maven
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
另外: 须要给插件
maven-compiler-plugin
增长符号处理器配置annotationProcessor
, 以下
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <annotationProcessors> <annotationProcessor>io.vertx.codegen.CodeGenProcessor</annotationProcessor> </annotationProcessors> </configuration> </plugin>
完整的 pom.xml 项目文件能够参考: https://github.com/developerw...
实现了一个服务提供者, 下面咱们来讲明如何消(调)费(用)这个服务.
package com.totorotec.service.qrcode; import io.vertx.core.AbstractVerticle; import io.vertx.core.logging.Logger; import io.vertx.core.logging.LoggerFactory; /** * QrcodeServiceConsumer */ public class QrcodeServiceConsumer extends AbstractVerticle { private static final Logger logger = LoggerFactory.getLogger(QrcodeServiceConsumer.class); @Override public void start() throws Exception { super.start(); QrcodeService qrcodeServiceProxy = QrcodeService.createProxy(vertx, QrcodeService.SERVICE_ADDRESS); qrcodeServiceProxy.getQrcode("https://www.qq.com", 600, "jpg", "file", "/tmp/%s.%s", ar -> { if (ar.succeeded()) { logger.info(ar.result().encodePrettily()); } else { logger.error(ar.cause()); } }); } }
var service = require("qrcode-service-js/qrcode_service"); console.log("Creating service proxy..."); var proxy = service.createProxy(vertx, "com.totorotec.servicefactory.qrcode-service"); proxy.getQrcode("https://www.qq.com", 380, "png", "dataurl", "/tmp/%s.%s", function (error, data) { if(error == null) { console.log(data); } else { console.log(error); } });
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Test Qrcode Service in Browser</title> <script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script> <script src="./vertx-eventbus.js"></script> <script src="./qrcode_service-proxy.js"></script> </head> <body> <div id="qrcode"></div> <script> var qrcodeStr; var eb = new EventBus('http://localhost:8080/eventbus'); eb.onopen = function () { var service = new QrcodeService(eb, "com.totorotec.servicefactory.qrcode-service"); service.getQrcode("https://www.qq.com", 380, "png", "dataurl", "/tmp/%s.%s", function (error, data) { if (error == null) { console.log(data.data); qrcodeStr = data.data document.getElementById("qrcode").innerHTML = qrcodeStr; } else { console.log(error); } }); }; </script> </body> </html>
var EventBus = require("vertx3-eventbus-client"); var eb = new EventBus("http://localhost:8080/eventbus"); eb.onopen = function () { // 导入代理模块 var QrcodeService = require("../target/classes/qrcode-service-js/qrcode_service-proxy"); // 实例化服务对象 var service = new QrcodeService(eb, "com.totorotec.servicefactory.qrcode-service"); // 调用服务 service.getQrcode("https://www.qq.com", 380, "png", "dataurl", "/tmp/%s.%s", function (data, error) { if(error == null) { console.log(data); } else { console.log(error); } }); }
事件总线, 对于异构系统集成来说是一个很好的工具. 经过定义服务接口, 服务实现, 代理类代码生成, 服务注册, 事件总线桥, 咱们能够把异构系统的各个端点链接到事件总线中, 实现分布式的, 异构系统的通讯, 事件处理.
异构还特别对团队有用, 大的团队使用不一样的开发工具, 语言, 运行时系统等, 均可以很方便的进行集成, 只要你在JVM的生态里面, 无论你使用JVM的什么语言. 即便你不在JVM生态里面, 例如Node.js, 浏览器, 其余编程语言等, Vert.x还提供了一个TCP事件总线桥的方式进行集成.
咱们前面只介绍了SockJS这种集成方式, 当前互联网应用程序大部分使用HTTP做为应用层协议, 原生TCP的方式用的比较少, 在这里就不详细说明了, 有须要的能够参考Vertx的文档: http://vertx.io/docs/vertx-tc...
完整的项目和实现能够参考个人仓库: https://github.com/developerw...