水平分库配置文件

# sharding-jdbc 水平分库分表策略
# 配置数据源,给数据源起别名
# 水平分库需要配置多个数据库
spring.shardingsphere.datasource.names=m1,f1,f2,f3,f4,f5
spring.shardingsphere.sharding.binding-tables=flight_record

# 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true

## 配置第一个数据源的具体内容,包含连接池,驱动,地址,用户名,密码 主数据源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://基础库:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456

# 配置第二个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
spring.shardingsphere.datasource.f1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.f1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.f1.url=jdbc:mysql://数据库地址:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.f1.username=root
spring.shardingsphere.datasource.f1.password=123456

# 配置第三个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
spring.shardingsphere.datasource.f2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.f2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.f2.url=jdbc:mysql://数据库地址:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.f2.username=root
spring.shardingsphere.datasource.f2.password=123456

# 配置第三个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
spring.shardingsphere.datasource.f3.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.f3.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.f3.url=jdbc:mysql://数据库地址:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.f3.username=root
spring.shardingsphere.datasource.f3.password=123456

# 配置第四个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
spring.shardingsphere.datasource.f4.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.f4.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.f4.url=jdbc:mysql://数据库地址:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.f4.username=root
spring.shardingsphere.datasource.f4.password=123456

# 配置第五个数据源的具体内容,包含连接池,驱动,地址,用户名,密码
spring.shardingsphere.datasource.f5.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.f5.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.f5.url=jdbc:mysql://数据库地址:3306/库名?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
spring.shardingsphere.datasource.f5.username=root
spring.shardingsphere.datasource.f5.password=123456


# 指定数据库分布的情况和数据表分布的情况
# m1 m2   flight_record  flight_record
spring.shardingsphere.sharding.tables.flight_record.actual-data-nodes=f$->{1..5}.flight_record

# 指定 flight_record 表里面主键 id 的生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.flight_record.key-generator.column=id
spring.shardingsphere.sharding.tables.flight_record.key-generator.type=SNOWFLAKE

# 指定分库策略    约定 analyse_id 值偶数添加到 m1 库,如果 tenant_id 是奇数添加到 m2 库
# 默认写法(所有的表的tenant_id)
spring.shardingsphere.sharding.tables.flight_record.database-strategy.inline.sharding-column=analyse_id
spring.shardingsphere.sharding.tables.flight_record.database-strategy.inline.algorithm-expression=f$->{analyse_id % 5 + 1}

#default-data-source-name: m1 #对于其他业务表设置默认数据源
spring.shardingsphere.sharding.default-data-source-name=m1

# 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值