使用环境:
jdk8
springboot2.7.11
shardingsphere5.4.0
场景、问题:
从5.3.0升级到5.4.0,通过javaAPI配置多数据源及分库分表策略,查询不参与分库的表时,提示表不存在。
已进行操作:
@Configuration
public class ShardingSphereConfig {
@Value("${spring.db.url}")
private String url;
@Value("${spring.db.username}")
private String username;
@Value("${spring.db.password}")
private String password;
@Bean
public ShardingSphereDataSource shardingSphereDataSource() throws SQLException {
// 创建ShardingSphereDataSource
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 配置数据源
dataSourceMap.put("ds-aa", createDataSource("order_aa"));
dataSourceMap.put("ds-bb", createDataSource("order_bb"));
StringBuffer actualNodeBuffer = new StringBuffer("${[");
dataSourceMap.keySet().forEach(datasource->actualNodeBuffer.append("'"+datasource+"',"));
String actualNodeStr = actualNodeBuffer.toString().substring(0, actualNodeBuffer.toString().length()-1)+"]}";
// 配置分片规则
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//设置默认分库规则
shardingRuleConfig.setDefaultDatabaseShardingStrategy(new NoneShardingStrategyConfiguration());
//设置分片算法
shardingRuleConfig.setShardingAlgorithms(createAlgorithmConfigurations());
// 配置分表规则
ShardingTableRuleConfiguration orderRuleConfiguration = new ShardingTableRuleConfiguration("order", actualNodeStr+".order_$->{0..4}");
orderRuleConfiguration.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_no", "database-split"));
orderRuleConfiguration.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_no", "order-no-split"));
shardingRuleConfig.getTables().add(orderRuleConfiguration);
return (ShardingSphereDataSource) ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), new Properties());
}
private Map<String, AlgorithmConfiguration> createAlgorithmConfigurations(){
Map<String, AlgorithmConfiguration> algorithmConfigurations = new HashMap<>();
Properties databaseProperties = new Properties();
databaseProperties.setProperty("strategy","standard");
databaseProperties.setProperty("algorithmClassName", "com.uni.sharding.DBShardingAlgorithm");
AlgorithmConfiguration dbConfiguration = new AlgorithmConfiguration("CLASS_BASED", databaseProperties);
algorithmConfigurations.put("database-split", dbConfiguration);
Properties orderNoProperties = new Properties();
orderNoProperties.setProperty("strategy","standard");
orderNoProperties.setProperty("algorithmClassName", "com.uni.sharding.TableFulfillmentNoShardingAlgorithm");
AlgorithmConfiguration orderNoConfiguration = new AlgorithmConfiguration("CLASS_BASED", orderNoProperties);
algorithmConfigurations.put("order-no-split", orderNoConfiguration);
return algorithmConfigurations;
}
private DataSource createDataSource(final String dataSourceName){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl(String.format(url, dataSourceName));
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxActive(500);
dataSource.setInitialSize(5);
dataSource.setMaxWait(60000);
dataSource.setMinIdle(10);
dataSource.setTimeBetweenEvictionRunsMillis(60000);
dataSource.setMinEvictableIdleTimeMillis(300000);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setTestOnReturn(false);
return dataSource;
}
}