dropwizard 中的多个数据库

Multiple databases in dropwizard

我正在尝试读取和写入两个数据库,但我 运行 遇到了问题:

config.yml

database1:
  driverClass: com.mysql.jdbc.Driver
  user: user1
  password: user!23
  url: jdbc:mysql://url.to.connect:3306/db1
  properties: charSet: UTF-8
  maxWaitForConnection: 1s
  minSize: 8
  maxSize: 32
  checkConnectionWhileIdle: false
  checkConnectionHealthWhenIdleFor: 10s
  closeConnectionIfIdleFor: 1 minute

database2:
  driverClass: com.mysql.jdbc.Driver
  user: user2
  password: user!23
  url: jdbc:mysql://url.to.connect:3306/db2
  properties: charSet: UTF-8
  maxWaitForConnection: 1s
  minSize: 8
  maxSize: 32
  checkConnectionWhileIdle: false
  checkConnectionHealthWhenIdleFor: 10s
  closeConnectionIfIdleFor: 1 minute

configuration.java

public class ExampleConfiguration extends Configuration {
@Valid
@NotNull
private DataSourceFactory database1 = new DataSourceFactory();

@Valid
@NotNull
private DataSourceFactory database2 = new DataSourceFactory();

@JsonProperty("database1")
public DataSourceFactory getDb1DataSourceFactory() {
    return database1;
}

@JsonProperty("database2")
public DataSourceFactory getDb2DataSourceFactory() {
    return database2;
}
}

现在如下:Maintain multiple migration files in parallel in dropwizard,我尝试添加一个 bootstrap 包如下:

@Override
  public void initialize(Bootstrap< ExampleConfiguration > bootstrap) {
    bootstrap.addCommand(new RenderCommand());
    bootstrap.addBundle(new AssetsBundle());
    bootstrap.addBundle(new MigrationsBundle<ExampleConfiguration>() {
      @Override
      public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
        return configuration.getDb1DataSourceFactory();
      }
    });

    bootstrap.addBundle(new MigrationsBundle<ExampleConfiguration>() {
      @Override
      public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
        return configuration.getDb2DataSourceFactory();
      }
    });
    bootstrap.addBundle(hibernateBundle);

  }

但是我得到了command 'db' has been already used。我仍然不清楚 initialize() 中发生了什么。如何在数据库实例之间切换以进行读写操作? 谢谢。

您应该覆盖每个 MigrationsBundle 实现的方法 name 以提供唯一的命令名称。然后使用 CLI,您需要根据数据库配置指定正确的名称。