pigx集成sharding jdbc
pom依赖:
<properties> <sharding-sphere.version>4.1.1</sharding-sphere.version> </properties> <dependencies> <!-- for spring boot --> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${sharding-sphere.version}</version> </dependency> </dependencies>
nacos:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/testdb?characterEncoding=utf8
改为如下分片配置:
# 数据源
spring:
shardingsphere:
datasource:
names: ds0, ds1, defaultds
defaultds:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://pigx-mysql:3306/testdb
username: root
password: root
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://pigx-mysql:3306/testdb_0
username: root
password: root
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://pigx-mysql:3306/testdb_1
username: root
password: root
sharding:
tables:
test_user:
actualDataNodes: ds${0..1}.test_user
databaseStrategy:
inline:
shardingColumn: id
algorithmExpression: ds${id % 2}
tableStrategy:
inline:
shardingColumn: id
algorithmExpression: test_user
keyGenerator:
type: SNOWFLAKE
column: id
defaultDataSourceName: defaultds
defaultTableStrategy:
none:
defaultKeyGenerator:
type: SNOWFLAKE
column: xxxx_replace_must
props:
sql.show: true
在启动类上把原本Druid的Datasource去掉,不要autoconfigure
@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class})
自省推动进步,视野决定未来。
心怀远大理想。
为了家庭幸福而努力。
商业合作请看此处:https://www.magicube.ai
心怀远大理想。
为了家庭幸福而努力。
商业合作请看此处:https://www.magicube.ai