2014-05-06 57 views
1

我已經瞭解了這個問題,但仍然不滿足於核心。如何配置多個數據庫實例?

要添加多個數據庫配置,下面是我得到迄今已是信息 -

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)); 
    } 
} 

但是,這是我要進入黑匣子的地方,我不知道要更改什麼配置或如何更改!

欣賞一些輸入。

回答

0

我最近不得不做一些類似的事情,我使用BeanDefinitionRegistryPostProcessor。我使用jdbcTemplates,但它應該轉移到休眠會話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); 

    } 
方法
相關問題