This article introduces the configuration method of using sharding jdbc for spring boot. I will share it with you. The details are as follows:
illustrate
To exclude DataSourceAutoConfiguration, otherwise multiple data sources cannot be configured
@SpringBootApplication@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } The configured multiple data sources are handed over to sharding-jdbc for management, sharding-jdbc creates a DataSource data source for mybatis use
Official document: http://shardingjdbc.io/index_zh.html
step
Configure multiple data sources, it is best to have certain rules for the name of the data source to facilitate the configuration of the calculation rules for the database.
@Bean(initMethod="init", destroyMethod="close", name="dataSource0")@ConfigurationProperties(prefix = "spring.datasource")public DataSource dataSource0(){ return new DruidDataSource();}@Bean(initMethod="init", destroyMethod="close", name="dataSource1")@ConfigurationProperties(prefix = "spring.datasource2")public DataSource dataSource1(){ return new DruidDataSource();} Configure data source rules, that is, hand over multiple data sources to sharding-jdbc for management, and can set default data sources. When the table does not have database rules configured, the default data sources will be used.
@Beanpublic DataSourceRule dataSourceRule(@Qualifier("dataSource0") DataSource dataSource0, @Qualifier("dataSource1") DataSource dataSource1){ Map<String, DataSource> dataSourceMap = new HashMap<>(); dataSourceMap.put("dataSource0", dataSource0); dataSourceMap.put("dataSource1", dataSource1); return new DataSourceRule(dataSourceMap, "dataSource0");} Configure data source policies and table policies, and implement specific policies yourself.
@Beanpublic ShardingRule shardingRule(DataSourceRule dataSourceRule){ //Table Policy TableRule orderTableRule = TableRule.builder("t_order") .actualTables(Arrays.asList("t_order_0", "t_order_1")) .tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())) .dataSourceRule(dataSourceRule) .build(); TableRule orderItemTableRule = TableRule.builder("t_order_item") .actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")) .tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())) .dataSourceRule(dataSourceRule) .build(); //Binding table policy, the main table strategy will be used to calculate the routed data source when querying, so the rules of the table that agree to bind the table policy need to be consistent, which can improve efficiency to a certain extent List<BindingTableRule> bindingTableRules = new ArrayList<BindingTableRule>(); bindingTableRules.add(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))); return ShardingRule.builder() .dataSourceRule(dataSourceRule) .tableRules(Arrays.asList(orderTableRule, orderItemTableRule)) .bindingTableRules(bindingTableRule) .databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm())) .tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())) .build();} Create a data source DataSource for sharding-jdbc, which MybatisAutoConfiguration will use
@Bean("dataSource")public DataSource shardingDataSource(ShardingRule shardingRule){ return ShardingDataSourceFactory.createDataSource(shardingRule);} Need to manually configure the transaction manager (the reason is unknown)
//Configuration transaction needs to be manually declared @Beanpublic DataSourceTransactionManager transactionitonManager(@Qualifier("dataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource);} Simple implementation of library partition strategy, interface: DatabaseShardingAlgorithm
import java.util.Collection;import java.util.LinkedHashSet;import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;import com.google.common.collect.Range;/** * Created by fuwei.deng on May 11, 2017. */public class ModuloDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Long> { @Override public String doEqualSharding(Collection<String> databaseNames, ShardingValue<Long> shardingValue) { for (String each : databaseNames) { if (each.endsWith(shardingValue.getValue() % 2 + "")) { return each; } } throw new IllegalArgumentException(); } @Override public Collection<String> doInSharding(Collection<String> databaseNames, ShardingValue<Long> shardingValue) { Collection<String> result = new LinkedHashSet<>(databaseNames.size()); for (Long value : shardingValue.getValues()) { for (String tableName : databaseNames) { if (tableName.endsWith(value % 2 + "")) { result.add(tableName); } } return result; } @Override public Collection<String> doBetweenSharding(Collection<String> databaseNames, ShardingValue<Long> shardingValue) { Collection<String> result = new LinkedHashSet<>(databaseNames.size()); Range<Long> range = (Range<Long>) shardingValue.getValueRange(); for (Long i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { for (String each : databaseNames) { if (each.endsWith(i % 2 + "")) { result.add(each); } } return result; }} Basic implementation of table sub-stage strategy, interface: TableShardingAlgorithm
import java.util.Collection;import java.util.LinkedHashSet;import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;import com.google.common.collect.Range;/** * Created by fuwei.deng on May 11, 2017. */public class ModuloTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Long> { @Override public String doEqualSharding(Collection<String> tableNames, ShardingValue<Long> shardingValue) { for (String each : tableNames) { if (each.endsWith(shardingValue.getValue() % 2 + "")) { return each; } } throw new IllegalArgumentException(); } @Override public Collection<String> doInSharding(Collection<String> tableNames, ShardingValue<Long> shardingValue) { Collection<String> result = new LinkedHashSet<>(tableNames.size()); for (Long value : shardingValue.getValues()) { for (String tableName : tableNames) { if (tableName.endsWith(value % 2 + "")) { result.add(tableName); } } } return result; } @Override public Collection<String> doBetweenSharding(Collection<String> tableNames, ShardingValue<Long> shardingValue) { Collection<String> result = new LinkedHashSet<>(tableNames.size()); Range<Long> range = (Range<Long>) shardingValue.getValueRange(); for (Long i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { for (String each : tableNames) { if (each.endsWith(i % 2 + "")) { result.add(each); } } return result; }} At this point, the function of dividing the database and dividing the table has been implemented
Read and write separation
Read and write separation requires adding a layer of master and slave data source creation before creating DataSourceRule
// Build a read-write separation data source. The read-write separation data source implements the DataSource interface, which can be directly processed as a data source. // masterDataSource0, slaveDataSource00, slaveDataSource01, etc. are real data sources configured using DBCP and other connection pools. DataSource masterSlaveDs0 = MasterSlaveDataSourceFactory.createDataSource("ms_0", masterDataSource0, slaveDataSource00, slaveDataSource01);DataSource masterSlaveDs1 = MasterSlaveDataSourceFactory.createDataSource("ms_1", masterDataSource1, slaveDataSource11, slaveDataSource11);// Build a sub-store data source Map<String, DataSource> dataSourceMap = new HashMap<>(2);dataSourceMap.put("ms_0", masterSlaveDs0);dataSourceMap.put("ms_1", masterSlaveDs1);// Continue to create ShardingDataSource through ShardingDataSourceFactory When using the main library
HintManager hintManager = HintManager.getInstance();hintManager.setMasterRouteOnly();// Continue JDBC operation
Forced routing
HintManager hintManager = HintManager.getInstance();hintManager.addDatabaseShardingValue("t_order", "user_id", 1L);hintManager.addTableShardingValue("t_order", "order_id", order.getOrderId());hintManager.addDatabaseShardingValue("t_order_item", "user_id", 1L);hintManager.addTableShardingValue("t_order_item", "order_id", order.getOrderId());Transactions
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.