diff --git a/pom.xml b/pom.xml index 6a249b6..3af661b 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 0.11.5 2.3.1 1.8.4 - 5.2.1 + 5.5.1 3.19.1 3.0.0-M1 2.5.0 @@ -127,7 +127,7 @@ org.apache.shardingsphere - shardingsphere-jdbc-core-spring-boot-starter + shardingsphere-jdbc ${shardingsphere-jdbc.version} diff --git a/src/main/java/io/github/xxyopen/novel/core/config/ShardingSphereConfiguration.java b/src/main/java/io/github/xxyopen/novel/core/config/ShardingSphereConfiguration.java index ec465ef..dd35674 100644 --- a/src/main/java/io/github/xxyopen/novel/core/config/ShardingSphereConfiguration.java +++ b/src/main/java/io/github/xxyopen/novel/core/config/ShardingSphereConfiguration.java @@ -1,9 +1,16 @@ package io.github.xxyopen.novel.core.config; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory; +import org.apache.shardingsphere.infra.url.core.ShardingSphereURL; +import org.apache.shardingsphere.infra.url.core.ShardingSphereURLLoadEngine; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import javax.sql.DataSource; + /** * ShardingSphere 配置类,控制是否开启 ShardingSphere * @@ -11,14 +18,23 @@ import org.springframework.context.annotation.Configuration; * @date 2023/12/21 */ @Configuration -@EnableAutoConfiguration(exclude = { - org.apache.shardingsphere.spring.boot.ShardingSphereAutoConfiguration.class -}) @ConditionalOnProperty( prefix = "spring.shardingsphere", name = {"enabled"}, - havingValue = "false" + havingValue = "true" ) +@Slf4j public class ShardingSphereConfiguration { + private static final String URL = "classpath:shardingsphere-jdbc.yml"; + + @Bean + @SneakyThrows + public DataSource shardingSphereDataSource() { + log.info(">>>>>>>>>>> shardingSphereDataSource init."); + ShardingSphereURLLoadEngine urlLoadEngine = new ShardingSphereURLLoadEngine( + ShardingSphereURL.parse(URL)); + return YamlShardingSphereDataSourceFactory.createDataSource(urlLoadEngine.loadContent()); + } + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index aea10e4..2d4c302 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -20,9 +20,16 @@ spring: threads: virtual: enabled: true - # initialize the schema history table + flyway: + # 是否开启 Flyway + enabled: false + # initialize the schema history table baseline-on-migrate: true +# url: jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai +# user: root +# password: test123456 + server: # 端口号 port: 8888 @@ -39,54 +46,6 @@ spring: shardingsphere: # 是否开启分库分表 enabled: false - props: - # 是否在日志中打印 SQL - sql-show: true - # 模式配置 - mode: - # 单机模式 - type: Standalone - # 元数据持久化 - repository: - # 数据库持久化 - type: JDBC - props: - # 元数据存储类型 - provider: H2 - jdbc_url: jdbc:h2:./.h2/shardingsphere - - # 数据源配置 - datasource: - names: ds_0 - ds_0: - type: com.zaxxer.hikari.HikariDataSource - driverClassName: com.mysql.cj.jdbc.Driver - jdbcUrl: jdbc:mysql://localhost:3306/novel_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai - username: root - password: test123456 - # 规则配置 - rules: - # 数据分片 - sharding: - tables: - # book_content 表 - book_content: - # 数据节点 - actual-data-nodes: ds_$->{0}.book_content$->{0..9} - # 分表策略 - table-strategy: - standard: - # 分片列名称 - sharding-column: chapter_id - # 分片算法名称 - sharding-algorithm-name: bookContentSharding - sharding-algorithms: - bookContentSharding: - # 行表达式分片算法,使用 Groovy 的表达式,提供对 SQL 语句中的 = 和 IN 的分片操作支持 - type: INLINE - props: - # 分片算法的行表达式 - algorithm-expression: book_content$->{chapter_id % 10} --- #---------------------中间件配置--------------------------- spring: diff --git a/src/main/resources/shardingsphere-jdbc.yml b/src/main/resources/shardingsphere-jdbc.yml new file mode 100644 index 0000000..cc9a1e3 --- /dev/null +++ b/src/main/resources/shardingsphere-jdbc.yml @@ -0,0 +1,55 @@ +mode: + # 单机模式 + type: Standalone + # 元数据持久化 + repository: + # 数据库持久化 + type: JDBC + props: + # 元数据存储类型 + provider: H2 + jdbc_url: jdbc:h2:./.h2/shardingsphere + +# 数据源配置 +dataSources: + ds_1: + dataSourceClassName: com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.cj.jdbc.Driver + jdbcUrl: jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai + username: root + password: test123456 + # 配置其他数据源 + +# 规则配置 +rules: + # 配置单表规则 + - !SINGLE + tables: + - "*.*" + # 配置分片规则 + - !SHARDING + tables: # 数据分片规则配置 + book_content: + # 分库策略,缺省表示使用默认分库策略 + actualDataNodes: ds_${1}.book_content${0..9} + # 分表策略 + tableStrategy: + standard: + # 分片列名称 + shardingColumn: chapter_id + # 分片算法名称 + shardingAlgorithmName: bookContentSharding + + shardingAlgorithms: + bookContentSharding: + # 行表达式分片算法,使用 Groovy 的表达式,提供对 SQL 语句中的 = 和 IN 的分片操作支持 + type: INLINE + props: + # 分片算法的行表达式 + algorithm-expression: book_content${chapter_id % 10} + + + +props: + # 是否在日志中打印 SQL + sql-show: true