SpringBoot 集成 ShardingSphere-jdbc

cccs7 Lv5

SpringBoot 集成 ShardingSphere-jdbc

在 SpringBoot 2.7.7 中配置使用 ShardingSphere


引入 maven 依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.0.0-alpha</version>
</dependency>

数据源中的 common 必须存在

参考官方的文档:https://shardingsphere.apache.org/document/5.0.0-alpha/cn/user-manual/shardingsphere-jdbc/configuration/spring-boot-starter/

1
2
3
4
5
6
7
8
9
spring.shardingsphere.datasource.names= # 数据源名称,多数据源以逗号分隔

spring.shardingsphere.datasource.common.type= # 数据库连接池类名称
spring.shardingsphere.datasource.common.driver-class-name= # 数据库驱动类名
spring.shardingsphere.datasource.common.username= # 数据库用户名
spring.shardingsphere.datasource.common.password= # 数据库密码
spring.shardingsphere.datasource.common.xxx= # 数据库连接池的其它属性

spring.shardingsphere.datasource.<datasource-name>.url= # 数据库 URL 连接

可通过以下配置覆盖

1
2
3
4
5
6
spring.shardingsphere.datasource.<datasource-name>.url= # 数据库 URL 连接
spring.shardingsphere.datasource.<datasource-name>.type= # 数据库连接池类名称,覆盖 common 中的 type 配置
spring.shardingsphere.datasource.<datasource-name>.driver-class-name= # 数据库驱动类名,覆盖 common 中的 driver-class-name 配置
spring.shardingsphere.datasource.<datasource-name>.username= # 数据库用户名 ,覆盖 common 中的 username 配置
spring.shardingsphere.datasource.<datasource-name>.password= # 数据库密码 ,覆盖 common 中的 password 配置
spring.shardingsphere.datasource.<data-source-name>.xxx= # 数据库连接池的其它属性 ,覆盖 common 中其他属性配置

再配置这个数据配置的时候,误认为后续带数据源的配置可以覆盖comm的公共配置,那么公共配置不配置也可以。 公共配置中的属性,在某种情况下是存在不一致的,比如两个数据库的连接池类型喝驱动不同。我的配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring.main.allow-bean-definition-overriding=true

spring.shardingsphere.props.sql-show=true
spring.shardingsphere.datasource.names="write-ds,read-ds"
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.write-ds.jdbc-url=jdbc:mysql://192.168.21.135/eatmore?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.write-ds.username=root
spring.shardingsphere.datasource.write-ds.password=C020611.
spring.shardingsphere.datasource.read-ds.jdbc-url=jdbc:mysql://192.168.21.136/eatmore?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.read-ds.username=root
spring.shardingsphere.datasource.read-ds.password=C020611.
spring.shardingsphere.rules.replica-query.data-sources.db0.primary-data-source-name=write-ds
spring.shardingsphere.rules.replica-query.data-sources.db0.replica-data-source-names=read-ds
spring.shardingsphere.rules.replica-query.load-balancers.db0.type=ROUND_ROBIN
spring.shardingsphere.rules.replica-query.load-balancers.db0.props.test=test

注意: datasource 命名不可以有下划线

  • Title: SpringBoot 集成 ShardingSphere-jdbc
  • Author: cccs7
  • Created at: 2023-04-01 20:12:22
  • Updated at: 2023-06-29 23:13:56
  • Link: https://blog.cccs7.icu/2023/04/01/SpringBoot-集成-ShardingSphere-jdbc/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
SpringBoot 集成 ShardingSphere-jdbc