Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ jobs:
test-hotspot-jdk-ci:
name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
# 集成测试要拉取较大的镜像(nacos/seata/mysql/postgres/consul),冷缓存时多留一些时间。
timeout-minutes: 120
timeout-minutes: 60
strategy:
matrix:
os: [ 'ubuntu-latest' ]
Expand Down Expand Up @@ -44,7 +43,5 @@ jobs:
java-version: ${{ matrix.java-version }}
cache: 'maven'
- name: Build spring-boot-2 samples with Maven
# 主代码保持 Java 8 兼容;测试需要 testcontainers 2.x(加载要求 Java 17+),
# 因此 JDK 8 任务只做编译验证,测试在上面的 JDK 17/21 任务中执行。
run: |
./mvnw -am -pl spring-boot-2/shardingsphere-jdbc-4.x-spring-sample -T1C -B -e clean compile
./mvnw -am -pl spring-boot-2/shardingsphere-jdbc-4.x-spring-sample,spring-boot-2/shardingsphere-jdbc-5.x-spring-sample -T1C -B -e clean test
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
- name-pattern-sample 自定义切面的使用示例
- quartz-sample 多数据源集成quartz示例
- shardingsphere-jdbc-5.x-core-sample 集成 ShardingSphere JDBC Driver 5.5.2 使用示例
- shardingsphere-jdbc-5.x-spring-sample 集成 ShardingSphere JDBC Driver 5.5.2 使用示例,
参考 https://github.com/apache/shardingsphere/issues/22469
- spel-sample 动态从外部参数spel来切换数据源的使用示例
- tx-local-sample 本地事务示例项目★★★★★★必看★★★★★★
- tx-seata-sample 基于seata的分布式事务集成使用示例
Expand All @@ -38,16 +36,16 @@

## Spring Boot 2(`spring-boot-2/`)

以下示例暂时无法迁移到高版本,予以保留:

- shardingsphere-jdbc-4.x-spring-sample 集成 ShardingSphere JDBC Spring Boot Starter 4.1.1 使用示例, 不再维护,
- shardingsphere-jdbc-4.x-spring-sample 集成 ShardingSphere JDBC Spring Boot Starter 4.1.1 使用示例, 不再维护,
参考 https://github.com/apache/shardingsphere/releases/tag/5.0.0-alpha
- shardingsphere-jdbc-5.x-spring-sample 集成 ShardingSphere JDBC Spring Boot Starter 5.2.1 使用示例,不再维护,
参考 https://github.com/apache/shardingsphere/issues/22469

## 单元测试说明

