Skip to content

Commit 6d0f19a

Browse files
committed
feat: 引入 druid、dynamic-datasoure
1 parent 74e2186 commit 6d0f19a

6 files changed

Lines changed: 194 additions & 24 deletions

File tree

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<mybatis-plus.version>3.5.1</mybatis-plus.version>
4141
<p6spy.version>3.8.1</p6spy.version>
4242
<jasypt.version>2.1.1</jasypt.version>
43+
<druid.version>1.2.15</druid.version>
44+
<dynamic-ds.version>3.6.0</dynamic-ds.version>
4345
</properties>
4446

4547
<dependencies>
@@ -134,6 +136,16 @@
134136
<artifactId>p6spy</artifactId>
135137
<version>${p6spy.version}</version>
136138
</dependency>
139+
<dependency>
140+
<groupId>com.alibaba</groupId>
141+
<artifactId>druid-spring-boot-starter</artifactId>
142+
<version>${druid.version}</version>
143+
</dependency>
144+
<dependency>
145+
<groupId>com.baomidou</groupId>
146+
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
147+
<version>${dynamic-ds.version}</version>
148+
</dependency>
137149
</dependencies>
138150
</dependencyManagement>
139151

uni-boot-application/src/main/resources/application-dev.yml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@ spring:
66
port: 6379
77
password: ENC(jIiKGruIMgDkKx5wj2gJRCROqPutkTvZ)
88
database: 0
9-
lettuce:
10-
pool:
11-
max-active: 8
12-
min-idle: 0
13-
max-idle: 8
14-
max-wait: -1
9+
datasource:
10+
type: com.alibaba.druid.pool.DruidDataSource
11+
dynamic:
12+
primary: master
13+
datasource:
14+
master:
15+
url: jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
16+
username: root
17+
password: ENC(donQ3DazAc4/6B+NFSRdxw==)
18+
driver-class-name: com.mysql.cj.jdbc.Driver
19+
slave1:
20+
url: jdbc:mysql://localhost:3306/demo_cluster?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
21+
username: root
22+
password: ENC(donQ3DazAc4/6B+NFSRdxw==)
23+
driver-class-name: com.mysql.cj.jdbc.Driver
24+
1525
# uni-boot-admin 配置
1626
uni-boot:
1727
config:
1828
name: UniBootAdmin
1929
version: 0.0.1
2030
swagger-on: true
21-
dynamic-ds-on: true
31+
dynamic-ds-on: false
2232
# swagger 配置
2333
swagger:
2434
title: Swagger 在线文档
@@ -37,21 +47,5 @@ uni-boot:
3747
expiration: 86400
3848
secret: 12345678123456781234567812345678
3949
ignore-urls:
50+
- /druid/**
4051
- /demo/**
41-
# 动态数据源配置
42-
dynamic-ds:
43-
master: db1
44-
datasource:
45-
db1:
46-
# url 加上 p6spy 开启 sql 监控
47-
jdbc-url: jdbc:p6spy:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
48-
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
49-
username: root
50-
password: ENC(donQ3DazAc4/6B+NFSRdxw==)
51-
pool-name: db1_pool
52-
db2:
53-
jdbc-url: jdbc:p6spy:mysql://localhost:3306/demo_cluster?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
54-
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
55-
username: root
56-
password: ENC(donQ3DazAc4/6B+NFSRdxw==)
57-
pool-name: db2_pool

uni-boot-application/src/main/resources/application.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,45 @@ spring:
77
jackson:
88
date-format: yyyy-MM-dd HH:mm:ss
99
time-zone: GMT+8
10+
# redis 公共配置
11+
redis:
12+
lettuce:
13+
pool:
14+
max-active: 8
15+
min-idle: 0
16+
max-idle: 8
17+
max-wait: -1
18+
# 数据源公共配置
19+
datasource:
20+
druid:
21+
initialSize: 5
22+
minIdle: 10
23+
maxActive: 20
24+
maxWait: 60000
25+
timeBetweenEvictionRunsMillis: 60000
26+
minEvictableIdleTimeMillis: 300000
27+
maxEvictableIdleTimeMillis: 900000
28+
validationQuery: SELECT 1 FROM DUAL
29+
testWhileIdle: true
30+
testOnBorrow: false
31+
testOnReturn: false
32+
webStatFilter:
33+
enabled: true
34+
statViewServlet:
35+
enabled: true
36+
allow:
37+
url-pattern: /druid/*
38+
login-username: admin
39+
login-password: admin123
40+
filter:
41+
stat:
42+
enabled: true
43+
log-slow-sql: true
44+
slow-sql-millis: 1000
45+
merge-sql: false
46+
wall:
47+
config:
48+
multi-statement-allow: true
1049

1150
# mybatis plus 配置
1251
mybatis-plus:

uni-boot-common/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,15 @@
9393
<groupId>p6spy</groupId>
9494
<artifactId>p6spy</artifactId>
9595
</dependency>
96+
<!--druid 连接池-->
97+
<dependency>
98+
<groupId>com.alibaba</groupId>
99+
<artifactId>druid-spring-boot-starter</artifactId>
100+
</dependency>
101+
<!--动态数据源-->
102+
<dependency>
103+
<groupId>com.baomidou</groupId>
104+
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
105+
</dependency>
96106
</dependencies>
97107
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package top.cadecode.uniboot.demo.controller;
2+
3+
import com.baomidou.dynamic.datasource.annotation.DS;
4+
import io.swagger.annotations.Api;
5+
import io.swagger.annotations.ApiOperation;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.jdbc.datasource.AbstractDataSource;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import top.cadecode.uniboot.common.annotation.ApiFormat;
13+
14+
import java.sql.SQLException;
15+
16+
/**
17+
* dynamic-datasource 测试
18+
*
19+
* @author Cade Li
20+
* @date 2023/2/22
21+
*/
22+
@ApiFormat
23+
@Slf4j
24+
@RequiredArgsConstructor
25+
@Api(tags = "dynamic-datasource 测试")
26+
@RestController
27+
@RequestMapping("demo/ds")
28+
public class DynamicDsController {
29+
30+
/**
31+
* 注入 Spring 数据源,用来查看 jdbc url 验证数据源切换
32+
*/
33+
private final AbstractDataSource dataSource;
34+
35+
@DS("master")
36+
@ApiOperation("测试 master 数据源")
37+
@PostMapping("test_master")
38+
public String testMaster() throws SQLException {
39+
return dataSource.getConnection().getMetaData().getURL();
40+
}
41+
42+
@DS("slave1")
43+
@ApiOperation("测试 slave1 数据源")
44+
@PostMapping("test_slave1")
45+
public String testSlave1() throws SQLException {
46+
return dataSource.getConnection().getMetaData().getURL();
47+
}
48+
49+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package top.cadecode.uniboot.framework.config;
2+
3+
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
4+
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
5+
import com.alibaba.druid.util.Utils;
6+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
9+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
import javax.servlet.*;
14+
import java.io.IOException;
15+
16+
/**
17+
* druid 连接池配置
18+
*
19+
* @author Cade Li
20+
* @date 2023/2/22
21+
*/
22+
@Configuration
23+
@ConditionalOnWebApplication
24+
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
25+
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
26+
public class DruidConfig {
27+
28+
/**
29+
* 去除 druid 监控页面底部广告的过滤器
30+
*/
31+
@Bean
32+
public FilterRegistrationBean<Filter> removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
33+
// 获取配置的 druid 监控页面的路径
34+
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
35+
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
36+
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
37+
String filePath = "support/http/resources/js/common.js";
38+
Filter filter = new Filter() {
39+
@Override
40+
public void init(FilterConfig filterConfig) {
41+
}
42+
43+
@Override
44+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
45+
chain.doFilter(request, response);
46+
// 重置缓冲区,响应头不会被重置
47+
response.resetBuffer();
48+
// 获取 common.js
49+
String text = Utils.readFromResource(filePath);
50+
// 替换 js buildFooter 方法,让其不执行
51+
text = text.replace("this.buildFooter();", "");
52+
response.getWriter().write(text);
53+
}
54+
55+
@Override
56+
public void destroy() {
57+
}
58+
};
59+
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
60+
registrationBean.setFilter(filter);
61+
registrationBean.addUrlPatterns(commonJsPattern);
62+
return registrationBean;
63+
}
64+
}
65+
66+

0 commit comments

Comments
 (0)