Springboot 集成 dynamic-datasource-spring-boot-starter 和 Sharding-jdbc 5.x,实现项目中原有的数据源作为主数据源,并保证分库分表有效

为了解决这个问题,你需要做以下几步:

  1. 确保你的项目中已经正确添加了dynamic-datasource-spring-boot-starterSharding-jdbc的依赖。
  2. 配置dynamic-datasource-spring-boot-starter。在application.ymlapplication.properties中,你需要定义多个数据源,并指定它们作为动态数据源。
  3. 配置Sharding-jdbc。你需要定义数据分片的规则,并指定分片键。
  4. 确保Sharding-jdbc的数据源名称与dynamic-datasource-spring-boot-starter配置的动态数据源名称相匹配。

以下是一个简化的示例配置:




spring:
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        url: jdbc:mysql://localhost:3306/ds0
        username: root
        password: 
        type: com.zaxxer.hikari.HikariDataSource
      ds1:
        url: jdbc:mysql://localhost:3306/ds1
        username: root
        password: 
        type: com.zaxxer.hikari.HikariDataSource
    sharding:
      tables:
        t_order:
          actualDataNodes: ds${0..1}.t_order_${0..1}
          databaseStrategy:
            standard:
              shardingColumn: user_id
              shardingAlgorithmName: database_inline
          tableStrategy:
            standard:
              shardingColumn: order_id
              shardingAlgorithmName: table_inline
          keyGenerateStrategy:
            column: order_id
            keyGeneratorName: snowflake
      shardingAlgorithms:
        database_inline:
          type: INLINE
          props:
            algorithm-expression: ds${user_id % 2}
        table_inline:
          type: INLINE
          props:
            algorithm-expression: t_order_${order_id % 2}
      keyGenerators:
        snowflake:
          type: SNOWFLAKE
 
dynamic:
  datasource:
    primary: ds0
    strict: false
    dynamic-datasource:
      ds0:
        url: jdbc:mysql://localhost:3306/ds0
        username: root
        password: 
        driver-class-name: com.mysql.cj.jdbc.Driver
      ds1:
        url: jdbc:mysql://localhost:3306/ds1
        username: root
        password: 
        driver-class-name: com.mysql.cj.jdbc.Driver

请注意,上述配置可能需要根据你的实际数据库信息进行调整,并且需要确保所有的数据库、表和分片键都已经存在。

在代码中,你可以使用@DS注解来指定使用哪个动态数据源,或者通过编程的方式动态切换数据源。

确保你的项目中包含了所有必要的配置类和注解,以便dynamic-datasource-spring-boot-starterSharding-jdbc能够正确地工作。如果你遇到任何具体的错误信息,请提供详细的异常堆栈跟踪以便进一步的分析和解决。

最后修改于:2024年09月05日 09:22

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日