Vertx之MQTT客户端服务端发送

介绍

Vert.x MQTT消息发送, 客户端和服务端

1. maven项目依赖

<dependencies>
	<dependency>
		<groupId>io.vertx</groupId>
		<artifactId>vertx-web</artifactId>
	</dependency>
	<dependency>
		<groupId>io.vertx</groupId>
		<artifactId>vertx-mqtt</artifactId>
	</dependency>
	<dependency>
		<groupId>io.vertx</groupId>
		<artifactId>vertx-config-yaml</artifactId>
	</dependency>
	<dependency>
		<groupId>com.fasterxml.jackson.core</groupId>
		<artifactId>jackson-databind</artifactId>
	</dependency>
	<dependency>
		<groupId>com.lance.common</groupId>
		<artifactId>vertx-common-core</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</dependency>
</dependencies>

2.YAML文件配置

server:
  host: 127.0.0.1
  port: 18003

3.MQTT服务端配置

public class MqttServerApp extends AbstractVerticle {
  private final static String CLIENT_ID = "clientHello";

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    ConfigProperties properties = config().mapTo(ConfigProperties.class);
    int port = properties.getServer().getPort();
    log.info("===>json: {}, port: {}", properties, port);

    MqttServer mqttServer = MqttServer.create(vertx, create(properties));
    mqttServer.endpointHandler(endpoint -> {
          // shows main connect info
          log.info("MQTT client [{}] request to connect, clean session 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Boot中整合Vert.x开发MQTT应用程序,可以按照以下步骤进行操作: 1. 添加依赖:在项目的pom.xml文件中添加以下依赖,包括Vert.xMQTT相关的依赖: ```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`中编写自定义的消息处理逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值