前言
引用oschina上关于vertx的文章,感觉他说得非常明白,不在这里过多讨论,这里我简单说明下如何在vertx和spring配合完成基础任务。
服务启动:
public static void main( String[] args ) { ApplicationContext context = new AnnotationConfigApplicationContext(SimpleSpringConfiguration.class); final Vertx vertx = Vertx.vertx(); vertx.deployVerticle(new SpringSimpleVerticle(context)); vertx.deployVerticle(new ServerVerticle()); }
EventBus接受给Service:
public class SpringSimpleVerticle extends AbstractVerticle { public static final String ALL_PRODUCTS_ADDRESS = "example.all.products"; private final ObjectMapper mapper = new ObjectMapper(); private final ProductService service; public SpringSimpleVerticle(final ApplicationContext context) { service = (ProductService) context.getBean("productService"); } private Handler<Message<String>> allProductsHandler(ProductService service, String name) { return ms2g -> vertx.<String>executeBlocking(future -> { try { future.complete(mapper.writeValueAsString(service.getAllProducts(ms2g.body()))); } catch (JsonProcessingException e) { System.out.println("Failed to serialize result"); future.fail(e); } }, result -> { if (result.succeeded()) { ms2g.reply(result.result()); } else { ms2g.reply(result.cause().toString()); } }); } @Override public void start() throws Exception { super.start(); System.out.println("<<<<<<<<<<<<<<<<<<<<<<< CONSUMER >>>>>>>>>>>>>>>>>>>>>>>>>"); vertx.eventBus().<String>consumer(ALL_PRODUCTS_ADDRESS).handler(allProductsHandler(service, "message")); } }
传统Service+Dao
@Service public class ProductService { @Autowired private ProductRepository repo; public List<Product> getAllProducts(String productId) { System.out.println("productid : " + productId); return repo.findAll(); } public void getProduct(String productId) { System.out.println("productid : " + productId); } }
端口监听:
public class ServerVerticle extends AbstractVerticle { @Override public void start() throws Exception { super.start(); HttpServer server = vertx.createHttpServer(); server.requestHandler(req -> { if (req.method() == HttpMethod.GET) { req.response().setChunked(true); if (req.path().equals("/products")) { vertx.eventBus().<String>send(SpringSimpleVerticle.ALL_PRODUCTS_ADDRESS, "123456", result -> { if (result.succeeded()) { req.response().setStatusCode(200).write(result.result().body()).end(); } else { req.response().setStatusCode(500).write(result.cause().toString()).end(); } }); } else { req.response().setStatusCode(200).write("Hello from vert.x").end(); } } else { // We only support GET for now req.response().setStatusCode(405).end(); } }); server.listen(8080); } }
结论:
DML操作基本都是类似的做法,但是观念的改变其实很大,传统烟囱的方式=》基于消息、事件的方式转移
相关推荐
vertx-mybatis-spring-async jar包
我想看看新的闪亮(好吧,不是真的)Spring Reactor与Vert.x的比较 这两个软件包都是用Groovy编写的。 这个想法是将9 hash get命令发送给redis。 这是通过以下方式完成的: 在vert.x示例中,使用Vertx-Redis-...
Spring Boot和Vertx Vert.x SpringBoot示例显示了如何将Vert.x嵌入到SpringBoot应用程序中。 要求 2. maven and jdk 8 从源头建造 使用github主页上的URL克隆git存储库: $ git clone ...
vertx for springboot 是基于vert.x集成spring boot框架,只需要启动自己的eureka service 就能启动
顶点弹簧数据Spring Data(JPA、MongoDB 等)的 Vertx mod ###mod-spring-data 这个允许您在 Vert.x 应用程序中使用 Spring Data,从而可以直接使用 JPA、MongoDB 等。 ###Sample applications 提供了示例以展示如何...
javascript Vertx开发教程
赠送jar包:vertx-web-3.9.0.jar; 赠送原API文档:vertx-web-3.9.0-javadoc.jar; 赠送源代码:vertx-web-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-web-3.9.0.pom; 包含翻译后的API文档:vertx-web-...
模块名称是jacpfx.vertx.spring (请注意包含 spring 依赖项,例如“需要 spring.context;”) 将 Vert.x 更新到 3.5.1 版 将 Spring 更新到 5.0.5.RELEASE 版本 用法 将 spring-vertx-ext 依赖项添加到您的项目中,...
自己写的一个java+vertx+mybatis的网关、服务分发,用于app后台接口开发的基本框架,仅供参考,请不要用于商业用途。
Vert.x助手 专案 vertx-web:构建带有注释的Web服务; vertx-starter:用于弹簧启动的vertx starter项目; 版本号 vertx-web maven描述: ... < artifactId>vertx-spring-boot-starter</ artifact
赠送jar包:vertx-web-3.9.0.jar; 赠送原API文档:vertx-web-3.9.0-javadoc.jar; 赠送源代码:vertx-web-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-web-3.9.0.pom; 包含翻译后的API文档:vertx-web-...
vertx3基本详情入门讲解,ppt格式的,里面含有vertx3的基本讲解和重要的组建信息
赠送jar包:vertx-core-3.9.0.jar; 赠送原API文档:vertx-core-3.9.0-javadoc.jar; 赠送源代码:vertx-core-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-core-3.9.0.pom; 包含翻译后的API文档:vertx-core...
赠送jar包:vertx-auth-common-3.9.0.jar; 赠送原API文档:vertx-auth-common-3.9.0-javadoc.jar; 赠送源代码:vertx-auth-common-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-auth-common-3.9.0.pom; ...
赠送jar包:vertx-bridge-common-3.9.0.jar; 赠送原API文档:vertx-bridge-common-3.9.0-javadoc.jar; 赠送源代码:vertx-bridge-common-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-bridge-common-3.9.0....
赠送jar包:vertx-core-3.9.0.jar; 赠送原API文档:vertx-core-3.9.0-javadoc.jar; 赠送源代码:vertx-core-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-core-3.9.0.pom; 包含翻译后的API文档:vertx-core...
使用IDEA和Gradle构建Vertx项目,详细操作步骤,也可以去查阅我的博客文章
vertx_cheatsheet
赠送jar包:vertx-bridge-common-3.9.0.jar; 赠送原API文档:vertx-bridge-common-3.9.0-javadoc.jar; 赠送源代码:vertx-bridge-common-3.9.0-sources.jar; 赠送Maven依赖信息文件:vertx-bridge-common-3.9.0....
TPA API BASED ON VERTX