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