- 纯 H2 的模块(`mybatisplus3-sample`、`beetlsql-sample`、`jdbc-template-sample`、`name-pattern-sample`、
`add-remove-datasource-sample`、`load-datasource-from-jdbc-sample`、`druid-sample`、`spel-sample`、
`shardingsphere-jdbc-5.x-spring-sample`)可直接 `mvn test`,无需外部服务。
`shardingsphere-jdbc-4.x-spring-sample`、`shardingsphere-jdbc-5.x-spring-sample`)可直接 `mvn test`,无需外部服务。
- 涉及外部中间件的模块使用 [Testcontainers](https://java.testcontainers.org/) 在测试中启动容器,
本地与 CI 运行测试都需要可用的 Docker 环境:
- `quartz-sample`:`mysql:8.4.7-oraclelinux9`
Expand All @@ -57,8 +55,9 @@
- `config-nacos-sample`:`nacos/nacos-server:v3.2.4`(新版 starter 内含 3.x 客户端,走 gRPC,
发布走 v3 运维 API,测试按实际映射端口动态计算 `nacos.server.grpc.port.offset`)
- `shardingsphere-jdbc-5.x-core-sample`:`mysql:8.4.7-oraclelinux9`
- `spring-boot-2` 下模块的主代码保持 Java 8 兼容,因此 JDK 8 的 CI 任务仅做 `clean compile`,
完整测试在 JDK 17/21 任务中执行。
- `spring-boot-2` 下模块的主代码保持 Java 8 兼容,配套使用 testcontainers 1.x(2.x 要求 Java 17+),
因此 JDK 8 的 CI 任务直接对 `shardingsphere-jdbc-4.x-spring-sample` 与 `shardingsphere-jdbc-5.x-spring-sample` 执行 `clean test`;
JDK 17/21 的全量 `clean test` 同样覆盖这两个模块。

## Contributing

Expand Down
10 changes: 10 additions & 0 deletions spring-boot-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@
<h2.version>2.2.224</h2.version>
<spring-boot-dependencies.version>2.7.18</spring-boot-dependencies.version>
<lombok.version>1.18.38</lombok.version>
<!--Java 8 模块只能用 testcontainers 1.x(2.x 要求 Java 17+),与 spring-boot-4 的 2.0.5 分开管理。-->
<testcontainers-bom.version>1.21.3</testcontainers-bom.version>
</properties>

<modules>
<module>shardingsphere-jdbc-4.x-spring-sample</module>
<module>shardingsphere-jdbc-5.x-spring-sample</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions spring-boot-2/shardingsphere-jdbc-4.x-spring-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,40 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>jdk17-plus-opens</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class MyDataSourceConfiguration {

// private final DataSource encryptDataSource;

private final DataSource masterSlaveDataSource;
// private final DataSource masterSlaveDataSource;

// private final DataSource shadowDataSource;

Expand All @@ -64,16 +64,15 @@ public class MyDataSourceConfiguration {
*/
public MyDataSourceConfiguration(DynamicDataSourceProperties properties, DefaultDataSourceCreator dataSourceCreator,
// @Lazy @Qualifier("encryptDataSource") DataSource encryptDataSource,
@Lazy
@Qualifier("masterSlaveDataSource") DataSource masterSlaveDataSource,
// @Lazy @Qualifier("masterSlaveDataSource") DataSource masterSlaveDataSource,
// @Lazy @Qualifier("shadowDataSource") DataSource shadowDataSource,
@Lazy
@Qualifier("shardingDataSource") DataSource shardingDataSource
) {
this.properties = properties;
this.dataSourceCreator = dataSourceCreator;
// this.encryptDataSource = encryptDataSource;
this.masterSlaveDataSource = masterSlaveDataSource;
// this.masterSlaveDataSource = masterSlaveDataSource;
// this.shadowDataSource = shadowDataSource;
this.shardingDataSource = shardingDataSource;
}
Expand All @@ -86,7 +85,7 @@ public DynamicDataSourceProvider dynamicDataSourceProvider() {
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
// dataSourceMap.put("encryptDataSourceInShardingSphere", encryptDataSource);
dataSourceMap.put("masterSlaveDataSourceInShardingSphere", masterSlaveDataSource);
// dataSourceMap.put("masterSlaveDataSourceInShardingSphere", masterSlaveDataSource);
// dataSourceMap.put("shadowDataSourceInShardingSphere", shadowDataSource);
dataSourceMap.put("shardingDataSourceInShardingSphere", shardingDataSource);
return dataSourceMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ spring:
jdbc-url: jdbc:h2:mem:slave2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
username: sa
password: ""
masterslave:
name: baomidou_readwrite_data_sources
load-balance-algorithm-type: round_robin
master-data-source-name: shardingmaster
slave-data-source-names: shardingslave0,shardingslave1
sharding:
tables:
t_order:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.baomidou.samples.shardingsphere.jdbc.v4.spring.mapper.TOrderMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -36,7 +35,6 @@
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@SpringBootTest
@Disabled("TODO wait for com.baomidou:dynamic-datasource-spring-boot-starter:4.2.0 release")
public class ControllerLayerTests {
MockMvc mockMvc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import com.baomidou.samples.shardingsphere.jdbc.v4.spring.mapper.TOrderMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.dao.DataIntegrityViolationException;

import java.util.List;

Expand All @@ -35,7 +34,6 @@
* TODO Unit tests for ShardingSphere 5.4.0 need to be synchronized.
*/
@SpringBootTest
@Disabled("TODO wait for com.baomidou:dynamic-datasource-spring-boot-starter:4.2.0 release")
public class MapperLayerTests {
@Autowired
TOrderMapper tOrderMapper;
Expand Down Expand Up @@ -68,7 +66,7 @@ void whenRequestToAddByNameAndUserId() {
void whenRequestToAddByNameAndUserIdWithPrimaryKey() {
List<TOrder> emptyState = tOrderMapper.findAll();
assertEquals(0, emptyState.size());
assertThrows(UncategorizedSQLException.class, () -> {
assertThrows(DataIntegrityViolationException.class, () -> {
tOrderMapper.addByNameAndUserId("Bright", 114514L);
tOrderMapper.addByNameAndUserId("Jordan", 114515L);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-4-samples</artifactId>
<artifactId>spring-boot-2-samples</artifactId>
<groupId>com.baomidou</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shardingsphere-jdbc-5.x-spring-sample</artifactId>
<properties>
<shardingsphere.version>5.5.2</shardingsphere.version>
<guava.version>33.4.6-jre</guava.version>
<shardingsphere.version>5.2.1</shardingsphere.version>
<!--On ShardingSphere JDBC 5.2.1 we have to use the Java API of SnakeYAML 1.33-->
<snakeyaml.version>1.33</snakeyaml.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand All @@ -39,7 +40,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -55,16 +56,16 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot4-starter</artifactId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright © ${project.inceptionYear} organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.samples.shardingsphere.jdbc.v5.spring.config;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class MyDataSourceConfiguration {
private final DynamicDataSourceProperties properties;
private final DefaultDataSourceCreator dataSourceCreator;
private final DataSource shardingSphereDataSource;

/**
* 1. 建议 spring-boot 2.5.0 以下版本或者发现不加 `@Lazy`, `DataSource shardingSphereDataSource` 是 null 的情况都打开 `@Lazy`
* 2. 与使用 Spring Boot Starter 相比,如果开发者遇到此类问题,
* 开发者应该直接使用 ShardingSphere 的 JDBC Driver 将其配置为 JDBC 数据源,即:
* 使用 `org.apache.shardingsphere:shardingsphere-jdbc-core:5.2.1` 而不是 `org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.2.1`。
* 更多信息请参阅 <a href="https://shardingsphere.apache.org/document/5.2.1/en/user-manual/shardingsphere-jdbc/yaml-config/jdbc_driver/">JDBC Driver</a>
*
* @see org.springframework.context.annotation.Lazy
* @see org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource
*/
public MyDataSourceConfiguration(DynamicDataSourceProperties properties,
DefaultDataSourceCreator dataSourceCreator,
@Qualifier("shardingSphereDataSource") DataSource shardingSphereDataSource) {
this.properties = properties;
this.dataSourceCreator = dataSourceCreator;
this.shardingSphereDataSource = shardingSphereDataSource;
}

@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
return new AbstractDataSourceProvider(dataSourceCreator) {
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 把 shardingSphereDataSource 加入多数据源,到时候使用的时候就可以 `@DS("shardingSphere")`
dataSourceMap.put("shardingSphere", shardingSphereDataSource);
return dataSourceMap;
}
};
}

/**
* 将 dynamic-datasource 设置为首选的
* 当 Spring 存在多个数据源时, 自动注入的是首选的对象
* 设置为主要的数据源之后,就可以支持 shardingSphere 原生的配置方式了
*/
@Primary
@Bean
public DataSource dataSource(List<DynamicDataSourceProvider> providers) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers);
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
Loading
Loading