【教程】shardingjdbc(五)默认策略配置

本文介绍了在ShardingJDBC中,当未指定数据源的表如user没有配置分片策略时,数据源的选择逻辑。默认情况下,ShardingJDBC会根据数据源中是否存在表来决定插入或查询操作,优先选择存在的表。如果配置了默认策略,包括defaultDatabaseStrategy、defaultTableStrategy、defaultKeyGenerateStrategy和defaultShardingColumn,可以定义默认的分片行为。4.x版本引入了默认数据源配置,但在5.x版本中已被移除。
摘要由CSDN通过智能技术生成

未指定数据源的表数据源如何选择的?

假设又有一个user表;对于user表未配置分片策略;

主要的配置信息为:

1. 2个数据源

2. 只对order 进行分表;

spring:
  application:
    name: shardingjdbcDemo
  main:
    allow-bean-definition-overriding: true

  shardingsphere:
    #数据源信息
    datasource:
      #名称为dbsource-0的数据源
      dbsource-0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
        username: root
        password: 123456
      #名称为dbsource-1的数据源
      dbsource-1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/db2?serverTimezone=Asia/Shanghai&
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ShardingSphere是一款开源的分布式数据库中间件,其中的Sharding-JDBC模块可以实现对关系型数据库进行分库分表,具有高性能、易扩展等优点。 以下是Sharding-JDBC配置示例: 1. 引入依赖 ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${shardingsphere.version}</version> </dependency> ``` 2. 配置数据源 ```yaml spring: datasource: # 默认数据源 primary: url: jdbc:mysql://localhost:3306/test_primary?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver # 分片数据源1 sharding1: url: jdbc:mysql://localhost:3306/test_sharding1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver # 分片数据源2 sharding2: url: jdbc:mysql://localhost:3306/test_sharding2?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver ``` 3. 配置分片规则 ```yaml spring: shardingsphere: datasource: names: primary, sharding1, sharding2 # 配置默认数据源 default-data-source-name: primary rules: # 配置表规则 - !SHARDING tables: user: actual-data-nodes: sharding${0..1}.user${0..1} table-strategy: inline: sharding-column: id algorithm-expression: user${id % 2} key-generator: column: id type: SNOWFLAKE # 配置分库策略 sharding-algorithms: - type: INLINE props: algorithm-expression: sharding${user_id % 2} # 配置分表策略 table-strategy: inline: sharding-column: user_id algorithm-expression: user${user_id % 2} ``` 在上述配置中,我们定义了三个数据源:一个主数据源和两个分片数据源。然后我们配置了一个分片规则,对user表进行水平分库分表,分为两个库、两个表,以id和user_id字段作为分片键。其中,id是用于分库的字段,user_id是用于分表的字段。 最后,我们配置了分库和分表的策略,这里我们使用了inline算法,通过id和user_id的模运算来决定数据分片的位置。 以上就是Sharding-JDBC的基本配置,更多详细的配置可以参考官方文档:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-spring-boot/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值