iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >【shardingsphere 5.x版本使用】,精准分表自定义策略配置,单库分表
  • 681
分享到

【shardingsphere 5.x版本使用】,精准分表自定义策略配置,单库分表

javamysqlshardingsphere 2023-08-21 10:08:07 681人浏览 八月长安
摘要

之前有发过4.x版本的使用教程,这次项目升级了5.x版本,教程实战贴一下 1. 首先是Maven依赖的添加 org.apache.shardingsphere s

之前有发过4.x版本的使用教程,这次项目升级了5.x版本,教程实战贴一下

1. 首先是Maven依赖的添加

                <dependency>            <groupId>org.apache.shardingspheregroupId>            <artifactId>shardingsphere-jdbc-core-spring-boot-starterartifactId>            <version>5.1.2version>        dependency>

2. yaml的配置,只配置一个数据库,单库分表,使用精准自定义分片策略

1.2版本增加一个参数props,将分片策略和全限定类名配置在这个下面。
而且精准分片的接口StandardShardingAlGorithm实现类需要实现的方法也多了两个,getProps和

    @Override    public String doSharding(Collection<T> tableNames, PreciseShardingValue<T> preciseShardingValue) {        return null;    }    @Override    public Collection<String> doSharding(Collection<T> collection, RangeShardingValue<T> rangeShardingValue) {        return null;    }    @Override    public String getType() {        return null;    }// 新增方法,暂不知用处    @Override    public Properties getProps() {        return null;    }        // 新增方法 删除了原init();方法,多了个参数properties    @Override    public void init(Properties properties) {    }
spring:  main:    allow-bean-definition-overriding: true  shardingsphere:    # 是否启用 Sharding    enabled: true    # 打印sql    props:      sql-show: true    datasource:      names: master      master:        type: com.alibaba.druid.pool.DruidDataSource        driver-class-name: com.Mysql.cj.jdbc.Driver        url: jdbc:mysql://localhost:3306/**?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8        username: root        passWord: root    rules:      sharding:        # 表策略配置        tables:          # terminal_heartbeat 是逻辑表          terminal_heartbeat:            actualDatanodes: master.**            tableStrategy:              # 使用标准分片策略              standard:                # 配置分片字段                shardinGColumn: terminal_code                # 分片算法名称,不支持大写字母和下划线,否则启动就会报错                shardingAlgorithmName: time-sharding-altorithm        # 分片算法配置        shardingAlgorithms:          # 分片算法名称,不支持大写字母和下划线,否则启动就会报错          time-sharding-altorithm:            # 类型:自定义策略            type: CLASS_BASED            props:              # 分片策略              strategy: standard              # 分片算法类              algorithmClassName: com.****.ShardingAlgorithmA

3. 踩坑合集:

  1. ShardingJDBC 5.1.0使用druid连接池需要加dbcp依赖
<dependency>    <groupId>org.apache.TomcatgroupId>    <artifactId>tomcat-dbcpartifactId>    <version>10.0.16version>dependency>
  1. 与flyway一起使用时会报错,因为flyway会查询 infORMation_schema.schemata 用户权限和数据库集合等信息,而ShardingJDBC暂未支持
    官方回复5.1.1已支持pr地址https://GitHub.com/apache/shardingsphere/issues/16234

但是我已尝试还是无效,会报错org.apache.shardingsphere.infra.exception.SchemaNotExistedException: Schema ‘information_schema’ 不存在

解决方法:使用Sharding-proxy,或者禁用flyway

来源地址:https://blog.csdn.net/zzztimes/article/details/130007248

您可能感兴趣的文档:

--结束END--

本文标题: 【shardingsphere 5.x版本使用】,精准分表自定义策略配置,单库分表

本文链接: https://www.lsjlt.com/news/376844.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作