Sharding-JDBC5.1.0的springboot数据源配置跟之前版本不一样?

这个配置在5.0.0及一下版本都可以

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: jdbc:mysql://121.196.123.79:3306/sharding_master?useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
        username: root
        password: Root@123
      ds_1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://121.196.123.79:3306/sharding_slave0?useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
        username: root
        password: Root@123

但是5.1.0就不行了?

SpringBoot启动的错误信息如下:

Description:

Configuration property name 'spring.shardingsphere.datasource.ds_0' is not valid:

    Invalid characters: '_'
    Bean: org.apache.shardingsphere.spring.boot.ShardingSphereAutoConfiguration
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

Action:

Modify 'spring.shardingsphere.datasource.ds_0' so that it conforms to the canonical names requirements.

Disconnected from the target VM, address: '127.0.0.1:56259', transport: 'socket'

Process finished with exit code 1

你应该暂时尝试放弃"_"号,转而使用类似ds-0ds0的命名方式. 5.1.1的里程碑涉及YAML的Value下划线的PR主要有两个, 一个是 [issue-15259] upgrade snakeyaml due to cve by pjfanning · Pull Request #15260 · apache/shardingsphere (github.com) , 一个是 Compatible with spring boot 2.x by SirMin · Pull Request #14729 · apache/shardingsphere (github.com)

如楼上所说, 5.1.0 还未支持 ds_1 这种格式, 这个问题可在下个版本中兼容. 请先调整为 ds-1ds1

@泠恒谦 @zjcnb
再看下这个自定义算法的配置

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: jdbc:mysql://192.168.74.36:3306/ftms_v2?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
        username: xieyongchao
        password: java@xycoiaSsd(*2
      ds-1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://gz-cdb-fanm6d3b.sql.tencentcdb.com:57569/demo_ds_1?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
        username: root
        password: xieyc5207
    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: standard
                algorithmClassName: com.xieyc.sharding.config.DefaultShardingAlgorithm
          ftmsFlowShardingAlgorithm:
              type: CLASS_BASED
              props:
                strategy: standard
                algorithmClassName: com.xieyc.sharding.config.FtmsFlowShardingAlgorithm

报错信息如下

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]

Disconnected from the target VM, address: '127.0.0.1:62520', transport: 'socket'

CLASS_BASE对应算法的getType返回的是null,而不是一个字符串。

@泠恒谦 我不是配置了type吗

你贴出你的自定义算法类

@泠恒谦 image
这里的type?枚举值有哪些?

这里没问题。问题是你不能通过standard来配置complex

@泠恒谦 那用什么?strategy:complex也不行啊

strategy:complexstrategy:standard对应的自定义分片算法实现的接口不一样的。

@泠恒谦 这是我实现的类源代码

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 "complex";
    }

    @Override
    public void init() {

    }
}

这一处getType()返回null。因为你实际使用的对应SPI是CLASS_BASE,而不是未注册SPI的complex
没需要的话,这个类置为final class。除非你需要动态注入。
大部分处理的细节在ShardingSphere Doc有提及。

@泠恒谦 返回null也不行啊,Doc看几遍了,没找到

参考 apache/shardingsphere: Ecosystem to transform any database into a distributed database system, and enhance it with sharding, elastic scaling, encryption features & more (github.com) 的master分支, 全局搜索org.apache.shardingsphere.example.extension.sharding.algortihm.classbased.fixture.ClassBasedStandardShardingAlgorithmFixture这个类即可.

@泠恒谦 未找到解决方法,按码云的example弄了一遍,也还是不行

There’s a bit of nothingness in the discussion. You mean the mirror image on the gitee? That’s not real-time at all, github’s is the best. Also, if you can provide a demo, it will help to end some of the uncertainty.

京ICP备2021015875号