Springboot 整合Shardingsphere 实现读写分离

Apache ShardingSphere 是一款开源分布式数据库生态项目,旨在碎片化的异构数据库上层构建生态,在最大限度的复用数据库原生存算能力的前提下,进一步提供面向全局的扩展和叠加计算能力。其核心采用可插拔架构,对上以数据库协议及 SQL 方式提供诸多增强功能,包括数据分片、访问路由、数据安全等。
官方文档:https://shardingsphere.apache.org/document/current/cn/quick-start/shardingsphere-jdbc-quick-start/

1.maven 依赖

<dependency>
           <groupId>org.apache.shardingsphere</groupId>
          <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
          <version>4.1.1</version>
 </dependency>

2.yml 配置

spring:
  shardingsphere:
    datasource:
      names: master,slave
      master:
        url: 
        username: 
        password: 
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: org.postgresql.Driver
        initialSize: 5
        maxActive: 100
        connection-timeout: 90000
        #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filters: stat
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
        multiStatementAllow: true
      slave:
        url: 
        username: 
        password: 
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: org.postgresql.Driver
        initialSize: 5
       maxActive: 100
        connection-timeout: 90000
        #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        filters: stat
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
        multiStatementAllow: true
    masterslave:
      load-balance-algorithm-type: round_robin
      name: ms#      master-data-source-name: master
      slave-data-source-names: slave
    props:
      sql:
        show: false
  autoconfigure:
   exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure

3.敲重点
1.配置不同的数据源连接池,配置的参数是不同的,我这个是配置的druid连接池。
2.因为我项目中本身已经引入了druid依赖,所以必须要加exclude,否则启动时会报Bean name重复,导致无法启动
3.shardingsphere 最坑的就是不支持很多原本的SQL语句,比如:case when,union,union all,having 等。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名软件修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值