官网地址:http://shardingsphere.apache.org/index_zh.html
Maven配置:
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
SpringBoot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
yml配置:
注意 此处是jdc-url 不是url
sharding:
jdbc:
datasource:
names: message0,message1
# 数据源ds0
message0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.10.10.201:3306/message0?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: momcome
password: momcome
# 数据源ds1
message1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.10.10.201:3306/message1?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
username: momcome
password: momcome
config:
sharding:
props:
sql.show: true
tables:
yun_xin_all_msg_t: #t_user表
key-generator-column-name: id #主键
actual-data-nodes: message$->{0..1}.yun_xin_all_msg_t_$->{0..2} #数据节点,均匀分布
database-strategy: #分库策略
inline: #行表达式
sharding-column: id #列名称,多个列以逗号分隔
algorithm-expression: message$->{id % 2} #按模运算分配
table-strategy: #分表策略
inline: #行表达式
sharding-column: id
algorithm-expression: yun_xin_all_msg_t_$->{id % 3}
运行成功截图: