增加数据库主备自动切换功能
This commit is contained in:
parent
6b4e43b159
commit
87a4d7270b
@ -46,8 +46,7 @@ public class StandbyDataSource extends DruidDataSource implements InitializingBe
|
||||
/**
|
||||
* 备库url连接
|
||||
*/
|
||||
private String[] standbyUrls;
|
||||
private volatile int standbyIndex = 0;
|
||||
private String standbyUrl;
|
||||
|
||||
@Override
|
||||
public void init() throws SQLException {
|
||||
@ -56,7 +55,7 @@ public class StandbyDataSource extends DruidDataSource implements InitializingBe
|
||||
// 第一次初始化
|
||||
if (!lastInited && inited) {
|
||||
// 如果没有配置,不启用主备切换
|
||||
if (standbyUrls == null || standbyUrls.length == 0) {
|
||||
if (standbyUrl == null) {
|
||||
return;
|
||||
}
|
||||
scheduledExecutorService.schedule(new ValidateUrlTask(), 1, TimeUnit.SECONDS);
|
||||
@ -79,10 +78,8 @@ public class StandbyDataSource extends DruidDataSource implements InitializingBe
|
||||
//如果这个数据源已经被初始化了,同时连接异常才进行处理
|
||||
if (isInited() && !isConnectionActive()) {
|
||||
// 主库有问题时,切换备库
|
||||
LOG.debug("Change standby urls, index:{}", standbyIndex);
|
||||
String standbyUrl = standbyUrls[standbyIndex];
|
||||
LOG.debug("Change standby urls");
|
||||
changeUrl(standbyUrl);
|
||||
standbyIndex++;
|
||||
break;
|
||||
}
|
||||
try {
|
||||
@ -105,8 +102,8 @@ public class StandbyDataSource extends DruidDataSource implements InitializingBe
|
||||
setUrl(url);
|
||||
}
|
||||
|
||||
public void setStandbyUrls(String[] standbyUrls) {
|
||||
this.standbyUrls = standbyUrls;
|
||||
public void setStandbyUrl(String standbyUrl) {
|
||||
this.standbyUrl = standbyUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user