spring vertx_如何在Spring设置Vertx

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中整合Vert.x开发MQTT应用程序,可以按照以下步骤进行操作: 1. 添加依赖:在项目的pom.xml文件中添加以下依赖,包括Vert.x和MQTT相关的依赖: ```xml <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-spring-boot-starter</artifactId> <version>2.1.6</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-mqtt-server</artifactId> <version>4.2.1</version> </dependency> ``` 2. 创建MQTT服务器:在Spring Boot应用程序的入口类中,创建一个Vert.x的`Vertx`实例,并使用`Vertx`实例创建一个`MqttServer`实例。 ```java import io.vertx.core.Vertx; import io.vertx.mqtt.MqttServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Bean public MqttServer mqttServer(Vertx vertx) { return MqttServer.create(vertx); } } ``` 3. 编写MQTT消息处理器:创建一个实现`MqttEndpointHandler`接口的类,用于处理接收到的MQTT消息。可以根据业务需求自定义消息处理逻辑。 ```java import io.vertx.mqtt.MqttEndpoint; import io.vertx.mqtt.MqttEndpointHandler; public class MyMqttEndpointHandler implements MqttEndpointHandler { @Override public void handle(MqttEndpoint endpoint) { // 处理消息逻辑 } } ``` 4. 注册MQTT消息处理器:在Spring Boot应用程序的入口类中,将自定义的`MqttEndpointHandler`实例注册到`MqttServer`中。 ```java import io.vertx.core.Vertx; import io.vertx.mqtt.MqttServer; import io.vertx.mqtt.MqttServerOptions; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Bean public MqttServer mqttServer(Vertx vertx, MqttEndpointHandler endpointHandler) { MqttServerOptions options = new MqttServerOptions() .setPort(1883); // 设置MQTT服务器端口 MqttServer mqttServer = MqttServer.create(vertx, options); mqttServer.endpointHandler(endpointHandler); // 注册消息处理器 return mqttServer; } @Bean public MqttEndpointHandler endpointHandler() { return new MyMqttEndpointHandler(); } } ``` 通过以上步骤,你就可以在Spring Boot应用程序中使用Vert.x开发MQTT应用程序了。启动应用程序后,Vert.x MQTT服务器将在指定的端口上监听并处理接收到的MQTT消息。你可以根据需要在`MyMqttEndpointHandler`中编写自定义的消息处理逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值