Vert.x 是高度模块化的,各模块之间通过EventBus进行通信。EventBus提供发布订阅功能和点对点的消息服务,远程服务调用就是通过EventBus的点对点消息服务实现的。
每条消息在EventBus上都有一个地址,发布者向这个地址发送消息,接收者从这个地址接收消息。
SenderVerticle.java
import io.vertx.core.AbstractVerticle;
public class SenderVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
/*
.publish 会将消息传递给所有在地址上注册过的处理器
.send 只会将消息传递给该地址注册的其中一个处理器
*/
vertx.eventBus().send("edu.abc.hello", "hello world!", res -> {
if (res.succeeded()){
System.out.println("received reply : " + res.result().body());
}
});
//vertx.eventBus().publish("edu.abc.hello", "hello world!");
}
@Override
public void stop() throws Exception {
super.stop();
}
}
ReceiverVerticle.java