Skip to content

Commit 74757c1

Browse files
committed
添加代码示例springboot-multisource
1 parent 1d0bc4c commit 74757c1

File tree

21 files changed

+1345
-0
lines changed

21 files changed

+1345
-0
lines changed

springboot-multisource/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea/
2+
target/
3+
*.iml
4+
*.ipr
5+
*.iws
6+
*.log
7+
.svn/
8+
.project
9+
rebel.xml
10+
.rebel-remote.xml.*

springboot-multisource/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-multisource/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## 配置多数据源
2+
3+
SpringBoot配置多数据源,使用MyBatis作为DAO层演示
4+
5+
## 安装MySQL数据库
6+
7+
数据库的安装教程网上非常多,版本最好是mysql5.5+,配置数据库的账号和密码。
8+
并且创建两个数据库pos和biz来测试多数据源。
9+
10+
## 修改application.yml
11+
12+
修改配置文件,主要是mysql的账号和密码,两个数据库都要配置
13+
14+
## 数据库初始化
15+
16+
然后执行SQL文件`src/main/resources/sql/schema.sql`
17+
18+
```
19+
# 下面是核心数据库中的插入数据
20+
INSERT INTO `t_user` VALUES (1,'admin','系统管理员','123456','www', '17890908889', '系统管理员', 1, '2017-12-12 09:46:12', '2017-12-12 09:46:12');
21+
INSERT INTO `t_user` VALUES (2,'aix','张三','123456','eee', '17859569358', '', 1, '2017-12-12 09:46:12', '2017-12-12 09:46:12');
22+
23+
# 下面是biz数据库中的插入数据
24+
INSERT INTO `t_user` VALUES (1,'admin1','系统管理员','123456','www', '17890908889', '系统管理员', 1, '2017-12-12 09:46:12', '2017-12-12 09:46:12');
25+
INSERT INTO `t_user` VALUES (2,'aix1','张三','123456','eee', '17859569358', '', 1, '2017-12-12 09:46:12', '2017-12-12 09:46:12');
26+
27+
```
28+
29+
## 运行测试用例
30+
31+
执行对用户表增/删/改/查的测试用例:`com.xncoding.pos.ApplicationTests.java`
32+
33+
## 许可证
34+
35+
Copyright (c) 2018 Xiong Neng
36+
37+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-multisource/pom.xml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-multisource</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-multisource</name>
13+
<description>SpringBoot配置多数据源</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
<druid.version>1.1.2</druid.version>
27+
<mysql-connector.version>8.0.7-dmr</mysql-connector.version>
28+
<mybatis-plus.version>2.1.8</mybatis-plus.version>
29+
<mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-aop</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-jdbc</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>mysql</groupId>
43+
<artifactId>mysql-connector-java</artifactId>
44+
<version>${mysql-connector.version}</version>
45+
<scope>runtime</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.alibaba</groupId>
49+
<artifactId>druid</artifactId>
50+
<version>${druid.version}</version>
51+
</dependency>
52+
<!-- MyBatis plus增强和springboot的集成-->
53+
<dependency>
54+
<groupId>com.baomidou</groupId>
55+
<artifactId>mybatis-plus</artifactId>
56+
<version>${mybatis-plus.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.baomidou</groupId>
60+
<artifactId>mybatisplus-spring-boot-starter</artifactId>
61+
<version>${mybatisplus-spring-boot-starter.version}</version>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-test</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.hamcrest</groupId>
71+
<artifactId>hamcrest-all</artifactId>
72+
<version>1.3</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-compiler-plugin</artifactId>
82+
<version>3.6.1</version>
83+
<configuration>
84+
<!--<proc>none</proc>-->
85+
<source>1.8</source>
86+
<target>1.8</target>
87+
</configuration>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-surefire-plugin</artifactId>
92+
<version>2.20</version>
93+
<configuration>
94+
<skip>true</skip>
95+
</configuration>
96+
</plugin>
97+
<plugin>
98+
<groupId>org.springframework.boot</groupId>
99+
<artifactId>spring-boot-maven-plugin</artifactId>
100+
<executions>
101+
</executions>
102+
</plugin>
103+
</plugins>
104+
105+
<resources>
106+
<resource>
107+
<directory>src/main/resources</directory>
108+
</resource>
109+
<resource>
110+
<directory>src/main/java</directory>
111+
<includes>
112+
<include>**/*.xml</include>
113+
</includes>
114+
</resource>
115+
</resources>
116+
</build>
117+
118+
</project>

springboot-multisource/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.pos;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.xncoding.pos.common.annotion;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* 多数据源标识
7+
*
8+
* @author xiongneng
9+
* @since 2017年3月5日 上午9:44:24
10+
*/
11+
@Inherited
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target({ElementType.METHOD})
14+
public @interface DataSource {
15+
16+
String name() default "";
17+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.xncoding.pos.common.aop;
2+
3+
import com.xncoding.pos.common.annotion.DataSource;
4+
import com.xncoding.pos.common.mutidatesource.DSEnum;
5+
import com.xncoding.pos.common.mutidatesource.DataSourceContextHolder;
6+
import org.apache.log4j.Logger;
7+
import org.aspectj.lang.ProceedingJoinPoint;
8+
import org.aspectj.lang.Signature;
9+
import org.aspectj.lang.annotation.Around;
10+
import org.aspectj.lang.annotation.Aspect;
11+
import org.aspectj.lang.annotation.Pointcut;
12+
import org.aspectj.lang.reflect.MethodSignature;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
14+
import org.springframework.core.Ordered;
15+
import org.springframework.stereotype.Component;
16+
17+
import java.lang.reflect.Method;
18+
19+
/**
20+
* 多数据源切换的aop
21+
*
22+
* @author xiongneng
23+
* @since 2017年3月5日 上午10:22:16
24+
*/
25+
@Aspect
26+
@Component
27+
@ConditionalOnProperty(prefix = "xncoding", name = "muti-datasource-open", havingValue = "true")
28+
public class MultiSourceExAop implements Ordered {
29+
30+
private Logger log = Logger.getLogger(this.getClass());
31+
32+
@Pointcut(value = "@annotation(com.xncoding.pos.common.annotion.DataSource)")
33+
private void cut() {
34+
35+
}
36+
37+
@Around("cut()")
38+
public Object around(ProceedingJoinPoint point) throws Throwable {
39+
40+
Signature signature = point.getSignature();
41+
MethodSignature methodSignature = null;
42+
if (!(signature instanceof MethodSignature)) {
43+
throw new IllegalArgumentException("该注解只能用于方法");
44+
}
45+
methodSignature = (MethodSignature) signature;
46+
47+
Object target = point.getTarget();
48+
Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
49+
50+
DataSource datasource = currentMethod.getAnnotation(DataSource.class);
51+
if (datasource != null) {
52+
DataSourceContextHolder.setDataSourceType(datasource.name());
53+
log.debug("设置数据源为:" + datasource.name());
54+
} else {
55+
DataSourceContextHolder.setDataSourceType(DSEnum.DATA_SOURCE_CORE);
56+
log.debug("设置数据源为:dataSourceCore");
57+
}
58+
try {
59+
return point.proceed();
60+
} finally {
61+
log.debug("清空数据源信息!");
62+
DataSourceContextHolder.clearDataSourceType();
63+
}
64+
}
65+
66+
67+
/**
68+
* aop的顺序要早于spring的事务
69+
*/
70+
@Override
71+
public int getOrder() {
72+
return 1;
73+
}
74+
75+
}

0 commit comments

Comments
 (0)