spring vertx
by Rick Lee
李瑞克(Rick Lee)
如何在Spring设置Vertx (How to set up Vertx in Spring)
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.
Spring可能是Java领域最受欢迎的框架。 我们都喜欢它的依赖注入和所有自动装配/配置魔术。 它使单元测试变得轻而易举。
On the other hand, Vertx.io, 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 Async MySQL / PostgreSQL Client, which make it another trendy choice besides Spring.
另一方面, 作为一种较新的工具箱/框架的Vertx.io在最近几年越来越受欢迎 。 它轻巧,并通过事件循环(如Node.js)和事件总线消息传递(如Akka)支持完全异步编程。 另外,社区已经开发了很多异步工具/数据库客户端,例如Async MySQL / PostgreSQL Client ,这使它成为除了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 Spring Initializer. Then add the following to the pom.xml:
首先,创建一个简单的Maven Springboot应用程序。 您可以通过Spring Initializer创建它。 然后将以下内容添加到pom.xml中:
As we’re going to query mysql using Async MySQL / PostgreSQL Client, a very primitive MysqlClient.java is created and the MySQL configuration is put on the application.yaml.
当我们使用Async MySQL / PostgreSQL Client查询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: https://github.com/rickcodetalk/spring-vertx-example
源代码在这里: https : //github.com/rickcodetalk/spring-vertx-example
翻译自: https://www.freecodecamp.org/news/vertx-in-spring-39c2dd7bc2a9/
spring vertx