概述

本文章主要是用SpringBoot 3.3.4 集成 ShardingSphere‐JDBC 5.5.2 并且搭配Dynamic使用的一个应用场景,为什么用了分库分表了还会用到多数据源呢?我们在做一些监控系统、集成系统、工具系统、统计系统等需要去连接多个项目数据库的时候,每个项目都有自己的数据库,而且部分系统又刚好用到了分库分表,那如果我们查询数据是用程序逻辑去适应分库分表的查询是相当的麻烦,这个时候当前应用场景就相当有意义了。之前我发过一篇单独集成shardingsphere-jdbc的文章,有需要的可以移步:Spring Boot 集成 ShardingSphere-JDBC 配置示例

代码

说明

官网地址:http://shardingsphere.apache.org/index_zh.html

shardingsphere主流为YMAL配置和JAVA API配置,本文用的YMAL配置,如果需要了解分库分表的详细逻辑和深入了解功能,可以去官网。

引入maven

        <!--    多数据源    -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency> <!-- shardingsphere -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<version>5.5.2</version>
</dependency>

application.yaml配置

server:
port: 8095
spring:
datasource:
dynamic:
primary: master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/secure?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
sharding:
driverClassName: org.apache.shardingsphere.driver.ShardingSphereDriver
url: jdbc:shardingsphere:classpath:sharding-dev.yaml
other:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/system?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# 其他配置省略

sharding-dev.yaml配置

这里只是做了一个分表的基本配置,一个info主表关联info_audio、info_audit关联表,algorithmClassName的地址是自己分表的自定义算法地址。如果还需要分库分表其他的功能,也只需要在这个配置文件补充就可以了。

dataSources:
center:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.3.192.20:3306/center?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: xhl2019cn rules:
- !SHARDING
tables:
# 主表特殊配置 (使用id分片)
info:
actualDataNodes: center.info_${0..9}
tableStrategy:
standard:
shardingColumn: id # 特殊分片键
shardingAlgorithmName: newsInfoShardingAlgorithm # 其他表使用默认策略 (news_id分片)
info_audio:
actualDataNodes: center.info_audio_${0..9}
info_audit:
actualDataNodes: center.info_audit_${0..9} # 绑定表组配置
bindingTables:
- info,info_audio,info_audit # 默认分表策略 (适用于所有表)
defaultTableStrategy:
standard:
shardingColumn: news_id # 默认分片键
shardingAlgorithmName: newsInfoShardingAlgorithm # 默认主键生成策略
defaultKeyGenerateStrategy:
column: id
keyGeneratorName: snowflake # 分片算法配置
shardingAlgorithms:
newsInfoShardingAlgorithm:
type: CLASS_BASED
props:
strategy: STANDARD
algorithmClassName: com.monitor.config.sharding.InfoSharding
newsInfoShardingTableNum: 10 # 分布式序列配置
keyGenerators:
snowflake:
type: SNOWFLAKE

使用

我们配置完成后,就可以正常使用dynamic一样去切换数据库了,在mapper文件使用注解@DS("sharding"),就会使用分表的逻辑去查询数据了。

SpringBoot集成ShardingSphere‐JDBC搭配Dynamic多数据源的更多相关文章

  1. SpringBoot 集成Spring JDBC

    (1)在pom.xml中添加依赖 <!--spring-jdbc的依赖--> <dependency> <groupId>org.springframework.b ...

  2. SpringBoot 集成mongodb(1)单数据源配置

    新项目要用到mongodb,于是在个人电脑上的虚拟环境linux上安装了下mongodb,练习熟悉下. 1.虚拟机上启动mongodb. 首先查看虚拟机ip地址,忘了哈~~ 命令行>ifconf ...

  3. SpringBoot 集成mongodb(2)多数据源配置

    github:https://github.com/xiaozhuanfeng/mongoProj 现MongoDB有两个数据库: pom.xml: <!-- mongodb 配置 --> ...

  4. SpringBoot集成Mybatis配置动态数据源

    很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...

  5. springboot集成activiti6.0多数据源的配置

    最近公司开始开发springboot的项目,需要对工作流进行集成.目前activiti已经发布了7.0的版本,但是考虑到6.0版本还是比较新而且稳定的,决定还是选择activiti6.0的版本进行集成 ...

  6. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  7. SpringBoot+Dynamic多数据源动态切换

    最近做了个小模块,需求就是项目同时读取三个数据库,操作数据.并不是分库分表,只用定时跑,不需要对外提供接口. 技术选型:SpringBoot + Mybatis Plus(Mybatis) + Dyn ...

  8. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  9. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  10. SpringBoot Druid整合,SpringBoot 集成Druid

    SpringBoot Druid整合,SpringBoot 集成Druid ================================ ©Copyright 蕃薯耀 2018年4月8日 http ...

随机推荐

  1. md语法练手随笔

    代码 单行代码:代码之间分别用一个反引号包起来 代码块: 代码之间分别用三个反引号包起来, 且两边的反引号单独占一行 这是一级标题 这是六级标题 这是加粗的文字 这是倾斜的文字 这是斜体加粗的文字 这 ...

  2. C# 多项目打包时如何将项目引用转为包依赖

    项目背景 最近开发一组类库,大约会有五六个项目.一个Core,加上若干面向不同产品的实现库,A/B/C/D...它们都依赖Core. 首先,我想统一版本号,这个容易,通过Directory.Build ...

  3. Ruby+Appium+testunit实现app自动化demo

    1.安装对应库 gem install test-unit gem install appium_lib 2.编写代码 代码如下: require 'appium_lib' require 'test ...

  4. Semaphore.acquire()方法的底层原理

    一.acquire() 的工作流程 当调用 acquire() 方法时,实际调用的是 AQS 的 acquireSharedInterruptibly(1) 方法.以下是其详细工作流程: // acq ...

  5. LinkedBlockingQueue的put方法底层源码

    一.LinkedBlockingQueue的put方法底层源码 LinkedBlockingQueue 的 put 方法是其核心方法之一,用于将元素插入队列.如果队列已满,调用 put 方法的线程会被 ...

  6. @Scheduled 定时任务自定义

    简介 @Scheduled 定时任务自定义可以通过SchedulingConfigurer实现. SchedulingConfigurer 是 Spring Framework 中的一个接口,用于配置 ...

  7. leetcode001 两数之和

    问题描述:两数之和 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答 ...

  8. MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题

    MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题 问题背景 在停服发版更新时,需对 200GB 大表(约 200 亿行数据)进行快速备份以预防操作失误. 因为曾经出现过有开发写的发 ...

  9. MySQL 的索引类型有哪些?

    MySQL 的索引类型 MySQL 提供多种索引类型,用于优化数据查询性能.每种索引类型在存储结构.适用场景和性能特性方面各不相同. 1. 常见的索引类型 (1)B+树索引 结构:基于 B+ 树实现, ...

  10. jmeter操作数据库增删改查的注意事项

    一,场景 1.在jmeter造数据后,可通过数据库查询数据库是否新增数据,判断脚本执行是否成功. 2.有些数据新增不可重复,因此脚本执行后需要将新增的数据删除,才能再次执行脚本. 二.连接数据库 在通 ...