若依微服务中配置 MySQL + DM 多数据源

文章目录
  • 1、导入 MySQL 和达梦(DM)依赖
  • 2、在 application-druid.yml 中配置达梦(DM)数据源
  • 3、在 DruidConfig 类中配置多数据源信息
  • 4、在 Service 层或方法级别切换数据源
    • 4.1 在 Service 类上切换到从库数据源
    • 4.2 在方法级别切换数据源

本文将详细说明如何在若依(RuoYi)微服务架构中集成 MySQL 和达梦(DM)数据库,实现多数据源配置。通过配置多个数据源,可以灵活管理数据库资源,满足不同的数据存储需求。

1、导入 MySQL 和达梦(DM)依赖

首先,在项目的 pom.xml 文件中添加 MySQL 和达梦数据库的驱动依赖,以便项目能够连接这两个数据库。

复制代码
<!-- MySQL 驱动包 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- 达梦 (DM) 驱动包 -->
<dependency>
    <groupId>com.dameng</groupId>
    <artifactId>Dm7JdbcDriver18</artifactId>
    <version>7.6.0.165</version>
</dependency>

添加上述依赖后,项目可以支持 MySQL 和达梦(DM)数据库连接。

2、在 application-druid.yml 中配置达梦(DM)数据源

application-druid.yml 文件中配置主数据源和从数据源,分别指定 MySQL 和达梦数据库连接信息。此配置示例如下:

复制代码
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
            # 主库数据源配置(MySQL)
            master:
                url: jdbc:mysql://localhost:3306/yourdb
                username: yourusername
                password: yourpassword
                driverClassName: com.mysql.cj.jdbc.Driver
            # 从库数据源配置(达梦 DM)
            slave:
                enabled: true  # 启用达梦数据源
                url: jdbc:dm://localhost:5236/yourdb
                username: yourusername
                password: yourpassword
                driverClassName: dm.jdbc.driver.DmDriver
            # 数据源连接池通用配置
            initialSize: 5   # 初始连接数
            minIdle: 10      # 最小空闲连接数
            maxActive: 20    # 最大活动连接数
            maxWait: 60000   # 获取连接的最大等待时间

通过以上配置,MySQL 数据源被设为主数据源(master),而达梦(DM)被设为从数据源(slave)。在从库数据源中,将 enabled 设置为 true 以启用该数据源。

3、在 DruidConfig 类中配置多数据源信息

DruidConfig 类中定义多数据源的配置信息,使应用能够识别并使用配置的 MySQL 和达梦数据库。以下为 DruidConfig 的配置代码示例:

复制代码
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;

@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.druid.master")
    public DataSource masterDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @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);
    }
}

DruidConfig 中,我们定义了 masterDataSourceslaveDataSource。其中 @ConditionalOnProperty 注解确保从库(slave)在配置 enabledtrue 时才启用。通过这种方式,可以轻松启用或禁用从库数据源。

4、在 Service 层或方法级别切换数据源

通过 @DataSource 注解,可以在 Service 层或具体方法上灵活地切换数据源。若不指定数据源,系统默认会使用主数据源(MySQL)。

4.1 在 Service 类上切换到从库数据源

在整个 Service 类上添加 @DataSource 注解,以指定默认使用从库(达梦)数据源:

复制代码
@Service
@DataSource(value = DataSourceType.SLAVE)
@Transactional
public class TestServiceImpl implements ITestService
{
    @Autowired
    private TestMapper testMapper;

    @Override
    public User selectAll()
    {
        return testMapper.selectAll();
    }
}

此示例中,@DataSource(value = DataSourceType.SLAVE) 指定 TestServiceImpl 使用达梦数据库作为默认数据源。

4.2 在方法级别切换数据源

如果仅希望特定方法使用从库数据源,可以在方法上添加 @DataSource 注解,而类级别默认数据源依然为主库。

复制代码
@Service
@Transactional
public class TestServiceImpl implements ITestService
{
    @Autowired
    private TestMapper testMapper;

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public User selectAll()
    {
        return testMapper.selectAll();
    }
}

在这种情况下,selectAll 方法使用达梦(DM)从库数据源,其余方法则默认使用主库 MySQL 数据源。

通过上述配置,若依项目即可支持 MySQL 和达梦(DM)多数据源的灵活切换。如果未能成功切换数据源,请仔细检查每一步配置,并确保所需依赖项和配置文件正确无误。

相关推荐
喝养乐多长不高1 小时前
JAVA微服务脚手架项目详解(三)
java·大数据·微服务·文件·地图·oss
ALex_zry2 小时前
MySQL连接数管理与优化实操经验分享
android·mysql·adb
hour_go2 小时前
《微服务系统故障诊断》:核心概念、技术流派与未来展望
微服务·云原生·架构
t***D2642 小时前
MySQL安全
数据库·mysql·安全
百***48073 小时前
Python使用PyMySQL操作MySQL完整指南
数据库·python·mysql
q***07143 小时前
MySQL无法连接到本地localhost的解决办法2024.11.8
数据库·mysql·adb
4***V2023 小时前
PHP在微服务通信中的消息队列
开发语言·微服务·php
apigfly3 小时前
深入Android系统(十三)Android的窗口系统
android·设计模式·源码
k***85843 小时前
【SpringBoot】【log】 自定义logback日志配置
android·前端·后端