我对 Vertx 很陌生,但我对测试它与 Spring 的集成非常感兴趣。我使用 Spring boot 来提升项目,并部署了两个 Verticle。我希望他们使用事件总线相互通信,但失败了。这就是我所做的:
在主要应用程序中:
@SpringBootApplication 公共类 MySpringVertxApplication { @Autowired MyRestAPIServer myRestAPIServer; @Autowired MyRestAPIVerticle MyRestAPIVerticle;
public static void main(String[] args) { SpringApplication.run(MySpringVertxApplication.class, args); } @PostConstruct public void deployVerticles(){ System.out.println("deploying..."); Vertx.vertx().deployVerticle(MyRestAPIVerticle); Vertx.vertx().deployVerticle(myRestAPIServer); }
}
在 APIVerticle 中:
@Component 公共类 MyRestAPIVerticle 扩展 AbstractVerticle {
public static final String ALL_ACCOUNT_LISTING = "com.example.ALL_ACCOUNT_LISTING"; @Autowired AccountService accountService; EventBus eventBus; @Override public void start() throws Exception { super.start(); eventBus = vertx.eventBus(); MessageConsumer<String> consumer = eventBus.consumer(MyRestAPIVerticle.ALL_ACCOUNT_LISTING); consumer.handler(message -> { System.out.println("I have received a message: " + message.body()); message.reply("Pretty Good"); }); consumer.completionHandler(res -> { if (res.succeeded()) { System.out.println("The handler registration has reached all nodes"); } else { System.out.println("Registration failed!"); } }); }
}
最后是ServerVerticle:
@Service 公共类 MyRestAPIServer 扩展 AbstractVerticle {
HttpServer server; HttpServerResponse response; EventBus eventBus; @Override public void start() throws Exception { server = vertx.createHttpServer(); Router router = Router.router(vertx); eventBus = vertx.eventBus(); router.route("/page1").handler(rc -> { response = rc.response(); response.setChunked(true); eventBus.send(MyRestAPIVerticle.ALL_ACCOUNT_LISTING, "Yay! Someone kicked a ball", ar->{ if(ar.succeeded()){ System.out.println("Response is :"+ar.result().body()); } } ); }); server.requestHandler(router::accept).listen(9999);
}
但是在我启动它并访问/page1之后,消息根本无法从ServerVerticle发送到APIVerticle。如果我将事件总线消费者移动到与 Sender 相同的 Verticle 中,则可以接收到事件。
在两个verticles之间发送消息有什么问题吗?我怎样才能让它工作?
提前致谢。