一、tienchin健身系统下的技术点复现--动态数据源

阳家坡对面的荫凉 / 2023-06-05 / 原文

一、tienchin健身系统下的技术点复现--动态数据源

自定义一个注解@DynamicDatasource,可以配置在方法或者类上面,标识方法或者类中的所有方法都使用某一个数据源。

使用数据源是使用ThreadLocal获取数据源名称的,当然要先存入到ThreadLocal中

自定义切面,使用环绕通知,拦截@DynamicDatasource注解的方法或者类中的所有方法的时候,将@DynamicDatasource注解所对应的数据源注入到ThreadLocal中。

最后,当Mapper执行方法时,需要DataSource,它会自动去AbstracRoutingDatasource类中去查找目标数据源。我们只需要在重写的方法determineCurrentLookupKey方法中,返回ThreadLocal的值即可。
Gitee代码路径

1.1、@annotation注解切换数据源

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.18</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

1.2 配置application.yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    druid:
      master:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
      slave:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/tesdb2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    # 初始连接数
    initialSize: 5
    # 最小连接池数量
    minIdle: 10
    # 最大连接池数量
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置连接超时时间
    connectTimeout: 30000
    # 配置网络超时时间
    socketTimeout: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    # 配置一个连接在池中最大生存的时间,单位是毫秒
    maxEvictableIdleTimeMillis: 900000
    # 配置检测连接是否有效
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    webStatFilter:
      enabled: true
    statViewServlet:
      enabled: true
      # 设置白名单,不填则允许所有访问
      allow:
      url-pattern: /druid/*
      # 控制台管理用户名和密码
      login-username: tienchin
      login-password: 123456
    filter:
      stat:
        enabled: true
        # 慢SQL记录
        log-slow-sql: true
        slow-sql-millis: 1000
        merge-sql: true
      wall:
        config:
          multi-statement-allow: true

1.3 使用ThreadLocal配置获取动态数据源

这个类就是把ThreadLocal的数据源名称给AbstracRoutingDatasource,它会找到对应的数据源。


配置ThreadLocal工具类


1.4 创建@DynamicDatasource注解


1.5 创建DynamicAspect切面


1.6 配置ConfigurationProperties


若不用此@ConfigurationProperties,可以使用@Value("${spring.datasource.type}") 此种类型。

在用 @ConfigureationProperties 中,会有提示 Not registered via @EnableConfigurationProperties 在 官网 中给出了解决办法

  • 引入依赖
  • 加入spring容器
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>