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
- TDDL 源码分析
- Tddl分析之一
- TDDL
- TDDL
- TDDL
- TDDL
- TDDL-剖析淘宝TDDL
- TDDL-剖析淘宝TDDL
- TDDL使用
- TDDL 事务
- TDDL使用
- 阿里巴巴TDDL
- 淘宝TDDL数据库分库分表
- 剖析淘宝TDDL
- 分布式数据库,TDDL
- TDDL配置数据源
- TDDL最佳实践
- tddl diamond配置
- 【Android】【ViewGroup】【ScrollView】ScrollView的一些细节
- ES6中的一些新特性
- POJ1159 Palindrome(滚动数组DP)
- 关于阿里云服务器的常用配置和php开发总结
- 培训机构毕业的程序员被歧视的背后逻辑
- TDDL 源码分析
- 最大子序列
- (4.6.17.3)进程保活(二、Android层面的不足):细数利用android系统机制的保活手段
- NYOJ2括号配对问题
- 杭电 Problem 1047 Integer Inquiry【大数水题】
- Android高仿QQ左右滑动菜单的效果实现方式之安卓菜单左右滑动效果实现方式
- 短信验证码在互联网方面的应用
- 用c++ 通过thrift2 访问hbase数据库
- KMP算法学习笔记