如何配置多个数据库实例?

时间:2014-05-06 19:26:16

标签: java dropwizard

我已经解决了现有的问题,但仍然不满意核心。

要添加多个数据库配置,以下是我到目前为止获得的信息 -

1)更新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
  validationQuery: "/* MyService Health Check */ SELECT 1"
  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
  validationQuery: "/* MyService Health Check */ SELECT 1"
  minSize: 8
  maxSize: 32
  checkConnectionWhileIdle: false
  checkConnectionHealthWhenIdleFor: 10s
  closeConnectionIfIdleFor: 1 minute

2)将更改添加到java配置文件中。

public class DropWizardConfiguration extends Configuration{

    @Valid
    @NotNull
    @JsonProperty
    private DatabaseConfiguration database1 = new DatabaseConfiguration();

    @Valid
    @NotNull
    @JsonProperty
    private DatabaseConfiguration database2 = new DatabaseConfiguration();

    public DatabaseConfiguration getDatabaseConfiguration1()
    {       
        return database1;
        }

        public DatabaseConfiguration getDatabaseConfiguration2()
    {       
        return database2;
        }
}

3)在这一步中,我应该更新DropWizard服务来处理相应的数据库实例。

public class DropWizardService extends Service<DropWizardConfiguration>{
    private final HibernateBundle<DropWizardConfiguration> hibernate = new HibernateBundle<DropWizardConfiguration>(Game.class) {

        // @Override
        public DatabaseConfiguration getDatabaseConfiguration(DropWizardConfiguration configuration){
            return configuration.getDatabaseConfiguration();
        }
    };

    @Override
    public void initialize(Bootstrap<DropWizardConfiguration> bootstrap) {  
        bootstrap.addBundle(hibernate);
    }

    public void run() throws Exception{
        this.run(new String[]{"server", "./config.yml"});
    }

    @Override
    public void run(DropWizardConfiguration configuration, Environment environment) throws Exception {
        SessionFactory factory = hibernate.getSessionFactory();     
        environment.addResource(new MyResource(factory));   
    }   
}

但这是我进入黑盒子的地方,我不确定要改变什么配置或如何改变!

欣赏一些意见。

1 个答案:

答案 0 :(得分:0)

我最近不得不做类似的事情,我这样做是通过使用BeanDefinitionRegistryPostProcessor。我使用了jdbcTemplates但它应该可以转移到hibernate会话aswel。这样做是因为我能够添加任意数量的数据库并且可以创建bean。然后只需在上下文文件中添加bean。

我所做的是在配置

@Valid
@NotNull
@JsonProperty("database")
private Collection<DataSourceConfig> databases;

public Collection<DataSourceConfig> getDatabase() {
    return databases;
}

这是我的DataSourceConfig类

public class DataSourceConfig {

@JsonProperty
private String name;

@JsonProperty
private String driverClassName;

@JsonProperty
private String username;

@JsonProperty
private String password;

@JsonProperty
private String url;

public String getDriverClassName() {
    return driverClassName;
}

public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

这是我的类实现BeanDefinitionPostProcessor

的方法
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

    /**
     * Builds JdbcTemplates for beans
     */
    Map<String,String> jdbcTemplateBeanNames = Maps.newHashMap();
    for (DataSourceConfig dsc : configuration.getDatabase()) {
        AbstractBeanDefinition jdbcTemplateDefinition = BeanDefinitionBuilder.rootBeanDefinition("org.springframework.jdbc.core.JdbcTemplate")
                .addPropertyValue("dataSource",
                        BeanDefinitionBuilder.genericBeanDefinition(DriverManagerDataSource.class)
                            .addPropertyValue("driverClassName", dsc.getDriverClassName())
                            .addPropertyValue("url", dsc.getUrl())
                            .addPropertyValue("username", dsc.getUsername())
                            .addPropertyValue("password", dsc.getPassword())
                            .getBeanDefinition()
                )
                .getBeanDefinition();
        String name = BeanDefinitionReaderUtils.registerWithGeneratedName(jdbcTemplateDefinition, registry);
        jdbcTemplateBeanNames.put(dsc.getName(), name);

    }