@RaigorJiang 老实说这个问题挺让我困惑的, 不知从哪开始排查. HINT强制路由的SPI Type似乎不是hint
?我尝试更改为如下代码后(加入Properties tableShardingAlgorithmrProps
来配置algorithmClassName
),抛出的异常居然是
No implementation class load from SPI `org.apache.shardingsphere.sharding.spi.ShardingAlgorithm` with type `hint`.
更改的代码是如下的样式
import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;
@Configuration
public class LocalShardingDatabasesAndTablesConfiguration {
// 创建 ShardingSphereDataSource
@Bean
public DataSource getDataSource() throws SQLException {
ModeConfiguration modeConfiguration = createModeConfiguration();
Map<String, DataSource> dataSourceMap = createDataSourceMap();
ShardingRuleConfiguration shardingRuleConfiguration = createShardingRuleConfiguration();
return ShardingSphereDataSourceFactory.createDataSource(
modeConfiguration,
dataSourceMap,
Collections.singleton(shardingRuleConfiguration),
new Properties());
}
// 配置分片规则
private ShardingRuleConfiguration createShardingRuleConfiguration() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
// 配置 tc 表规则
shardingRuleConfig.getTables().add(this.getTcTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("tc");
// 配置分表算法
Properties tableShardingAlgorithmrProps = new Properties();
tableShardingAlgorithmrProps.setProperty("algorithmClassName", "com.lingh.algorithm.HintXXXAlgorithm");
shardingRuleConfig.getShardingAlgorithms().put("linghAlgorithm", new ShardingSphereAlgorithmConfiguration("hint", tableShardingAlgorithmrProps));
return shardingRuleConfig;
}
// 配置 tc 表规则
private ShardingTableRuleConfiguration getTcTableRuleConfiguration() {
List<String> collect = Arrays.asList("tc_a", "tc_bb", "tc_ccc");
String collect1 = collect.stream()
.map(s -> "'" + s + "'")
.collect(Collectors.joining(","));
ShardingTableRuleConfiguration tc = new ShardingTableRuleConfiguration("tc", "ds0.${[" + collect1 + "]}");
tc.setTableShardingStrategy(new HintShardingStrategyConfiguration("linghAlgorithm"));
return tc;
}
// 配置真实数据源
private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 配置第 1 个数据源
HikariDataSource dataSource1 = new HikariDataSource();
dataSource1.setDriverClassName("org.h2.Driver");
dataSource1.setJdbcUrl("jdbc:h2:mem:test;MODE=MySQL;DATABASE_TO_LOWER=TRUE;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'");
dataSource1.setUsername("root");
dataSource1.setPassword("123456");
dataSourceMap.put("ds0", dataSource1);
return dataSourceMap;
}
private static ModeConfiguration createModeConfiguration() {
return new ModeConfiguration("Memory", null, true);
}
}
抛出的异常是
Caused by: org.apache.shardingsphere.spi.exception.ServiceProviderNotFoundException: No implementation class load from SPI `org.apache.shardingsphere.sharding.spi.ShardingAlgorithm` with type `hint`.
at org.apache.shardingsphere.spi.typed.TypedSPIRegistry.getRegisteredService(TypedSPIRegistry.java:76) ~[shardingsphere-spi-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory.createAlgorithm(ShardingSphereAlgorithmFactory.java:41) ~[shardingsphere-infra-common-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.sharding.rule.ShardingRule.lambda$new$0(ShardingRule.java:91) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_312]
at org.apache.shardingsphere.sharding.rule.ShardingRule.<init>(ShardingRule.java:91) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.sharding.rule.builder.ShardingRuleBuilder.build(ShardingRuleBuilder.java:41) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.sharding.rule.builder.ShardingRuleBuilder.build(ShardingRuleBuilder.java:35) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder.buildRules(SchemaRulesBuilder.java:85) ~[shardingsphere-infra-common-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.infra.rule.builder.schema.SchemaRulesBuilder.buildRules(SchemaRulesBuilder.java:70) ~[shardingsphere-infra-common-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.mode.manager.memory.MemoryContextManagerBuilder.build(MemoryContextManagerBuilder.java:53) ~[shardingsphere-memory-mode-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource.createContextManager(ShardingSphereDataSource.java:75) ~[shardingsphere-jdbc-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource.<init>(ShardingSphereDataSource.java:61) ~[shardingsphere-jdbc-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory.createDataSource(ShardingSphereDataSourceFactory.java:79) ~[shardingsphere-jdbc-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory.createDataSource(ShardingSphereDataSourceFactory.java:94) ~[shardingsphere-jdbc-core-5.0.0.jar:5.0.0]
at com.sg8000.config.LocalShardingDatabasesAndTablesConfiguration.getDataSource(LocalShardingDatabasesAndTablesConfiguration.java:27) ~[classes/:na]
at com.sg8000.config.LocalShardingDatabasesAndTablesConfiguration$$EnhancerBySpringCGLIB$$3edabb14.CGLIB$getDataSource$0(<generated>) ~[classes/:na]
at com.sg8000.config.LocalShardingDatabasesAndTablesConfiguration$$EnhancerBySpringCGLIB$$3edabb14$$FastClassBySpringCGLIB$$a0d400d1.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.14.jar:5.3.14]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.14.jar:5.3.14]
at com.sg8000.config.LocalShardingDatabasesAndTablesConfiguration$$EnhancerBySpringCGLIB$$3edabb14.getDataSource(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_312]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_312]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_312]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_312]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.14.jar:5.3.14]
... 83 common frames omitted
进程已结束,退出代码0