【问题标题】:Setting multiple instances for Verticles using vertx embedded and Guice使用 vertx embedded 和 Guice 为 Verticle 设置多个实例
【发布时间】:2015-09-29 14:58:11
【问题描述】:

我在同一台机器上使用 Vertx 集群(开发模式)。 我也在使用 guice 来注入一些 pojo。

但是,当我尝试增加 Verticle 实例时,我得到:

java.lang.IllegalArgumentException: Can't specify > 1 instances for already created verticle
    at io.vertx.core.impl.DeploymentManager.deployVerticle(DeploymentManager.java:70)
    at io.vertx.core.impl.VertxImpl.deployVerticle(VertxImpl.java:516)
    at io.vertx.core.impl.VertxImpl.deployVerticle(VertxImpl.java:511)
    at com.mycompany.world_map_service.web.starter.StarterVerticle.lambda$main$0(StarterVerticle.java:39)

这是我的配置方式:

 public static void main(String[] args) throws Exception {

        final Logger logger = Logger.getLogger(StarterVerticle.class);


        ClusterManager mgr = new HazelcastClusterManager();
        VertxOptions options = new VertxOptions().setClusterManager(mgr);
        Vertx.clusteredVertx(options, res -> {
            DeploymentOptions deploymentOptions = new DeploymentOptions().setConfig(config);
            if (res.succeeded()) {
                Vertx vertx = res.result();
                //  Injector injector = Guice.createInjector(new AppInjector(vertx));
                Injector injector = Guice.createInjector(new AppInjector(vertx, deploymentOptions));
                vertx.deployVerticle(injector.getInstance(VertxHttpServerVerticle.class), deploymentOptions.setInstances(3));
                ...
}

那是我的 AppInjector 类:

public class AppInjector extends AbstractModule {

    private Vertx vertx = null;
    private Context context = null;
    DeploymentOptions deploymentOptions = null;


    public AppInjector(Vertx vertx, DeploymentOptions deploymentOptions) {
        this.vertx = vertx;
        this.context = vertx.getOrCreateContext();
        this.deploymentOptions = deploymentOptions;
    }


    @Override
    protected void configure() {
        bind(LocationService.class).to(LocationServiceImpl.class).in(Singleton.class);
        bind(LocationServiceDAO.class).to(LocationServiceDaoImpl.class).in(Singleton.class);
        bind(RedisRepo.class).toProvider(() -> {
            return new RedisRepo(deploymentOptions.getConfig());
        });
        bind(Neo4jRepo.class).toProvider(() -> {
            return new Neo4jRepo(deploymentOptions.getConfig());
        });
    }
}

知道我为什么会发生碰撞吗? 我知道我应该按名称使用:com.mycompany.world_map_service.web.http.VertxHttpServerVerticle,但如果我注入依赖项,它们将为每个实例重复,不是吗?

【问题讨论】:

    标签: java guice inject vert.x


    【解决方案1】:

    我知道我在这里有点晚了,但是对于可能在路上找到这个问题的人来说,这就是“为什么”。按照错误信息:

    无法为已创建的verticle指定> 1个实例

    您正在使用 Guice 创建一个 Verticle 实例,然后尝试部署 3 个实例,但您已经创建了一个 Verticle 实例:

    vertx.deployVerticle(injector.getInstance(VertxHttpServerVerticle.class), deploymentOptions.setInstances(3));
    

    这就是您要传递类名的原因。您可以在io.vertx.core.impl.DeploymentManager 中看到流程,这也解释了上述错误。

    我知道我应该使用名称: “com.mycompany.world_map_service.web.http.VertxHttpServerVerticle”但是 如果我注入依赖项,它们将为每个重复 他们不会吗?

    我不确定你的意思。如果您出于某种原因不想复制数据/依赖项,也许您可​​以涉及另一个对象或单例,或利用 Vertx's Shared Data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2013-06-02
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多