1

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

  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 中:

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

    }

  3. 最后是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之间发送消息有什么问题吗?我怎样才能让它工作?

提前致谢。

4

2 回答 2

1

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

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

尝试这个:

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle);
vertx.deployVerticle(myRestAPIServer);
于 2016-03-09T09:03:01.750 回答
0

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

Vertx vertx = Vertx.vertx();
vertx.deployVerticle(MyRestAPIVerticle.class.getName());
vertx.deployVerticle(MyRestAPIServer.class.getName());
于 2018-11-29T04:41:12.617 回答