栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 整合Shardingsphere 实现读写分离

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 整合Shardingsphere 实现读写分离

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

1.maven 依赖


           org.apache.shardingsphere
          >sharding-jdbc-spring-boot-starter
          4.1.1
 

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 等。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020986.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号