4.x版本配置
maven依赖
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
application配置
spring:
shardingsphere:
datasource:
names: master,slave,master-log,slave-log
master:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.100.100:3306/db_user
username: root
password: 123456
slave:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.100.100:3301/db_user
username: root
password: 123456
master-log:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.100.100:3310/db_order
username: root
password: 123456
slave-log:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.100.100:3311/db_order
username: root
password: 123456
props:
sql:
show: true
# 切片 + 主从
sharding:
# 主从分离 主从 + 双数据源
master-slave-rules:
user:
master-data-source-name: master
slave-data-source-names: slave
log:
master-data-source-name: master-log
slave-data-source-names: slave-log
# 表分片
tables:
t_order:
actual-data-nodes: log.t_order$->{0..1}
table-strategy:
inline:
sharding-column: id
algorithm-expression: t_order$->{id % 2}
# 默认数据源
default-data-source-name: user
报错注意事项
JDK版本过高,切换 JDK1.8,或者在启动参数上添加
--add-opens=java.base/java.lang=ALL-UNNAMED
就可以了
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.lang.Number.serialVersionUID accessible: module java.base does not "opens java.lang" to unnamed module @1324409e
### The error may exist in fun/psgame/demo/mapper/DeviceMapper.java (best guess)
### The error may involve fun.psgame.demo.mapper.DeviceMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO tb_device ( device_id, device_type ) VALUES ( ?, ? )
### Cause: java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.lang.Number.serialVersionUID accessible: module java.base does not "opens java.lang" to unnamed module @1324409e
标签:jdbc,java,log,数据源,mysql,JDBC,master,shardingSphere,slave
From: https://blog.51cto.com/learningfish/9017172