mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -102,7 +102,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
private final CountDownLatch initedLatch = new CountDownLatch(2);
|
||||
|
||||
private boolean enable = false;
|
||||
private boolean enable = true;
|
||||
|
||||
private boolean resetStatEnable = true;
|
||||
|
||||
@@ -221,8 +221,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
this.id = DruidDriver.createDataSourceId();
|
||||
|
||||
enable = true;
|
||||
|
||||
if (maxActive <= 0) {
|
||||
throw new IllegalArgumentException("illegal maxActive " + maxActive);
|
||||
}
|
||||
@@ -324,7 +322,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
destoryConnectionThread.start();
|
||||
|
||||
initedLatch.await();
|
||||
inited = true;
|
||||
|
||||
createdTime = new Date();
|
||||
DruidDataSourceStatManager.add(this);
|
||||
@@ -335,6 +332,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
} catch (InterruptedException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
} finally {
|
||||
inited = true;
|
||||
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,28 +3,36 @@ package com.alibaba.druid.pool.ha;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.balance.Balancer;
|
||||
import com.alibaba.druid.pool.ha.balance.RoundRobinBlancer;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements DataSource {
|
||||
public class HADataSource extends MultiDataSource implements HADataSourceMBean, DataSource {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
|
||||
private final AtomicLong masterConnectCount = new AtomicLong();
|
||||
private final AtomicLong slaveConnectCount = new AtomicLong();
|
||||
|
||||
protected DruidDataSource master;
|
||||
protected DruidDataSource slave;
|
||||
|
||||
protected Balancer balancer = new RoundRobinBlancer();
|
||||
|
||||
public HADataSource(){
|
||||
|
||||
}
|
||||
|
||||
public long getMasterConnectCount() {
|
||||
return masterConnectCount.get();
|
||||
}
|
||||
|
||||
public long getSlaveConnectCount() {
|
||||
return slaveConnectCount.get();
|
||||
}
|
||||
|
||||
public DruidDataSource getMaster() {
|
||||
return master;
|
||||
}
|
||||
@@ -43,20 +51,59 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
this.slave = slave;
|
||||
}
|
||||
|
||||
public boolean isMasterEnable() {
|
||||
if (master == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return master.isEnable();
|
||||
}
|
||||
|
||||
public void setMasterEnable(boolean value) {
|
||||
if (master == null) {
|
||||
throw new IllegalStateException("slave is null");
|
||||
}
|
||||
|
||||
master.setEnable(value);
|
||||
}
|
||||
|
||||
public boolean isSlaveEnable() {
|
||||
if (slave == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return slave.isEnable();
|
||||
}
|
||||
|
||||
public void setSlaveEnable(boolean value) {
|
||||
if (slave == null) {
|
||||
throw new IllegalStateException("slave is null");
|
||||
}
|
||||
|
||||
slave.setEnable(value);
|
||||
}
|
||||
|
||||
public synchronized void setDataSources(List<DruidDataSource> dataSources) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Balancer getBalancer() {
|
||||
return balancer;
|
||||
}
|
||||
|
||||
public void setBalancer(Balancer balancer) {
|
||||
this.balancer = balancer;
|
||||
}
|
||||
|
||||
public Connection getConnectionInternal(MultiDataSourceConnection connection, String sql) throws SQLException {
|
||||
return this.balancer.getConnection(connection, sql);
|
||||
Connection conn = null;
|
||||
if (master.isEnable()) {
|
||||
conn = master.getConnection();
|
||||
masterConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (slave.isEnable()) {
|
||||
conn = slave.getConnection();
|
||||
slaveConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (conn == null) {
|
||||
throw new SQLException("get HAConnection error");
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
public void handleNotAwailableDatasource(DruidDataSource dataSource) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
public interface HADataSourceMBean {
|
||||
long getMasterConnectCount();
|
||||
|
||||
long getSlaveConnectCount();
|
||||
|
||||
boolean isMasterEnable();
|
||||
|
||||
void setMasterEnable(boolean value);
|
||||
|
||||
boolean isSlaveEnable();
|
||||
|
||||
void setSlaveEnable(boolean value);
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public abstract class MultiDataSource extends DataSourceAdapter {
|
||||
protected void close() {
|
||||
scheduler.shutdownNow();
|
||||
|
||||
Object[] items = this.getDataSources().toArray();
|
||||
Object[] items = this.getDataSources().values().toArray();
|
||||
for (Object item : items) {
|
||||
JdbcUtils.close((DruidDataSource) item);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.alibaba.druid.pool.ha.balance;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
@@ -40,7 +42,8 @@ public class RoundRobinBlancer implements Balancer {
|
||||
|
||||
try {
|
||||
// 处理并发时的错误
|
||||
dataSource = multiDataSource.getDataSources().get(index);
|
||||
List<DruidDataSource> dataSources = new ArrayList<DruidDataSource>(multiDataSource.getDataSources().values());
|
||||
dataSource = dataSources.get(index);
|
||||
} catch (Exception ex) {
|
||||
indexErrorCount.incrementAndGet();
|
||||
if (LOG.isDebugEnabled()) {
|
||||
|
||||
@@ -23,7 +23,7 @@ public class SimpleBalancer implements Balancer {
|
||||
public Connection getConnection(MultiDataSourceConnection connectionProxy, String sql) throws SQLException {
|
||||
MultiDataSource multiDataSource = connectionProxy.getHaDataSource();
|
||||
|
||||
Object[] array = multiDataSource.getDataSources().toArray();
|
||||
Object[] array = multiDataSource.getDataSources().values().toArray();
|
||||
|
||||
DruidDataSource minDataSource = null;
|
||||
int min = 0;
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.alibaba.druid.pool.ha.cobar;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.MultiDataSource;
|
||||
import com.alibaba.druid.pool.ha.MultiDataSourceConnection;
|
||||
|
||||
|
||||
public class CobarDataSource extends MultiDataSource {
|
||||
private List<DruidDataSource> dataSources = new ArrayList<DruidDataSource>();
|
||||
|
||||
@Override
|
||||
public List<DruidDataSource> getDataSources() {
|
||||
return dataSources;
|
||||
}
|
||||
|
||||
public void setDataSources(List<DruidDataSource> dataSources) {
|
||||
this.dataSources = dataSources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return new MultiDataSourceConnection(this, createConnectionId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnectionInternal(MultiDataSourceConnection conn, String sql) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleNotAwailableDatasource(DruidDataSource dataSource) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.pool.ha.config;
|
||||
|
||||
public class HADataSourceConfig {
|
||||
|
||||
private DruidDataSourceConfig master;
|
||||
private DruidDataSourceConfig slave;
|
||||
|
||||
public DruidDataSourceConfig getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(DruidDataSourceConfig master) {
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public DruidDataSourceConfig getSlave() {
|
||||
return slave;
|
||||
}
|
||||
|
||||
public void setSlave(DruidDataSourceConfig slave) {
|
||||
this.slave = slave;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user