动态切换数据源

so-fast框架支持基于注解的动态数据源切换功能。默认只支持主从库。

使用方式如下:

  • 配置文件配置主从DB

     # 数据源配置
     spring:
       datasource:
         type: com.alibaba.druid.pool.DruidDataSource
         driverClassName: com.mysql.cj.jdbc.Driver
         druid:
           # 主库数据源
           master:
             url: jdbc:mysql://xxxx:3306/so-fast?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
             username: username
             password: password
           # 从库数据源
           slave:
             # 从数据源开关/默认关闭
             enabled: false
             url:
             username:
             password:
    
  • 通过注解切换DB

    在Controller或Service的类或方法上添加注解。例如:读写分离场景

     @DynamicDS(value = DataSourceType.SLAVE)
     @ApiOperation(value = "查询操作日志")
     @LogOperator(title = "查询操作日志", type = Constants.SELECT_OPERATOR)
     @GetMapping(value = "/")
     public R<Page<OperatorLog>> list(OperatorLogQueryVo operatorLogQueryVo) {
    
     @DynamicDS(value = DataSourceType.MASTER)
     @ApiOperation(value = "根据ID删除日志")
     @LogOperator(title = "日志删除", type = Constants.DELETE_OPERATOR)
     @PostMapping(value = "/delete/{ids}")
     public R delete(@PathVariable Long[] ids) {
    
Copyright © 2020. 恩梯梯数据(中国)信息技术有限公司. all right reserved,powered by Gitbook该文件修订时间: 2021-01-07 15:24:32

results matching ""

    No results matching ""