配置如下
spring.shardingsphere.datasource.names=ds-0
spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/smiler_user?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=123456
#指定表smiler_user的分布情况,配置表在哪个数据库里面,表名称都是什么
spring.shardingsphere.rules.sharding.tables.smiler_user.actual-data-nodes=ds-0.smiler_user_$->{1..2}
# 指定分片策略 约定id值偶数添加到表smiler_user_1,如果id是奇数添加到表smiler_user_2
spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-column=id
spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-algorithm-name=smiler_user-inline
spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.props.algorithm-expression=smiler_user_$->{id.remainder(2).add(1)}
异常如下
Caused by: java.lang.NullPointerException: Inline sharding algorithm expression cannot be null.
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:897) ~[guava-29.0-jre.jar:na]
at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.getAlgorithmExpression(InlineShardingAlgorithm.java:58) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
at org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm.init(InlineShardingAlgorithm.java:52) ~[shardingsphere-sharding-core-5.0.0.jar:5.0.0]
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]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1807) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
... 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
3、通过配置文件可知,明明配置了 spring.shardingsphere.rules.sharding.sharding-algorithms.smiler_user-inline.props.algorithm-expression,为何这里找不到呢?
原因就在于 方法 PropertyUtil._containPropertyPrefix 无法识别 _smiler_user-inline 这种带下划线的字符。
将 smiler_user-inline 替换成 smiler-user-inline 即可
神坑~~