Commit a1b7697eb39be26351b7a9f5c5ecd9f21249037f

Authored by timfruit
1 parent 8a9c7145

完成多数据源配置

... ... @@ -46,6 +46,7 @@
46 46 <easyexcel.verion>2.2.7</easyexcel.verion>
47 47 <velocity.version>2.2</velocity.version>
48 48 <screw.version>1.0.5</screw.version>
  49 + <dynamic-datasource.version>3.3.2</dynamic-datasource.version>
49 50 </properties>
50 51  
51 52 <!-- 依赖声明 -->
... ... @@ -271,6 +272,14 @@
271 272 <version>${screw.version}</version>
272 273 </dependency>
273 274  
  275 + <!-- 多数据源 -->
  276 + <dependency>
  277 + <groupId>com.baomidou</groupId>
  278 + <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  279 + <version>${dynamic-datasource.version}</version>
  280 + </dependency>
  281 +
  282 +
274 283 </dependencies>
275 284  
276 285  
... ...
src/main/java/cn/iocoder/dashboard/framework/datasource/enums/DatasourceMark.java 0 → 100644
  1 +package cn.iocoder.dashboard.framework.datasource.enums;
  2 +
  3 +/**
  4 + * 对应于多数据源中不同数据源配置
  5 + *
  6 + * 在方法上使用注解{@code @DS(DatasourceMark.slave)}可以指定slave数据源,默认是master数据源
  7 + */
  8 +public interface DatasourceMark {
  9 + String master = "master";
  10 + String slave = "slave";
  11 +}
... ...
src/main/resources/application-dev.yaml
... ... @@ -4,13 +4,11 @@ server:
4 4 --- #################### 数据库相关配置 ####################
5 5  
6 6 spring:
7   - # 数据源配置项 TODO 多数据源;TODO 监控配置
  7 + # 数据源配置项 TODO 监控配置
  8 + # 排除默认的自动配置,使用dynamic-datasource-spring-boot-starter配置多数据源,整合druid
  9 + autoconfigure:
  10 + exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
8 11 datasource:
9   - name: ruoyi-vue-pro
10   - url: jdbc:mysql://400-infra.server.iocoder.cn:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
11   - driver-class-name: com.mysql.jdbc.Driver
12   - username: root
13   - password: 3WLiVUBEwTbvAfsh
14 12 druid:
15 13 web-stat-filter:
16 14 enabled: true
... ... @@ -32,6 +30,34 @@ spring:
32 30 wall:
33 31 config:
34 32 multi-statement-allow: true
  33 + dynamic:
  34 + # druid全局配置 https://dynamic-datasource.com/en/guide/integration/Druid.html#configurate-parameters
  35 +# druid: #The following are the supported global parameters
  36 +# initial-size:
  37 +# max-active:
  38 +# min-idle:
  39 +# max-wait:
  40 + primary: master
  41 + datasource:
  42 + master:
  43 + name: ruoyi-vue-pro
  44 + url: jdbc:mysql://400-infra.server.iocoder.cn:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
  45 + driver-class-name: com.mysql.jdbc.Driver
  46 + username: root
  47 + password: 3WLiVUBEwTbvAfsh
  48 + # druid 局部配置
  49 +# druid: # The following are independent parameters that can be reset for each db
  50 +# validation-query: select 1 FROM DUAL #such as oracle need this
  51 +# slave:
  52 +# name: ruoyi-vue-pro-slave
  53 +# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
  54 +# driver-class-name: com.mysql.jdbc.Driver
  55 +# username: root
  56 +# password: 123456
  57 +
  58 +
  59 +
  60 +
35 61  
36 62 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
37 63 redis:
... ...
src/main/resources/application-local.yaml
... ... @@ -4,13 +4,11 @@ server:
4 4 --- #################### 数据库相关配置 ####################
5 5  
6 6 spring:
7   - # 数据源配置项 TODO 多数据源;TODO 监控配置
  7 + # 数据源配置项 TODO 监控配置
  8 + # 排除默认的自动配置,使用dynamic-datasource-spring-boot-starter配置多数据源,整合druid
  9 + autoconfigure:
  10 + exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
8 11 datasource:
9   - name: ruoyi-vue-pro
10   - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
11   - driver-class-name: com.mysql.jdbc.Driver
12   - username: root
13   - password: 123456
14 12 druid:
15 13 web-stat-filter:
16 14 enabled: true
... ... @@ -32,6 +30,32 @@ spring:
32 30 wall:
33 31 config:
34 32 multi-statement-allow: true
  33 + dynamic:
  34 + # druid全局配置 https://dynamic-datasource.com/en/guide/integration/Druid.html#configurate-parameters
  35 +# druid: #The following are the supported global parameters
  36 +# initial-size:
  37 +# max-active:
  38 +# min-idle:
  39 +# max-wait:
  40 + primary: master
  41 + datasource:
  42 + master:
  43 + name: ruoyi-vue-pro
  44 + url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
  45 + driver-class-name: com.mysql.jdbc.Driver
  46 + username: root
  47 + password: 123456
  48 + # druid 局部配置
  49 +# druid: # The following are independent parameters that can be reset for each db
  50 +# validation-query: select 1 FROM DUAL #such as oracle need this
  51 +# slave:
  52 +# name: ruoyi-vue-pro-slave
  53 +# url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
  54 +# driver-class-name: com.mysql.jdbc.Driver
  55 +# username: root
  56 +# password: 123456
  57 +
  58 +
35 59  
36 60 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
37 61 redis:
... ... @@ -77,9 +101,9 @@ apollo:
77 101 eagerLoad:
78 102 enabled: true # 设置 Apollo 在日志初始化前生效,可以实现日志的动态级别配置
79 103 jdbc: # 自定义的 JDBC 配置项,用于数据库的地址
80   - url: ${spring.datasource.url}
81   - username: ${spring.datasource.username}
82   - password: ${spring.datasource.password}
  104 + url: ${spring.datasource.dynamic.datasource.master.url}
  105 + username: ${spring.datasource.dynamic.datasource.master.username}
  106 + password: ${spring.datasource.dynamic.datasource.master.password}
83 107  
84 108 --- #################### 服务保障相关配置 ####################
85 109  
... ... @@ -161,3 +185,11 @@ yudao:
161 185 exclude-urls: # 如下两个 url,仅仅是为了演示,去掉配置也没关系
162 186 - ${spring.boot.admin.context-path}/** # 不处理 Spring Boot Admin 的请求
163 187 - ${management.endpoints.web.base-path}/** # 不处理 Actuator 的请求
  188 +
  189 +
  190 +
  191 +
  192 +
  193 +
  194 +
  195 +
... ...