TDDL 源码分析

来源:互联网 发布:js仿淘宝上拉查看详情 编辑:程序博客网 时间:2024/06/06 19:52

Web应用架构:

TDDL位于数据库和持久层之间,它直接与数据库建立交道
这里写图片描述

TDDL架构:

这里写图片描述
TDDL其实主要可以划分为3层架构,分别是Matrix层、Group层和Atom层。Matrix层用于实现分库分表逻辑,底层持有多个Group实例。而Group层和Atom共同组成了动态数据源,Group层实现了数据库的Master/Salve模式的写分离逻辑,底层持有多个Atom实例。最后Atom层(TAtomDataSource)实现数据库ip,port,password,connectionProperties等信息的动态推送,以及持有原子的数据源分离的JBOSS数据源。

DataSource源码分析:

DataSource 接口:

public interface DataSource  extends CommonDataSource, Wrapper {  // 返回连接给JdbcTemplate使用  Connection getConnection() throws SQLException;  Connection getConnection(String username, String password)    throws SQLException;}

AbstractTAtomDataSource 抽象类:

public abstract class AbstractTAtomDataSource implements DataSource {    protected abstract DataSource getDataSource() throws SQLException;    public abstract void init() throws Exception;    public abstract void flushDataSource();    public abstract void destroyDataSource() throws Exception;    public Connection getConnection() throws SQLException {        return getDataSource().getConnection();    }    public Connection getConnection(String username, String password) throws SQLException {        return getDataSource().getConnection(username, password);    }    public PrintWriter getLogWriter() throws SQLException {        return getDataSource().getLogWriter();    }    public int getLoginTimeout() throws SQLException {        return getDataSource().getLoginTimeout();    }    public void setLogWriter(PrintWriter out) throws SQLException {        getDataSource().setLogWriter(out);    }    public static void setShutDownMBean(boolean shutDownMBean) {        TDDLMBeanServer.shutDownMBean=shutDownMBean;    }    public void setLoginTimeout(int seconds) throws SQLException {        getDataSource().setLoginTimeout(seconds);    }    @SuppressWarnings("unchecked")    public <T> T unwrap(Class<T> iface) throws SQLException    {        if(isWrapperFor(iface)){            return (T) this;        }else{            throw new SQLException("not a wrapper for "+ iface);        }    }    public boolean isWrapperFor(Class<?> iface) throws SQLException    {        return AbstractTAtomDataSource.class.isAssignableFrom(iface);    }    public static void main(String[] args) throws SQLException    {        StaticTAtomDataSource s = new StaticTAtomDataSource();        System.out.println(s.isWrapperFor(StaticTAtomDataSource.class));    }}

dataSource主要提供数据库的连接、刷新和关闭等操作的实现

TAtomDataSource 源码分析:

public class TAtomDataSource extends AbstractTAtomDataSource {    // 本地缓存    private static Map<String, TAtomDsConfHandle> cacheConfHandleMap = new HashMap<String, TAtomDsConfHandle>();    // 处理数据库配置同步    private volatile TAtomDsConfHandle dsConfHandle = new TAtomDsConfHandle();    ...}

TAtomDsConfHandle 源码分析:

class TAtomDsConfHandle {    // Atom层的配置实体类    private volatile TAtomDsConfDO runTimeConf = new TAtomDsConfDO();    private TAtomDsConfDO localConf = new TAtomDsConfDO();    // 数据库配置管理类    private DbConfManager dbConfManager;    private DbPasswdManager dbPasswdManager;    // 监听列表    private volatile List<TAtomDbStatusListener> dbStatusListeners;    ...}

DbconfManager 实现类 DiamondDbConfManager

public class DiamondDbConfManager implements DbConfManager {    private ConfigDataHandlerFactory configFactory;    // 同步配置处理者    private ConfigDataHandler globalHandler;    private ConfigDataHandler appDBHandler;    private volatile List<ConfigDataListener> globalDbConfListener = new ArrayList<ConfigDataListener>();    private volatile List<ConfigDataListener> appDbConfListener = new ArrayList<ConfigDataListener>();    public void init() {        // 默认配置信息        configFactory = new DefaultConfigDataHandlerFactory();        Map<String, String> config = new HashMap<String, String>();        config.put("group", TAtomConstants.DEFAULT_DIAMOND_GROUP);        globalHandler = configFactory.getConfigDataHandlerWithListenerListCE(                globalConfigDataId, globalDbConfListener,                Executors.newSingleThreadScheduledExecutor(), config);        appDBHandler = configFactory.getConfigDataHandlerWithListenerListCE(                appConfigDataId, appDbConfListener,                Executors.newSingleThreadScheduledExecutor(), config);    }    ...}

ConfigDataHandler 实现类:

public class DiamondConfigDataHandler implements ConfigDataHandler {    private static final Log logger = LogFactory            .getLog(DiamondConfigDataHandler.class);    private DiamondManager diamondManager;    private String dataId;    private String mbeanId;    private TDDLMBean mbean;    public void init(final String dataId,            final List<ConfigDataListener> configDataListenerList,            final Map<String, Object> config) {        mbean = new TDDLMBean("Diamond Config Info "                + System.currentTimeMillis());        mbeanId = dataId + System.currentTimeMillis();        DiamondConfig.handleConfig(config);        DefaultDiamondManager.Builder builder = new DefaultDiamondManager.Builder(                dataId, new ManagerListener() {                    // 同步配置信息接口                    public void receiveConfigInfo(String data) {                        if (configDataListenerList != null) {                            // 给监听者同步信息                            for (ConfigDataListener configDataListener : configDataListenerList) {                                configDataListener.onDataRecieved(dataId, data);                            }                            if (data != null) {                                mbean.setAttribute(dataId, data);                            } else {                                mbean.setAttribute(dataId, "");                            }                        }                    }                    public Executor getExecutor() {                        return (Executor) config.get("executor");                    }                });        String group = (String) config.get("group");        if (null != group) {            builder.setGroup(group);        }        this.diamondManager = builder.build();        this.dataId = dataId;        TDDLMBeanServer.registerMBeanWithId(mbean, mbeanId);    }

TGroupDataSource 源码分析:

public class TGroupDataSource implements DataSource {    private ConfigManager configManager;    private String dsKeyAndWeightCommaArray;    // 数据源获取类(Atom层)    private DataSourceFetcher dataSourceFetcher;    ...}

DataSourceFetcher 接口:

public interface DataSourceFetcher {    DataSource getDataSource(String key);    DBType getDataSourceDBType(String key);}

DataSourceFetcher 的2个实现类:
1.

private class MyDataSourceFetcher implements DataSourceFetcher {        private DBType dbType = DBType.MYSQL;        @Override        public DataSource getDataSource(String dsKey) {            // String->DataSource,相当于配置映射文件            DataSourceWrapper dsw = dataSourceWrapperMap.get(dsKey);            if (dsw != null) {                dbType = dsw.getDBType();                return dsw.getWrappedDataSource();            } else {                if (createTAtomDataSource) {                    TAtomDataSource atom = createTAtomDataSource(dsKey);                    dbType = DBType.valueOf(atom.getDbType().name());                    return atom;                } else {                    throw new IllegalArgumentException(dsKey + " not exist!");                }            }        }        @Override        public DBType getDataSourceDBType(String key) {            return dbType;        }    };

2.

public class SpringDataSourceFetcher implements DataSourceFetcher, ApplicationContextAware {    private ApplicationContext springContext;     private DBType dbType = DBType.MYSQL;    // 通过Spring进行获取    @Override    public DataSource getDataSource(String key) {        return (DataSource) this.springContext.getBean(key);    }    ...}
0 0
原创粉丝点击