一、官方例程解讀
官方例程 raw-jdbc-nodep-example 解讀
/**
* Sharding類(lèi)型:
* 1 分庫(kù)demo
* 2 分表demo
* 3 同時(shí)分庫(kù)和分表demo
* 4 主從demo
* 5 Sharding主從demo
*/
1. Demo1 分庫(kù)
- 數(shù)據(jù)源配置
@Override
public DataSource getDataSource() throws SQLException {
/**
* 初始化ShardingRule配置類(lèi)
*/
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
/**
* 設(shè)置TableRule和ItemRule表規(guī)則
*/
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
/**
* 設(shè)置默認(rèn)的分庫(kù)策略配置(setDefaultDatabaseShardingStrategyConfig方法是分庫(kù)方法)
* (分庫(kù)依據(jù)是參數(shù)user_id, 分庫(kù)算法是表名最后一個(gè)后綴2取余)
*/
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration("user_id",
"demo_ds_${user_id % 2}"));
/**
* 獲取數(shù)據(jù)源對(duì)象
*/
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new HashMap<String, Object>(), new Properties());
}
2. Demo2 分表
- 數(shù)據(jù)源配置
public DataSource getDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
/**
* 設(shè)置t_order和t_order_item的表規(guī)則
*/
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
/**
* 將表t_order和t_order_item添加到綁定的表組中
*/
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
/**
* 設(shè)置分表策略(分表依據(jù)列order_id)
*/
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
new StandardShardingStrategyConfiguration("order_id",
new PreciseModuloShardingTableAlgorithm()));
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(),
shardingRuleConfig, new HashMap<String, Object>(), new Properties());
}
-. 分表配置
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("t_order");
// 設(shè)置實(shí)際的數(shù)據(jù)節(jié)點(diǎn)(即哪張表耗跛,表0或者表1)
result.setActualDataNodes("demo_ds.t_order_${[0, 1]}"); // 分表才會(huì)設(shè)置
// 設(shè)置主鍵生成列
result.setKeyGeneratorColumnName("order_id");
return result;
}
-
運(yùn)行結(jié)果
3. Demo3 同時(shí)分庫(kù)分表
- 數(shù)據(jù)源配置
@Override
public DataSource getDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
/**
* 分庫(kù)策略配置DatabaseSharding
*/
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new InlineShardingStrategyConfiguration("user_id",
"demo_ds_${user_id % 2}"));
/**
* 分表策略配置TableSharding
*/
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
new StandardShardingStrategyConfiguration("order_id",
new PreciseModuloShardingTableAlgorithm()));
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(),
shardingRuleConfig, new HashMap<String, Object>(), new Properties());
}
-. 分庫(kù)分表配置
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("t_order");
/**
* 分表依據(jù)
*/
result.setActualDataNodes("demo_ds_${0..1}.t_order_${[0, 1]}");
result.setKeyGeneratorColumnName("order_id");
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("t_order_item");
/**
* 分表依據(jù)
*/
result.setActualDataNodes("demo_ds_${0..1}.t_order_item_${[0, 1]}");
return result;
}
- 運(yùn)行結(jié)果:同上
4. Demo4 主從
-. 數(shù)據(jù)源配置
@Override
public DataSource getDataSource() throws SQLException {
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration();
/**
* 設(shè)置主從配置名稱
*/
masterSlaveRuleConfig.setName("demo_ds_master_slave");
/**
* 設(shè)置主數(shù)據(jù)源名稱
*/
masterSlaveRuleConfig.setMasterDataSourceName("demo_ds_master");
/**
* 設(shè)置從數(shù)據(jù)源名稱
*/
masterSlaveRuleConfig.setSlaveDataSourceNames(Arrays.asList("demo_ds_slave_0",
"demo_ds_slave_1"));
return MasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(),
masterSlaveRuleConfig, new ConcurrentHashMap<String, Object>(), new Properties());
}
-. 分庫(kù)分表配置 (不需要)
5. Demo5 主從數(shù)據(jù)庫(kù)且分庫(kù)分表
-. 數(shù)據(jù)源配置
public DataSource getDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
/**
* 添加分庫(kù)分表規(guī)則
*/
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
/**
* 配置默認(rèn)的分庫(kù)策略
*/
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new StandardShardingStrategyConfiguration("user_id",
new PreciseModuloShardingDatabaseAlgorithm()));
/**
* 配置默認(rèn)的分表策略
*/
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
new StandardShardingStrategyConfiguration("order_id",
new PreciseModuloShardingTableAlgorithm()));
/**
* 配置主從數(shù)據(jù)庫(kù)規(guī)則
*/
shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations());
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(),
shardingRuleConfig, new HashMap<String, Object>(), new Properties());
}
-. 主從數(shù)據(jù)庫(kù)配置
/**
* 設(shè)置主從數(shù)據(jù)庫(kù)規(guī)則
*
* @return
*/
private static List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
MasterSlaveRuleConfiguration masterSlaveRuleConfig1 = new MasterSlaveRuleConfiguration();
masterSlaveRuleConfig1.setName("ds_0");
masterSlaveRuleConfig1.setMasterDataSourceName("demo_ds_master_0");
masterSlaveRuleConfig1.setSlaveDataSourceNames(Arrays.asList("demo_ds_master_0_slave_0",
"demo_ds_master_0_slave_1"));
MasterSlaveRuleConfiguration masterSlaveRuleConfig2 = new MasterSlaveRuleConfiguration();
masterSlaveRuleConfig2.setName("ds_1");
masterSlaveRuleConfig2.setMasterDataSourceName("demo_ds_master_1");
masterSlaveRuleConfig2.setSlaveDataSourceNames(Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
return Lists.newArrayList(masterSlaveRuleConfig1, masterSlaveRuleConfig2);
}
-. 分庫(kù)配置 (數(shù)據(jù)源配置中已經(jīng)配置)
/**
* 添加分表規(guī)則
*/
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
/**
* 配置默認(rèn)的分表策略
*/
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
new StandardShardingStrategyConfiguration("order_id",
new PreciseModuloShardingTableAlgorithm()));
/**
* 配置默認(rèn)的分庫(kù)策略
*/
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new StandardShardingStrategyConfiguration("user_id",
new PreciseModuloShardingDatabaseAlgorithm()));
-. 分表配置
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("t_order");
result.setActualDataNodes("ds_${0..1}.t_order_${[0, 1]}");
result.setKeyGeneratorColumnName("order_id");
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable("t_order_item");
result.setActualDataNodes("ds_${0..1}.t_order_item_${[0, 1]}");
return result;
}
感謝你的閱讀!