配置如下

  1. spring.shardingsphere.datasource.names=ds-0
  2. spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/smiler_user?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  3. spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
  4. spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.ds-0.username=root
  6. spring.shardingsphere.datasource.ds-0.password=123456
  7. #指定表smiler_user的分布情况,配置表在哪个数据库里面,表名称都是什么
  8. spring.shardingsphere.rules.sharding.tables.smiler_user.actual-data-nodes=ds-0.smiler_user_$->{1..2}
  9. # 指定分片策略 约定id值偶数添加到表smiler_user_1,如果id是奇数添加到表smiler_user_2
  10. spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-column=id
  11. spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-algorithm-name=smiler_user-inline
  12. spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.type=INLINE
  13. spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.props.algorithm-expression=smiler_user_$->{id.remainder(2).add(1)}

异常如下

  1. Caused by: java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
  2. at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897) ~[guava-29.0-jre.jar:na]
  3. at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.getAlgorithmExpression(InlineShardingAlgorithm.java:58) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
  4. at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.init(InlineShardingAlgorithm.java:52) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
  5. at org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry.postProcessAfterInitialization(AbstractAlgorithmProvidedBeanRegistry.java:98) ~[shardingsphere-jdbc-spring-boot-starter-infra-5.0.0.jar:5.0.0]
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1807) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
  9. ... 107 common frames omitted

原因分析

1、上述异常是由于配置 spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.props.algorithm-expression 没有获取到
2、关注如下方法
org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry#registerBean
image.png
3、通过配置文件可知,明明配置了 spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.props.algorithm-expression,为何这里找不到呢?

原因就在于 方法 PropertyUtil._containPropertyPrefix 无法识别 _smiler_user-inline 这种带下划线的字符。
将 smiler_user-inline 替换成 smiler-user-inline 即可

神坑~~