SpringBoot集成多数据源

摘要:
--动态数据源配置--˃com.baomidoudynamic-datasource-spring-boot-starter3.2.02、在application.yml,将spring.profiles.active改为system,dynamicDataapplication.yml修改配置可以如下:spring:profiles:#切换配置文件active:system,dynamicData3、在application-dynamicData.yml,设置主数据源、从数据源数据库配置信息,可以配置多个从数据源。useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8username:XXXXpassword:XXXXdriver-class-name:com.mysql.cj.jdbc.Drivertype:com.alibaba.druid.pool.DruidDataSource#strict为false时候,可以不配置从数据源slave_1:url:jdbc:mysql://localhost:3306/dbname2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8username:xxxxpassword:xxxxdriver-class-name:com.mysql.jdbc.Drivertype:com.alibaba.druid.pool.DruidDataSource#集成多数据源,需要排除druid默认配置,从动态数据源获取druid配置autoconfigure:exclude:com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure三、代码中使用多数据源使用@DS切换数据源。
一、多数据源集成简介

多数据源集成使用
dynamic-sring-boot-starter,github地址:https://github.com/baomidou/dynamic-datasource-spring-boot-starter,详细使用可以参考github地址。

二、BDP框架集成多数据源步骤
  • 1、pom.xm文件引入dynamic-datasource-spring-boot-starter maven依赖。maven依赖配置如下
 <!--https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
        <!--动态数据源配置-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
  • 2、在application.yml,将spring.profiles.active改为system,dynamicDataapplication.yml修改配置可以如下:
spring:profiles:#    切换配置文件
    active: system,dynamicData
  • 3、在application-dynamicData.yml,设置主数据源、从数据源数据库配置信息,可以配置多个从数据源。

关键配置如下:

spring:
  datasource:
      dynamic:
        druid: #以下是全局默认值,可以全局更改
          # 下面为连接池的补充设置,应用到上面所有数据源中
          # 初始化大小,最小,最大
          initialSize: 1
          minIdle: 3
          maxActive: 100
          # 配置获取连接等待超时的时间
          maxWait: 6000
          # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
          timeBetweenEvictionRunsMillis: 1000
          # 配置一个连接在池中最小生存的时间,单位是毫秒
          minEvictableIdleTimeMillis: 30000
          validationQuery: select 'x' FROM DUAL
          testWhileIdle: true
          testOnBorrow: false
          testOnReturn: false
          # 打开PSCache,并且指定每个连接上PSCache的大小
          poolPreparedStatements: true
          maxPoolPreparedStatementPerConnectionSize: 20
          # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
          filters: stat,wall,slf4j
          # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
          connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
        primary: master #设置默认的数据源或者数据源组,默认值即为master
        strict: true #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
        datasource:
          master:
            url: jdbc:mysql://localhost:3306/dbname1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
            username: XXXX
            password: XXXX
            driver-class-name: com.mysql.cj.jdbc.Driver
            type: com.alibaba.druid.pool.DruidDataSource
          # strict为false时候,可以不配置从数据源
          slave_1:
            url: jdbc:mysql://localhost:3306/dbname2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
            username: xxxx
            password: xxxx
            driver-class-name: com.mysql.jdbc.Driver
            type: com.alibaba.druid.pool.DruidDataSource
#   集成多数据源,需要排除druid默认配置,从动态数据源获取druid配置
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
三、代码中使用多数据源
  1. 使用 @DS 切换数据源。@DS 可以注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解。

注解

结果

没有@DS

使用默认数据库

@DS("dbName")

dbName可以为组名也可以为具体某个库的名称

代码使用实例

建议在service层类上或者方法添加@DS("dsName"),可以用来指定数据库连接那个数据库。

@RestController
@RequestMapping("/dbTest")
public classDynamicDatasourceTest {
    @Autowired
    privateUserServiceImpl userService;

    @GetMapping("/list")
    public ResponseResult<List<SAuUser>>list() {
        final List<SAuUser> list =userService.selectAll();
        return  ResponseResult.renderSuccess(list, "获取用户信息成功");
    }
}

//建议在service层类上或者方法添加@DS("dsName"),可以用来指定数据库连接那个数据库
@Service
@DS("slave_1")  //slave_1 多数据源从库名称 ,如果不使用@DB注解,默认连接主库
classUserServiceImpl {
    @Autowired
    privateISAuUserService userService;

    public List<SAuUser>selectAll() {
        final List<SAuUser> list =userService.list();
        returnlist;
    }

}

免责声明:文章转载自《SpringBoot集成多数据源》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MySQL++:liunx 安装 MySQL微信小程序-设置本地缓存和读取下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

springboot中的AOP开发

三步: 1.引入springboot-boot-start-aop jar包 <!--springboot与aop集成jar包--> <dependency> <groupId>org.springframework.boot</groupId>...

Spring MVC-表单(Form)标签-单选按钮集合(RadioButtons)示例(转载实践)

以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_radiobuttons.htm 说明:示例基于Spring MVC 4.1.6。 以下示例显示如何使用Spring Web MVC框架在窗体中使用RadioButtons。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spri...

spring/spring boot/spring mvc中用到的注解

在spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解。 Spring最开始是为了解决EJB等大型企业框架对应用程序的侵入性,因此大量依靠配置文件来“非侵入式”得给POJO增加功能,然而,从Spring 3.x开始,Spring被外界最为诟病的一点就是配置繁多,号称“配置地狱”,各种xml文件,出了问题非常难排查。从Sprin...

关于Spring的问题(一)

一、核心容器(应用上下文) 模块 这是基本的Spring模块,提供spring 框架的基础功能,BeanFactory 是 任何以spring为基础的应用的核心。Spring 框架建立在此模块之上,它使Spring成为一个容器。 二、BeanFactory – BeanFactory 实现举例 Bean 工厂是工厂模式的一个实现,提供了控制反转功能,用来把...

Springboot + Mybatis 多数据源配置

1、src/main/resources/application.properties 中配置好多个数据源   spring.datasource.initialize=false  #接口请求端口号、路径  server.port=9090  servcer.context-path=/  #mybatis配置  #mybatis.config-loca...

SpringBoot 学集 (第三章) 日志框架

写在前面:分享技术,共同进步,有不足请见谅,相关意见可评论告知 ~ 编程路漫漫,学习无止尽! 目录 日志框架 SLF4j使用 统一日志记录 SpringBoot日志关系总结 日志框架的使用; 日志的级别 SpringBoot修改日志的默认配置 日志输出格式: 指定配置 常见错误及其异常①log4j中Logger.getLogger(...