Skip to content

Commit f3aa0e9

Browse files
committed
ApiBoot DataSource Switch 示例上传.
1 parent 9972dc2 commit f3aa0e9

File tree

8 files changed

+254
-0
lines changed

8 files changed

+254
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>api-boot-samples</artifactId>
7+
<groupId>org.minbox.framework</groupId>
8+
<version>2.0.3.RC1</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>api-boot-sample-datasource-switch</artifactId>
13+
<description>ApiBoot DataSource Switch 示例</description>
14+
<dependencies>
15+
<!--SpringBoot Web-->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<!--ApiBoot DataSource Switch-->
21+
<dependency>
22+
<groupId>org.minbox.framework</groupId>
23+
<artifactId>api-boot-starter-datasource-switch</artifactId>
24+
</dependency>
25+
<!--SpringBoot Test-->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
</dependency>
30+
<!--MySQL-->
31+
<dependency>
32+
<groupId>mysql</groupId>
33+
<artifactId>mysql-connector-java</artifactId>
34+
</dependency>
35+
<!--性能测试依赖-->
36+
<dependency>
37+
<groupId>org.databene</groupId>
38+
<artifactId>contiperf</artifactId>
39+
<version>2.3.4</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
<!--ApiBoot版本依赖-->
44+
<dependencyManagement>
45+
<dependencies>
46+
<dependency>
47+
<groupId>org.minbox.framework</groupId>
48+
<artifactId>api-boot-dependencies</artifactId>
49+
<version>2.0.3.RC1</version>
50+
<type>pom</type>
51+
<scope>import</scope>
52+
</dependency>
53+
</dependencies>
54+
</dependencyManagement>
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.minbox.framework.api.boot.sample;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
/**
9+
* @author:恒宇少年 - 于起宇
10+
* <p>
11+
* DateTime:2019-04-02 16:38
12+
* Blog:http://blog.yuqiyu.com
13+
* WebSite:http://www.jianshu.com/u/092df3f77bca
14+
* Gitee:https://gitee.com/hengboy
15+
* GitHub:https://github.com/hengboy
16+
*/
17+
@SpringBootApplication
18+
public class ApiBootDataSourceSwitchSampleApplication {
19+
/**
20+
* logger instance
21+
*/
22+
static Logger logger = LoggerFactory.getLogger(ApiBootDataSourceSwitchSampleApplication.class);
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(ApiBootDataSourceSwitchSampleApplication.class, args);
26+
logger.info("「「「「「ApiBoot DataSource Switch 启动完成.」」」」」");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.minbox.framework.api.boot.sample;
2+
3+
import org.minbox.framework.api.boot.plugin.datasource.annotation.DataSourceSwitch;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
7+
import javax.sql.DataSource;
8+
import java.sql.Connection;
9+
10+
/**
11+
* @author:恒宇少年 - 于起宇
12+
* <p>
13+
* DateTime:2019-04-02 10:58
14+
* Blog:http://blog.yuqiyu.com
15+
* WebSite:http://www.jianshu.com/u/092df3f77bca
16+
* Gitee:https://gitee.com/hengboy
17+
* GitHub:https://github.com/hengboy
18+
*/
19+
@Service
20+
@DataSourceSwitch("master")
21+
public class MasterDataSourceSampleService {
22+
23+
@Autowired
24+
private DataSource dataSource;
25+
26+
@Autowired
27+
private Slave1DataSourceSampleService slave1DataSourceSampleService;
28+
@Autowired
29+
private Slave2DataSourceSampleService slave2DataSourceSampleService;
30+
31+
public void print() throws Exception {
32+
Connection connection = dataSource.getConnection();
33+
System.out.println(this.getClass().getSimpleName() + " ->" + connection.getCatalog());
34+
connection.close();
35+
// slave 1
36+
slave1DataSourceSampleService.print();
37+
// slave 2
38+
slave2DataSourceSampleService.print();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.minbox.framework.api.boot.sample;
2+
3+
import org.minbox.framework.api.boot.plugin.datasource.annotation.DataSourceSwitch;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
7+
import javax.sql.DataSource;
8+
import java.sql.Connection;
9+
10+
/**
11+
* @author:恒宇少年 - 于起宇
12+
* <p>
13+
* DateTime:2019-04-02 11:00
14+
* Blog:http://blog.yuqiyu.com
15+
* WebSite:http://www.jianshu.com/u/092df3f77bca
16+
* Gitee:https://gitee.com/hengboy
17+
* GitHub:https://github.com/hengboy
18+
*/
19+
@Service
20+
@DataSourceSwitch("slave_1")
21+
public class Slave1DataSourceSampleService {
22+
@Autowired
23+
private DataSource dataSource;
24+
25+
public void print() throws Exception {
26+
Connection connection = dataSource.getConnection();
27+
System.out.println(this.getClass().getSimpleName() + " ->" + connection.getCatalog());
28+
connection.close();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.minbox.framework.api.boot.sample;
2+
3+
import org.minbox.framework.api.boot.plugin.datasource.annotation.DataSourceSwitch;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
7+
import javax.sql.DataSource;
8+
import java.sql.Connection;
9+
10+
/**
11+
* @author:恒宇少年 - 于起宇
12+
* <p>
13+
* DateTime:2019-04-02 11:00
14+
* Blog:http://blog.yuqiyu.com
15+
* WebSite:http://www.jianshu.com/u/092df3f77bca
16+
* Gitee:https://gitee.com/hengboy
17+
* GitHub:https://github.com/hengboy
18+
*/
19+
@Service
20+
@DataSourceSwitch("slave_2")
21+
public class Slave2DataSourceSampleService {
22+
@Autowired
23+
private DataSource dataSource;
24+
25+
public void print() throws Exception {
26+
Connection connection = dataSource.getConnection();
27+
System.out.println(this.getClass().getSimpleName() + " ->" + connection.getCatalog());
28+
connection.close();
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
spring:
2+
application:
3+
name: api-boot-sample-datasource-switch
4+
api:
5+
boot:
6+
datasource:
7+
# 默认的数据源,默认值为master
8+
primary: master
9+
# 配置使用hikari数据源
10+
hikari:
11+
# master datasource config
12+
master:
13+
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai
14+
username: root
15+
password: 123456
16+
# 默认值为【com.mysql.cj.jdbc.Driver】
17+
#driver-class-name: com.mysql.cj.jdbc.Driver
18+
# slave 1
19+
slave_1:
20+
url: jdbc:mysql://localhost:3306/oauth2?characterEncoding=utf8&serverTimezone=Asia/Shanghai
21+
username: root
22+
password: 123456
23+
# slave 2
24+
slave_2:
25+
url: jdbc:mysql://localhost:3306/resources?characterEncoding=utf8&serverTimezone=Asia/Shanghai
26+
username: root
27+
password: 123456
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.minbox.framework.api.boot.sample;
2+
3+
import org.databene.contiperf.PerfTest;
4+
import org.databene.contiperf.junit.ContiPerfRule;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
/**
13+
* ApiBootDataSourceSwitchTester
14+
*
15+
* @author:恒宇少年 - 于起宇
16+
* <p>
17+
* DateTime:2019-04-02 16:37
18+
* Blog:http://blog.yuqiyu.com
19+
* WebSite:http://www.jianshu.com/u/092df3f77bca
20+
* Gitee:https://gitee.com/hengboy
21+
* GitHub:https://github.com/hengboy
22+
*/
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest(classes = ApiBootDataSourceSwitchSampleApplication.class)
25+
public class ApiBootDataSourceSwitchTester {
26+
27+
@Rule
28+
public ContiPerfRule i = new ContiPerfRule();
29+
30+
@Autowired
31+
private MasterDataSourceSampleService masterDataSourceSampleService;
32+
33+
/**
34+
* 开启100个线程,执行10000次
35+
*
36+
* @throws Exception
37+
*/
38+
@Test
39+
@PerfTest(invocations = 10000, threads = 100)
40+
public void test() throws Exception {
41+
masterDataSourceSampleService.print();
42+
}
43+
}

api-boot-samples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>api-boot-sample-alibaba-oss</module>
1919
<module>api-boot-sample-alibaba-sms</module>
2020
<module>api-boot-sample-quartz</module>
21+
<module>api-boot-sample-datasource-switch</module>
2122
</modules>
2223

2324
<build>

0 commit comments

Comments
 (0)