![](/style/images/good.png)
![](/style/images/bad.png)
若依(ruoyi)开源系统-多数据源问题踩坑实录 - 敬YES
source link: https://www.cnblogs.com/janes/p/17756455.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
若依(ruoyi)开源系统-多数据源问题踩坑实录 - 敬YES - 博客园
随笔- 229 文章- 1 评论- 1173 阅读-
78万
上一节内容 介绍了用开源系统若依(ruoyi)搭建页面的过程。在实际项目中,经常遇到多数据源后者主从库的情况。本节记录若依多数据源配置过程中遇到的问题排查过程。
1.上一节在ry-vue库中新建了表t_user,这次新建数据库jingyes,新加同样的表t_user。其他功能不变,我们将t_user数据源由ry-vue切换到jingyes库,实现简单的多数据源场景。
CREATE TABLE `t_user` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) NOT NULL DEFAULT '' COMMENT '姓名',
`age` int(11) NULL DEFAULT NULL COMMENT '年龄',
`gender` tinyint(2) NOT NULL DEFAULT 0 COMMENT '性别,0:女 1:男',
PRIMARY KEY (`id`)
) COMMENT = '用户表';
若依多数据源配置
官网教程操作若依多数据源配置
http://doc.ruoyi.vip/ruoyi/document/htsc.html#多数据源使用
- application-druid.yml 新增数据源配置
spring:
datasource:
jingyes:
url: jdbc:mysql://localhost:13306/jingyes?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
- 扩展DataSourceType枚举,新增数据源JINGYES
public enum DataSourceType
{
/**
* 主库
*/
MASTER,
/**
* 从库
*/
SLAVE,
JINGYES;
}
- 在目标方法设置注解 @DataSource
@Override
@DataSource(value = DataSourceType.JINGYES)
public List<TUser> selectTUserList(TUser tUser)
{
return tUserMapper.selectTUserList(tUser);
}
- 调整DruidConfig,新增数据源配置
@Bean @ConfigurationProperties("spring.datasource.druid.jingyes") @ConditionalOnProperty(prefix = "spring.datasource.druid.jingyes", name = "enabled", havingValue = "true") public DataSource jingyesDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); } @Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dataSource(DataSource masterDataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); //这里新增自定义数据源 setDataSource(targetDataSources, DataSourceType.JINGYES.name(), "jingyesDataSource"); return new DynamicDataSource(masterDataSource, targetDataSources); }
- 重启服务端项目
发现点击上一节创建的“外包用户管理”,显示“切换到JINGYES数据源”,然后实际上访问的仍然上MASTER数据源,也就是ry-vue库。(jingyes.t_user表目前还没有数据)
三、调试排查
跟踪调试一下,发现个问题,这里的targetDataSources居然只有Master,那put进去的其他两条呢?
![37001-20231011102929564-1383625963.png](https://img2023.cnblogs.com/blog/37001/202310/37001-20231011102929564-1383625963.png)
终于被我找到了个若依的坑:这里的异常被吃掉了!!
![37001-20231011102942657-1530405440.png](https://img2023.cnblogs.com/blog/37001/202310/37001-20231011102942657-1530405440.png)
暂时先打印下异常日志,看下到底什么情况,感觉快找到问题了。
![37001-20231011102954638-641667608.png](https://img2023.cnblogs.com/blog/37001/202310/37001-20231011102954638-641667608.png)
上面命名定义了bean了,为什么会没有呢?返回去看看
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
@ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
public DataSource slaveDataSource(DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
原来是属性中enabled的原因,恍然大悟啊。
四、解决方案
最终解决的方式很简单,把application-druid.yml中jingyes数据源新增enabled=true。
spring:
datasource:
jingyes:
enabled: true #看这里!!
url: jdbc:mysql://localhost:13306/jingyes?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
重启服务端项目,再调试一下,发现可以取到正确的数据源了。
![37001-20231011103010158-1689423513.png](https://img2023.cnblogs.com/blog/37001/202310/37001-20231011103010158-1689423513.png)
补充一点,@DataSource除了放在Service层,直接放在Mapper接口也是可以的。
@DataSource(value = DataSourceType.JINGYES)
public List<TUser> selectTUserList(TUser tUser);
本人公众号[ 敬YES ]同步更新,欢迎大家关注~
![img](https://img2023.cnblogs.com/blog/37001/202308/37001-20230822194405666-261743903.jpg)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK