Vertx 事件总线无法将消息发送到不同的顶点



我对Vertx很陌生,但我对测试它与Spring的集成非常感兴趣。我使用 Spring boot 来提升项目,并部署了两个垂直点。我希望它们使用事件总线相互通信,但失败了。这是我所做的:

  1. 在主应用程序中:

    @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);
    }
    

    }

  2. 在APIVerticle中:

    @Componentpublic class MyRestAPIVerticle extensions 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!");
        }
      });
    }
    

    }

  3. 最后服务器垂直:

    @Servicepublic class MyRestAPIServer extensions 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 相同的垂直点,则可以接收事件。

在两个顶点之间发送消息有什么问题吗?我怎样才能让它工作?

提前谢谢。

您在单独的 vertx 实例中部署了它们:

Vertx.vertx().deployVerticle(MyRestAPIVerticle);
Vertx.vertx().deployVerticle(myRestAPIServer);

试试这个:

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle);
vertx.deployVerticle(myRestAPIServer);
Vertx

事件总线不会像您尝试的那样在不同的 Vertx 实例之间共享(但集群 Vert.x 应用程序可以做到这一点)。在您的情况下,将其更改为使用单个 Vert.x 实例,如下所示 MySpringVertxApplication .

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle.class.getName());
vertx.deployVerticle(MyRestAPIServer.class.getName());

最新更新