Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes CI error by dynamic-datasource API change #36

Merged
merged 1 commit into from
Oct 29, 2023
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
25 changes: 22 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,33 @@ env:
MAVEN_OPTS: -Dhttps.protocols=TLSv1.2 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true

jobs:
test-ci:
test-hotspot-jdk-ci:
name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 60
strategy:
matrix:
os: [ 'ubuntu-latest', 'windows-latest' ]
java-version: [ '8', '17', '21-ea' ]
os: [ 'ubuntu-latest' ]
java-version: [ '17', '21' ]
steps:
- uses: actions/checkout@v3
- name: Setup java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.java-version }}
cache: 'maven'
- name: Build all test with Maven
run: |
./mvnw -T1C -B -e clean test
test-minimum-hotspot-jdk-ci:
name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 60
strategy:
matrix:
os: [ 'ubuntu-latest' ]
java-version: [ '8' ]
steps:
- uses: actions/checkout@v3
- name: Setup java
Expand Down
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# 演示例子

所有数据库连接为h2数据库,仅供测试。
大部分数据库连接为 H2Database,仅供测试。

所有单元测试可在 OpenJDK 17 / OpenJDK 21 及其下游发行版执行,并在 Github Actions 完成验证。

除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 OpenJDK 8 - OpenJDK 21 及其下游发行版下执行单元测试。

所有测试可在 OpenJDK 17 / OpenJDK 21 及其下游发行版直接跑,注意观察启动的日志。
除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 OpenJDK 8 下执行单元测试。
你可能希望参考 [位于 Github Actions 的 CI 文件](./.github/workflows/ci.yml)。

- add-remove-datasource 动态添加删除数据源的使用示例
Expand All @@ -19,9 +21,20 @@
- quartz-sample 多数据源集成quartz示例
- 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-core-sample 集成 ShardingSphere JDBC Driver 5.4.0 使用示例
- shardingsphere-jdbc-5.x-core-sample 集成 ShardingSphere JDBC Driver 5.4.1 使用示例
- shardingsphere-jdbc-5.x-spring-sample 集成 ShardingSphere JDBC Spring Boot Starter 5.2.1 使用示例, 不再维护,
参考 https://github.com/apache/shardingsphere/issues/22469
- spel-sample 动态从外部参数spel来切换数据源的使用示例
- tx-local-sample 本地事务示例项目★★★★★★必看★★★★★★
- tx-seata-sample 基于seata的分布式事务集成使用示例

## Contributing

我们欢迎社区的贡献。

在提交 Pull Request 之前, 请在本地通过 OpenJDK 17 - OpenJDK 21 下完成此命令的验证。
我们鼓励通过 `SDKMAN!` 切换到 `21.0.1-graalce` 来验证。

