首页 > 解决方案 > 在springboot项目的verticles之间使用eventBus进行通信时没有地址处理程序

问题描述

我使用 Springboot 开发了一个项目,并使用 Vertx 作为异步响应式工具包。我的 ServerVerticle,创建一个 httpServer,它接收来自 Angular 应用程序的 http 请求并通过 eventBus 向它发送消息。顺便说一句,收到消息的时间,ServerVerticle 将它发送到另一个有服务实例的 Verticle(用于连接到存储库)。我用邮递员对其进行了测试,并得到“没有地址处理程序”错误作为错误请求。
这是我的 ServerVerticle:

HttpServerResponse res = routingContext.response();
res.setChunked(true);
EventBus eventBus = vertx.eventBus();
eventBus.request(InstrumentsServiceVerticle.FETCH_INSTRUMENTS_ADDRESS, "", result -> {
    if (result.succeeded()) {
        res.setStatusCode(200).write((Buffer) result.result().body()).end();
    } else {
        res.setStatusCode(400).write(result.cause().toString()).end();
    }

});

我的 instrumentVerticle 如下:

static final String FETCH_INSTRUMENTS_ADDRESS = "fetch.instruments.service";
// Reuse the Vert.x Mapper :)
private final ObjectMapper mapper = Json.mapper;

private final InstrumentService instrumentService;

public InstrumentsServiceVerticle(InstrumentService instrumentService) {
    this.instrumentService = instrumentService;
}

private Handler<Message<String>> fetchInstrumentsHandler() {
    return msg -> vertx.<String>executeBlocking(future -> {
                try {
                    future.complete(mapper.writeValueAsString(instrumentService.getInstruments()));
                } catch (JsonProcessingException e) {
                    logger.error("Failed to serialize result "+ InstrumentsServiceVerticle.class.getName());
                    future.fail(e);
                }
            },
            result -> {
                if (result.succeeded()) {
                    msg.reply(result.result());
                } else {
                    msg.reply(result.cause().toString());
                }
            });
}

@Override
public void start() throws Exception {
    super.start();
    vertx.eventBus().<String>consumer(FETCH_INSTRUMENTS_ADDRESS).handler(fetchInstrumentsHandler());
}

我在 springbootApp 启动器中部署了两个 Verticle。

标签: vert.x

解决方案


推荐阅读