转:
一、添加依赖
<!-- https:
//mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>
2.4
.
2
</version>
</dependency>
二、配置 application.properties 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#server.port = 8091 #mysql #删除多表 #allowMultiQueries= true #换成 druid spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #设置默认的数据源或者数据源组,默认值即为master spring.datasource.dynamic.primary=master spring.datasource.dynamic.datasource.master.driver- class -name=com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.master.url=jdbc:mysql: //127.0.0.1:3306/wangmx_test1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true spring.datasource.dynamic.datasource.master.username=hopson spring.datasource.dynamic.datasource.master.password=hopson # slave_1 spring.datasource.dynamic.datasource.slave_1.driver- class -name=com.mysql.cj.jdbc.Driver spring.datasource.dynamic.datasource.slave_1.url=jdbc:mysql: //127.0.0.1:3306/wangmx_test2?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true spring.datasource.dynamic.datasource.slave_1.username=hopson spring.datasource.dynamic.datasource.slave_1.password=hopson # druid 下面为连接池的补充设置,应用到上面所有数据源中 # druid 初始化连接数大小,最小,最大 spring.datasource.dynamic.druid.initial-size= 8 spring.datasource.dynamic.druid.min-idle= 8 spring.datasource.dynamic.druid.max-active= 30 # druid 配置获取连接等待超时的时间 spring.datasource.dynamic.druid.max-wait= 30000 # druid 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.dynamic.druid.time-between-eviction-runs-millis= 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.dynamic.druid.min-evictable-idle-time-millis= 300000 # druid 验证数据库连接的有效性 spring.datasource.dynamic.druid.validation-query=select 1 spring.datasource.dynamic.druid.test- while -idle= true spring.datasource.dynamic.druid.test-on-borrow= false spring.datasource.dynamic.druid.test-on- return = false # druid 打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.dynamic.druid.pool-prepared-statements= true spring.datasource.dynamic.druid.max-open-prepared-statements= 20 spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size= 20 # druid 配置监控统计拦截的filters,去掉后监控界面sql无法统计, ‘wall‘ 用于防火墙 spring.datasource.dynamic.druid.filters=stat,slf4j # druid 状态监控 spring.datasource.dynamic.druid.filter.stat.enabled= true spring.datasource.dynamic.druid.filter.stat.db-type=mysql spring.datasource.dynamic.druid.filter.stat.log-slow-sql= true spring.datasource.dynamic.druid.filter.stat.slow-sql-millis= 2000 # druid 监控过滤器 spring.datasource.dynamic.druid.web-stat-filter.enabled= true spring.datasource.dynamic.druid.web-stat-filter.exclusions= "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" # druid 监控页面 spring.datasource.dynamic.druid.stat-view-servlet.enabled= true spring.datasource.dynamic.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.dynamic.druid.stat-view-servlet.reset-enable= false spring.datasource.druid.stat-view-servlet.login-username=root spring.datasource.druid.stat-view-servlet.login-password=root spring.datasource.dynamic.druid.stat-view-servlet.allow= mybatis-plus.mapper-locations=classpath*:/mybatis/*/**.xml mybatis-plus.type-aliases- package =com.bims.hopson.*.entity spring.jackson.serialization.indent_output= true # redis 配置 #spring.redis.host= 192.168 . 0.100 #spring.redis.host= 127.0 . 0.1 #spring.redis.port= 6379 #spring.redis.pool.max-idle= 20 |
三、在启动类上加注解
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
四、启动日志
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
网上关于动态数据源的切换的文档有很多,核心只有两种。
AbstractRoutingDataSource
,参考一些文档自己实现切换。如果你的数据源较少,场景不复杂,选择以上任意一种都可以。如果你需要更多特性,请尝试本动态数据源。
不能使用多数据源事务(同一个数据源下能使用事务),网上其他方案也都不能提供。
如果你需要使用到分布式事务,那么你的架构应该到了微服务化的时候了。
如果呼声强烈,项目达到800 star,作者考虑集成分布式事务。
PS: 如果您只是几个数据库但是有强烈的需求分布式事务,建议还是使用传统方式自己构建多套环境集成atomic这类,网上百度很多。
_
分割的数据源 首部 即为组的名称,相同组名称的数据源会放在一个组下。
强烈建议在 主从模式 下遵循普遍的规则,以便他人能更轻易理解你的代码。
主数据库 建议 只执行 INSERT
UPDATE
DELETE
操作。
从数据库 建议 只执行 SELECT
操作。
引入dynamic-datasource-spring-boot-starter。
配置数据源。
使用 @DS 切换数据源。
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
注解在service实现或mapper接口方法上,但强烈不建议同时在service和mapper注解。 (可能会有问题)
注解 | 结果 |
---|---|
没有@DS | 默认数据源 |
@DS("dsName") | dsName可以为组名也可以为具体某个库的名称 |
在mybatis环境下也可注解在mapper接口层。
__EOF__
作 者:浩宇天尚/Alex
出 处:https://www.cnblogs.com/liboware
关于博主:热爱生活,爱读书/旅游,乐于专研。评论和私信会在第一时间回复。或者直接私信我。
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
原文:https://www.cnblogs.com/libin6505/p/13431938.html