Sharding-jdbc 5.1.0 自定义分片算法异常

自定义分片算法实现类

public class DefaultShardingAlgorithm implements ComplexKeysShardingAlgorithm<String> {

    @Override
    public Collection<String> doSharding(Collection availableTargetNames, ComplexKeysShardingValue complexKeysShardingValue) {
        List<String> shardingSuffix = new ArrayList<>();
        Collection uidValues = (Collection) complexKeysShardingValue.getColumnNameAndShardingValuesMap().get("user_id");
        for (Object uidValue : uidValues) {
            if (uidValue instanceof Integer) {
                String actualTable = "ds-" + (int) uidValue % 2;
                if (availableTargetNames.contains(actualTable)) {
                    shardingSuffix.add(actualTable);
                }
            }

        }
        return shardingSuffix;
    }

    @Override
    public String getType() {
        return null;
    }

    @Override
    public void init() {

    }
}

SpringBoot Sharding配置

spring:
  shardingsphere:
    props:
      sql:
        show: true
#    schema:
#      name: sharding_db
#    mode:
#      type: Cluster
#      repository:
#        type: ZooKeeper
#        props:
#          namespace: governance_ds
#          server-lists: localhost:2181
#          retryIntervalMilliseconds: 500
#          timeToLiveSeconds: 60
#          maxRetries: 3
#          operationTimeoutMilliseconds: 500
#      overwrite: false
    datasource:
      names: ds-0,ds-1
      ds-0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: 
        username: 
        password: 
      ds-1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: 
        username: 
        password: 
    rules:
      sharding:
        tables:
          ftms_flow:
            actualDataNodes: ds-${0..1}.ftms_flow_$->{[201602,201603,201604,201605,201606,201607,201608,201609,201610,201611,201612,201701,201702,201703,201704,201705,201706,201707,201708,201709,201710,201711,201712,201801,201802,201803,201804,201805,201806,201807,201808,201809,201810,201811,201812,201901,201902,201903,201904,201905,201906,201907,201908,201909,201910,201911,201912,202001,202002,202003,202004,202005,202006,202007,202008,202009,202010,202011,202012,202101,202102,202103,202104,202105,202106,202107,202108,202109,202110,202111,202112,202201,202202,202203,202204,202205,202206,202207,202208,202209,202210,202211,202212]}
            databaseStrategy:
              complex:
                shardingColumns: user_id
                shardingAlgorithmName: defaultAlgorithm
            table-strategy:
              complex:
                shardingColumns: uid,create_date
                shardingAlgorithmName: ftmsFlowShardingAlgorithm
        shardingAlgorithms:
          defaultAlgorithm:
              type: CLASS_BASED
              props:
                strategy: complex
                algorithmClassName: com.xieyc.sharding.config.DefaultShardingAlgorithm
          ftmsFlowShardingAlgorithm:
              type: CLASS_BASED
              props:
                strategy: complex
                algorithmClassName: com.xieyc.sharding.config.FtmsFlowShardingAlgorithm
#        defaultDatabaseStrategy:
#          inline:
#            shardingColumn: user_id
#            algorithmExpression: ds-${user_id % 2}

异常信息:

022-02-28 16:27:13.226 ERROR 1212 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.apache.shardingsphere.spi.exception.ServiceProviderNotFoundException: No implementation class load from SPI `org.apache.shardingsphere.sharding.spi.ShardingAlgorithm` with type `null`.
	at org.apache.shardingsphere.spi.typed.TypedSPIRegistry.getRegisteredService(TypedSPIRegistry.java:76) ~[shardingsphere-spi-5.1.0.jar:5.1.0]
	at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.lambda$registerBean$2(AbstractAlgorithmProvidedBeanRegistry.java:78) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.1.0.jar:5.1.0]
	at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
	at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.registerBean(AbstractAlgorithmProvidedBeanRegistry.java:77) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.1.0.jar:5.1.0]
	at org.apache.shardingsphere.sharding.spring.boot.algorithm.ShardingAlgorithmProvidedBeanRegistry.postProcessBeanDefinitionRegistry(ShardingAlgorithmProvidedBeanRegistry.java:38) ~[shardingsphere-sharding-spring-boot-starter-5.1.0.jar:5.1.0]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:126) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.7.RELEASE.jar:2.2.7.RELEASE]
	at com.xieyc.sharding.ShardingDemoApplication.main(ShardingDemoApplication.java:12) ~[classes/:na]

@xieyc 收到反馈,我会调查这个问题

@xieyc
我使用你的算法和分片配置,项目是可以正常启动并运行的,也能进入自定义算法的调用。
因此无法定位你的问题,如果可以请提供一个能运行的完整 demo 吧。


@RaigorJiang
git仓库地址:
登录 - Gitee.com

算法 key 的名称从驼峰改为短横线连接就可以了:

也推送到了你提供的仓库,可以验证下。

@RaigorJiang 看源码没看到有这个限制。。。 :grinning_face_with_smiling_eyes:

@xieyc FAQ :: ShardingSphere 可以看下这个文档

1 个赞


我是用5.1.1項目正常启动,保存数据的时候报空指针异常

有尝试过别的版本吗? 是只有 5.1.1 有这样的情况吗?

京ICP备2021015875号