博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring vertx_如何在Spring设置Vertx
阅读量:2531 次
发布时间:2019-05-11

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

spring vertx

by Rick Lee

李瑞克(Rick Lee)

如何在Spring设置Vertx (How to set up Vertx in Spring)

is probably the most popular framework in the Java space. We all love its dependency injection and all that autowired/configuration magic. It makes unit testing a piece of cake.

可能是Java领域最受欢迎的框架。 我们都喜欢它的依赖注入和所有自动装配/配置魔术。 它使单元测试变得轻而易举。

On the other hand, , which is a newer toolkit/framework, is gaining traction in recent years. It is light-weight and supports fully asynchronous programming via event loop like Node.js and eventbus messaging like Akka. Also, the community has made quite a lot of asynchronous tools/db clients like , which make it another trendy choice besides Spring.

另一方面, 一种较新的工具箱/框架的在最近几年越来越 。 它轻巧,并通过事件循环(如Node.js)和事件总线消息传递(如Akka)支持完全异步编程。 另外,社区已经开发了很多异步工具/数据库客户端,例如 ,这使它成为除了Spring之外的另一种时尚选择。

It seems that it’s tough to choose between Vertx and Spring for new projects, but the good news is they are indeed not mutually exclusive! The following is a simple example to illustrate the setup.

对于新项目,很难在Vertx和Spring之间进行选择,但是好消息是它们的确不互斥! 以下是一个简单的示例来说明设置。

This example project is about deploying a Vertical in a Springboot application. The Vertical provides a function for querying MySQL using an Async MySQL client. The function can be called directly or via vertx.eventbus.

这个示例项目是关于在Springboot应用程序中部署Vertical的。 垂直提供了使用异步MySQL客户端查询MySQL的功能。 该函数可以直接调用,也可以通过vertx.eventbus调用。

First of all, create a simple maven Springboot application. You can create it through . Then add the following to the pom.xml:

首先,创建一个简单的Maven Springboot应用程序。 您可以通过创建它。 然后将以下内容添加到pom.xml中:

As we’re going to query mysql using , a very primitive MysqlClient.java is created and the MySQL configuration is put on the application.yaml.

当我们使用查询mysql时,创建了一个非常原始的MysqlClient.java并将MySQL配置放在application.yaml上。

Create a dummy user table with 2 fields and insert some data:

创建一个具有2个字段的虚拟用户表,并插入一些数据:

Optionally, create a repository class for accessing the user table:

(可选)创建一个存储库类以访问用户表:

Now we can create the vertical, which has a single method for handling MySQL queries.

现在我们可以创建垂直的,它具有用于处理MySQL查询的单一方法。

Finally, create the Spring application and add a deployVerticle method with the @PostConstruct annotation.

最后,创建Spring应用程序并添加带有@PostConstruct批注的deployVerticle方法。

If you run the Spring application, you will see the following System printout of “dbVerticle deployed” and it means the Verticle is running on the Spring application.

如果运行Spring应用程序,则会看到以下“ dbVerticle已部署”的系统打印输出,这意味着Verticle在Spring应用程序上运行。

2019-02-11 08:56:27.110  INFO 29444 --- [ntloop-thread-0] i.v.ext.asyncsql.impl.MYSQLClientImpl    : Creating configuration for localhost:33062019-02-11 08:56:27.442  INFO 29444 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'dbVerticle deployed2019-02-11 08:56:27.848  INFO 29444 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''2019-02-11 08:56:27.853  INFO 29444 --- [           main] n.r.s.SpringVertxExampleApplication      : Started SpringVertxExampleApplication in 5.393 seconds (JVM running for 6.671)

To test it, we can simply add a db query request right after the Verticle was deployed.

为了测试它,我们可以在部署Verticle之后直接添加一个数据库查询请求。

The console prints out the following:

控制台将打印出以下内容:

dbVerticle deployedsuccess[{"id":10466,"username":"ricklee"}][{"id":10468,"username":"maryjohnson"}]

This example illustrated how you can enjoy the facilities from both the Spring and Vertx world with a simple setup.

此示例说明了如何通过简单的设置来享受Spring和Vertx世界中的设施。

Source code here:

源代码在这里: :

翻译自:

spring vertx

转载地址:http://opuzd.baihongyu.com/

你可能感兴趣的文章
[JS]事件捕获和冒泡
查看>>
【译】SQL Server误区30日谈-Day10-数据库镜像在故障发生后,马上就能发现
查看>>
linq之where子句
查看>>
Socket之UDP发送文件
查看>>
多语言在线代码编辑器,可运行程序
查看>>
C#:使用UPnP来穿透NAT使内网接口对外网可见
查看>>
js+css 实现遮罩居中弹出层(随浏览器窗口滚动条滚动)
查看>>
项目管理的小故事
查看>>
Visual Studio不显示智能提示代码,快捷键Alt+→也不出现
查看>>
多文件调用(函数、结构体)
查看>>
C# 获取本地电脑所有的盘符
查看>>
D3.js学习(三)
查看>>
汇编语言实验9
查看>>
window资源管理器下无法打开ftp站点
查看>>
spring特点与好处
查看>>
html 自制属性
查看>>
面向对象术语概念
查看>>
细胞(cell) 矩阵快速幂
查看>>
HDU - 1272 小希的迷宫
查看>>
EntityFramework(1)基础概念与Database First
查看>>