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));
}
}
但是,這是我要進入黑匣子的地方,我不知道要更改什麼配置或如何更改!
欣賞一些輸入。