先解释下什么编排治理:
1.配置集中化与动态化,可支持数据源、表与分片策略的动态切换(2.0.0.M1)
2.客户端的数据库治理,数据源失效自动切换(2.0.0.M2)
3.基于Open Tracing协议的APM信息输出(2.0.0.M3)
先面开始撸源码:
依旧由单元测试入手,单元测试写的好的一个原则就是:单元测试可以被当做文档来用。自动校验的文档。
YamlOrchestrationMasterSlaveIntegrateTest
@Test
public void assertWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(YamlOrchestrationMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = OrchestrationMasterSlaveDataSourceFactory.createDataSource(yamlFile);
} else {
dataSource = OrchestrationMasterSlaveDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
}
}), yamlFile);
}
try (Connection conn = dataSource.getConnection();
Statement stm = conn.createStatement()) {
stm.executeQuery("SELECT * FROM t_order");
stm.executeQuery("SELECT * FROM t_order_item");
stm.executeQuery("SELECT * FROM t_config");
}
OrchestrationDataSourceCloseableUtil.closeQuietly(dataSource);
}
注意其中一个门面类,封装了一个标志信息 3个service 1个监听器 加一个注册中心
OrchestrationFacade implements AutoCloseable {
private final boolean isOverwrite;
private final ConfigurationService configService;
private final InstanceStateService instanceStateService;
private final DataSourceService dataSourceService;
private final ListenerFactory listenerManager;
private final RegistryCenter regCenter;
}
初始化 3个service 加一个监听器
/**
* Initialize for master-slave orchestration.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param configMap config map
* @throws SQLException SQL exception
*/
public void init(
final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig,
final Map<String, Object> configMap, final MasterSlaveDataSource masterSlaveDataSource) throws SQLException {
configService.persistMasterSlaveConfiguration(dataSourceMap, masterSlaveRuleConfig, configMap, isOverwrite);
instanceStateService.persistMasterSlaveInstanceOnline();
dataSourceService.persistDataSourcesNode();
listenerManager.initMasterSlaveListeners(masterSlaveDataSource);
}
init的时候再注册中心注册了dataSourde
configService.persistMasterSlaveConfiguration:
/**
* Persist master-slave configuration.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param configMap config map
* @param isOverwrite is overwrite registry center's configuration
*/
public void persistMasterSlaveConfiguration(
final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Map<String, Object> configMap, final boolean isOverwrite) {
persistDataSourceConfiguration(dataSourceMap, isOverwrite);
persistMasterSlaveRuleConfiguration(masterSlaveRuleConfig, isOverwrite);
persistMasterSlaveConfigMap(configMap, isOverwrite);
}
注册中心监听注册:
/**
* Initialize listeners for master-slave data source.
*
* @param masterSlaveDataSource master-slave data source
*/
public void initMasterSlaveListeners(final MasterSlaveDataSource masterSlaveDataSource) {
configurationListenerManager.start(masterSlaveDataSource);
instanceListenerManager.start(masterSlaveDataSource);
dataSourceListenerManager.start(masterSlaveDataSource);
configMapListenerManager.start(masterSlaveDataSource);
}
剩下的核心,就是搞明白这四个监听器都干了什么……
定制化自己的策略
网友评论