```shell
./mvnw -T1C clean test
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.baomidou.samples.spel.entity.User;
import com.baomidou.samples.spel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -29,8 +28,11 @@
@RestController
public class UserController {

@Autowired
private UserService userService;
private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping("/session")
public List<User> session(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package com.baomidou.samples.spel.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

private Integer id;
Expand All @@ -27,4 +31,8 @@ public class User {
private Integer age;

private String tenantName;

public User(String tenantName) {
this(null, null, null, tenantName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.List;

@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
public interface UserMapper {

@Select("select * from t_user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.baomidou.samples.spel.service;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.samples.spel.entity.User;

import java.util.List;
Expand All @@ -29,4 +30,10 @@ public interface UserService {
List<User> selectSpelByKey(String tenantName);

List<User> selecSpelByTenant(User user);

@DS("#tenantName")
String getGroupNameInSpELSelf(String tenantName);

@DS("#user.tenantName")
String getGroupNameInsideObjectSpEL(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.samples.spel.entity.User;
import com.baomidou.samples.spel.mapper.UserMapper;
import com.baomidou.samples.spel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -29,8 +29,11 @@
@DS("slave")
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;
private final UserMapper userMapper;

public UserServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}

@Override
@DS("#session.tenantName")
Expand All @@ -47,6 +50,7 @@ public List<User> selectSpelByHeader() {
@Override
@DS("#tenantName")
public List<User> selectSpelByKey(String tenantName) {
assert !tenantName.equals("tenant1") || DynamicDataSourceContextHolder.peek().equals("tenant1");
return userMapper.selectUsers();
}

Expand All @@ -55,4 +59,16 @@ public List<User> selectSpelByKey(String tenantName) {
public List<User> selecSpelByTenant(User user) {
return userMapper.selectUsers();
}

@Override
@DS("#tenantName")
public String getGroupNameInSpELSelf(String tenantName) {
return DynamicDataSourceContextHolder.peek();
}

@Override
@DS("#user.tenantName")
public String getGroupNameInsideObjectSpEL(User user) {
return DynamicDataSourceContextHolder.peek();
}
}
12 changes: 5 additions & 7 deletions features-samples/spel-sample/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@ spring:
master:
username: sa
password: ""
url: jdbc:h2:mem:test;MODE=MySQL
url: jdbc:h2:mem:master;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
driver-class-name: org.h2.Driver
init:
schema: db/schema.sql
tenant1_1:
username: sa
password: ""
url: jdbc:h2:mem:test;MODE=MySQL
url: jdbc:h2:mem:tenant1_1;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
driver-class-name: org.h2.Driver
tenant1_2:
username: sa
password: ""
url: jdbc:h2:mem:test;MODE=MySQL
url: jdbc:h2:mem:tenant1_2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
driver-class-name: org.h2.Driver
tenant2_1:
username: sa
password: ""
url: jdbc:h2:mem:test;MODE=MySQL
url: jdbc:h2:mem:tenant2_1;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
driver-class-name: org.h2.Driver
tenant2_2:
username: sa
password: ""
url: jdbc:h2:mem:test;MODE=MySQL
url: jdbc:h2:mem:tenant2_2;INIT=RUNSCRIPT FROM 'classpath:db/schema.sql'
driver-class-name: org.h2.Driver
logging:
level:
Expand Down
5 changes: 2 additions & 3 deletions features-samples/spel-sample/src/main/resources/db/schema.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
CREATE TABLE IF NOT EXISTS t_user
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NULL DEFAULT NULL,
age INT(11) NULL DEFAULT NULL,
PRIMARY KEY (id)
age INT NULL DEFAULT NULL
);
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
package com.baomidou.samples.spel.test;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.samples.spel.SpelApplication;
import com.baomidou.samples.spel.entity.User;
import com.baomidou.samples.spel.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -31,6 +32,7 @@
import javax.sql.DataSource;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Expand All @@ -40,56 +42,27 @@

@SpringBootTest(classes = SpelApplication.class, webEnvironment = RANDOM_PORT)
public class SPELTest {

MockMvc mockMvc;

@Autowired
DataSource dataSource;

@Autowired
DefaultDataSourceCreator dataSourceCreator;

@Autowired
UserService userService;

@BeforeEach
void setup(WebApplicationContext webApplicationContext) {
this.mockMvc = webAppContextSetup(webApplicationContext).defaultResponseCharacterEncoding(StandardCharsets.UTF_8).build();
}

@Test
void testSPEL() {
DataSourceProperty masterDataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("master");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("sa");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant1_1DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant1_1");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant1_1");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant1_2DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant1_2");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant1_2");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant2_1DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant2_1");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant2_1");
masterDataSourceProperty.setPassword("");
DataSourceProperty tenant2_2DataSourceProperty = new DataSourceProperty();
masterDataSourceProperty.setPoolName("tenant2_2");
masterDataSourceProperty.setDriverClassName("org.h2.Driver");
masterDataSourceProperty.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
masterDataSourceProperty.setUsername("tenant2_2");
masterDataSourceProperty.setPassword("");
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
ds.addDataSource(masterDataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(masterDataSourceProperty));
ds.addDataSource(tenant1_1DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant1_1DataSourceProperty));
ds.addDataSource(tenant1_2DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant1_2DataSourceProperty));
ds.addDataSource(tenant2_1DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant2_1DataSourceProperty));
ds.addDataSource(tenant2_2DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant2_2DataSourceProperty));
// assertThat(ds.getDataSources().keySet().contains("master", "tenant1_1", "tenant1_2", "tenant2_1", "tenant2_2");
assertThat(ds.getDataSources().keySet()).contains("master", "tenant1_1", "tenant1_2", "tenant2_1", "tenant2_2");
assertDoesNotThrow(() -> {
mockMvc.perform(MockMvcRequestBuilders.get("/users/session").characterEncoding(StandardCharsets.UTF_8))
.andDo(print()).andExpectAll(
Expand All @@ -104,5 +77,7 @@ void testSPEL() {
content().encoding(StandardCharsets.UTF_8)
).andReturn().getResponse().getContentAsString();
});
assertThat(userService.getGroupNameInSpELSelf("tenant1")).isEqualTo("tenant1");
assertThat(userService.getGroupNameInsideObjectSpEL(new User("tenant2"))).isEqualTo("tenant2");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
package com.baomidou.samples.quartz.config;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
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.List;

//测试的时候请打开这个,并关闭其他Mode的配置
//@Configuration
// 测试的时候请打开这个 `@Configuration`,并关闭其他 Mode 的配置
@Configuration
public class MyQuartzAutoConfigurationMode2 {

@Autowired
Expand All @@ -36,8 +39,8 @@ public class MyQuartzAutoConfigurationMode2 {

@Primary
@Bean
public DataSource dataSource() {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
public DataSource dataSource(List<DynamicDataSourceProvider> providers) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers);
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

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

@Configuration
Expand Down Expand Up @@ -100,8 +101,8 @@ public Map<String, DataSource> loadDataSources() {
*/
@Primary
@Bean
public DataSource dataSource() {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
public DataSource dataSource(List<DynamicDataSourceProvider> providers) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers);
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
Expand Down
Loading