springboot,druid动态数据源切换

关键字:springboot,druid数据库连接池,两个数据源(可以切换成多个),事务管理

关于druid简介传送门:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
在这里插入图片描述

具体分为四步:

1,ThreadLocal维护分支信息,

/**
 * @ClassName DataSourceContextHolder
 * @Description: 设置threadlocal数据元信息
 * @Author gjq
 * @Date 2024/2/29
 * @Version V1.0
 **/
public class DataSourceContextHolder {

    /**使用Threadlocal维护变量,threadlocal为每个使用该变量的现成提供独立的变量副本,
        所以每一个线程可以独立的改变自己的副本,而不会影响其他线程使用对应的副本
     **/
    private static final ThreadLocal<String> HOLDER = new InheritableThreadLocal<>();

    public static void setDataSource(String key) {
        HOLDER.set(key);
    }

    public static String getDataSource() {
        return HOLDER.get();
    }

    public static void clearDataSource() {
        HOLDER.remove();
    }

}

2,创建数据源设置方法,初始化数据源信息

package com.pg.ga.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;

/**
 * @ClassName DynamicDataSource
 * @Description:
 * @Author gjq
 * @Date 2024/2/29
 * @Version V1.0
 **/
public class DynamicDataSource extends AbstractRoutingDataSource {

    public DynamicDataSource(DataSource dataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(dataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }

}

3,多数据源配置,及枚举值定义

枚举值定义:
package com.pg.ga.datasource;

public enum DataSourceType {
    MASTER,
    SLAVE
}

package com.pg.ga.conf;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.pg.ga.datasource.DataSourceType;
import com.pg.ga.datasource.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.*;

/**
 * @ClassName PgConfig
 * @Description:类描述
 * @Author gjq
 * @Date 2024/2/29
 * @Version V1.0
 **/
@Configuration
@MapperScan({"com.pg.ga.dao"})
public class PgConfig {


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

    @Bean
    @ConfigurationProperties("spring.datasource.druid.slave")
    // @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enable", havingValue = "true")
    public DataSource slaveDataSource() {
        return DruidDataSourceBuilder.create().build();
    }


    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
        Map<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DataSourceType.MASTER.name(), masterDataSource);
        targetDataSource.put(DataSourceType.SLAVE.name(), slaveDataSource);
        return new DynamicDataSource(masterDataSource(), targetDataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dynamicDataSource) throws Exception {
        //核心就是这个,替换原始的SqlSessionFactoryBean 用mybatisSqlSessionFactoryBean即可
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dynamicDataSource);
        //设置mapper.xml位置路径
        factoryBean.setMapperLocations(resolveMapperLocations());
        return factoryBean.getObject();
    }

    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath*:mapper/*.xml");
        List<Resource> resourceList = new ArrayList<>();
        if (null != mapperLocations) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourcePatternResolver.getResources(mapperLocation);
                    resourceList.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return resourceList.toArray(new Resource[resourceList.size()]);
    }


    /**
     * @MethodName:
     * @Description: 配置@Transactional注解事务
     * @Param:
     * @Return:
     * @Author: gjq
     * @Date: 2024/2/29
     **/
    @Bean
    public PlatformTransactionManager transactionManager(DynamicDataSource dynamicDataSource) {
        return new DataSourceTransactionManager(dynamicDataSource);
    }

}

4,动态切换注解定义

package com.pg.ga.datasource;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {

    /**
     @MethodName:
     @Description: 切换数据源名称
     @Param:
     @Return:
     @Author: gjq
     @Date: 2024/2/29
     **/
    DataSourceType value() default DataSourceType.MASTER;
}

切面实现
package com.pg.ga.datasource;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName DataSourceAspect
 * @Description:类描述
 * @Author gjq
 * @Date 2024/2/29
 * @Version V1.0
 **/
@Aspect
@Component
@Order(1)
public class DataSourceAspect {
    //先声明切点
    @Pointcut("@annotation(com.pg.ga.datasource.DataSource)")
    public void dsPointCut() {
        System.out.println(11111);
    }

    @Around("dsPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable{
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        DataSource dataSource = method.getAnnotation(DataSource.class);
        if(null!=dataSource){
            DataSourceContextHolder.setDataSource(dataSource.value().name());
        }
        try{
            return point.proceed();
        }finally {
            DataSourceContextHolder.clearDataSource();
        }
    }
}

数据库配置信息.yaml文件配置

server :
  port : 8090
  ##项目名字配置
  #servlet :
  #  context-path : /demo
  tomcat :
    uri-encoding : UTF-8
    #xx 报错修改的地方
    max-connections: 200000
    max-http-form-post-size: 9000000
    threads:
      max: 128
      min-spare: 5
spring :
  # 环境 dev|test|prod
  profiles :
    active : dev
    #引入其他配置文件,例如ftpHX 未配置文件application-ftpHX.yml
    #include: ftpHX,ftpCloud
  servlet:
    multipart:
      #设置总上传的数据大小
      max-request-size: 100MB
      #单个文件大小
      maxFileSize : 30MB
        #xx 报错修改的地方
    max-connections: 200000
    max-http-post-size: 9000000
  #热部署模块
  devtools:
    restart:
      #热部署开关
      enabled: true
      #指定热部署的目录
      additional-paths: src/main/java
      #指定目录不更新
      exclude: test/**
  mvc:   #静态文件
    static-path-pattern : /static/**
    pathmatch:
      matching-strategy: ant_path_matcher
  #模板引擎
  thymeleaf:
    model: HTML5
    prefix: classpath:/templates/
    suffix: .html
    #指定编码
    encoding: utf-8
    #禁用缓存 默认false
    cache: false
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  redis:
    ssl: false
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 
    timeout: 1000
    lettuce:
      pool:
        max-active: 200
        max-wait: -1
        max-idle: 10
        min-idle: 0

# 控制台输出sql、下划线转驼峰
mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml
  typeAliasesPackage: com.pg.ga.model
  # 控制台输出sql、下划线转驼峰
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

#pagehelper分页插件
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
application-dev.yaml配置信息
#dev环境  mysql7.0
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: org.postgresql.Driver
    #druid连接池配置
    druid:
     #主库数据源
     master:
        url: jdbc:postgresql://X.X.X.X:30811/ticket
        username: postgres
        password: postgres
     #备数据源 #关闭
     slave:
        enabled: false
        url: jdbc:postgresql://X.X.X.X:30811/ticket
        username: postgres
        password: postgres
        #配置初始化连接数大小
     initial-size: 10
     # 最大连接数
     maxActive: 50
     #最小连接数
     minIdle: 10
     #获取连接等待超时时间
     maxWait: 5000
     poolPreparedStatements: true #是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
     maxPoolPreparedStatementPerConnection-size: 20
     validationQuery: SELECT 1 FROM DUAL
     validationQueryTimeout: 20000
     testOnBorrow: false #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
     testOnReturn: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
     testWhileIdle: true #建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
     timeBetweenEvictionRunsMillis: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
     minEvictableIdleTimeMillis: 300000  #一个连接在池中最小生存的时间,单位是毫秒
     maxEvictableIdleTimeMillis: 900000  # 配置一个连接在池中最大生存的时间,单位是毫秒
     #StatViewServlet配置。(因为暴露的监控信息比较敏感,支持密码加密和访问ip限定)
     webStatFilter:
      enabled: true
     statViewServlet:
      enabled: true
      urlPattern: /druid/*
      #可以增加访问账号密码【去掉注释就可以】
      #login-username: admin
      #login-password: admin
     filter:
      stat:
        enabled: true
        # 慢SQL记录
        log-slow-sql: true
        slow-sql-millis: 1000
        merge-sql: true
      wall:
        config:
          multi-statement-allow: true
测试controller
package com.pg.ga.controller;

import com.alibaba.fastjson.JSONObject;
import com.pg.ga.model.TicketBall;
import com.pg.ga.service.TickService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/crud")
public class Crud {
    @Autowired
    TickService tickService;

    @GetMapping("/master")
    public String master() {
        TicketBall ticketBall = tickService.selectMaster();
        System.out.println(JSONObject.toJSON(ticketBall));
        return null;
    }

    @GetMapping("/slave")
    public String create() {
        TicketBall ticketBall = tickService.selectSlave();
        System.out.println(JSONObject.toJSON(ticketBall));
        return null;
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/428357.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode148.排序链表

题目 给你链表的头结点 head &#xff0c;请将其按 升序 排列并返回 排序后的链表 。 示例 输入&#xff1a;head [4,2,1,3] 输出&#xff1a;[1,2,3,4] 输入&#xff1a;head [-1,5,3,4,0] 输出&#xff1a;[-1,0,3,4,5] 输入&#xff1a;head [] 输出&#xff1a;[] 思路…

STM32CubeIDE基础学习-新建STM32CubeIDE基础工程

STM32CubeIDE基础学习-新建STM32CubeIDE基础工程 前言 有开发过程序的朋友都清楚&#xff0c;后面开发是不需要再新建工程的&#xff0c;一般都是在初学时或者有特殊需要的时候才需要新建项目工程的。 后面开发都是可以在这种已有的工程上添加相关功能就行&#xff0c;只要前…

sylar高性能服务器-日志(P43-P48)内容记录

文章目录 P43&#xff1a;Hook01一、HOOK定义接口函数指针获取接口原始地址 二、测试 P44-P48&#xff1a;Hook02-06一、hook实现基础二、class FdCtx成员变量构造函数initsetTimeoutgetTimeout 三、class FdManager成员变量构造函数get&#xff08;获取/创建文件句柄类&#x…

华工的各类型PPT模板

华工的各类型PPT模板&#xff0c;包括原创的PPT及改良内容的PPT&#xff0c;适合科研/比赛/组会汇报等 前言各种毕业答辩夏令营答辩复试答辩奖学金答辩比赛/项目答辩组会汇报 前言 设计不易&#xff0c;排版不易&#xff0c;内容编排不易 待更新项目1 原创声明&#xff1a;不经…

17 easy 290. 单词规律

//给定一种规律 pattern 和一个字符串 s &#xff0c;判断 s 是否遵循相同的规律。 // // 这里的 遵循 指完全匹配&#xff0c;例如&#xff0c; pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接的对应规律。 // // // // 示例1: // // //输入: patte…

Kubernetes 学习总结(46)—— Pod 不停重启问题分析与解决

我们在做性能测试的时候&#xff0c;往往会发现我们的pod服务&#xff0c;频繁重启&#xff0c;通过kubectl get pods 命令&#xff0c;我们来逐步定位问题。 现象:running的pod&#xff0c;短时间内重启次数太多。 定位问题方法:查看pod日志 kubectl get event …

攻防世界 php_rce

已经给了开发框架了用的是ThinkPHP V5 所以我们直接搜这个框架爆出来的漏洞就好了 可以得到这里面有个远程rce payload url/index.php?s/index/\think\app/invokefunction&functioncall_user_func_array&vars[0]system&vars[1][]dir 然后我们就可以命令执行了…

【大厂AI课学习笔记NO.63】模型的维护

说是模型的维护&#xff0c;其实这堂课都是在讲“在工业环境中开发和部署机器学习模型的流程”。 上图来自于我的笔记思维脑图&#xff0c;已经上传&#xff0c;要链接的访问的主页查看资源。 一路走来&#xff0c;我们学习了数据管理、模型学习、模型验证、模型部署等重要的步…

Elixir 依赖 (deps) 调试的小技巧

最近使用 Elixir 有点多, 经常需要观察一些依赖 (Deps) 的实现, 比如想加个日志打印点 IO.inspect 啥的观察下某个变量&#xff0c;才能更好的理解某个 Elixir 的依赖。这里介绍下一些调试的方式: 这里以 yeshan333/ex_integration_coveralls 为例子. 我们先 clone 项目到本地…

每日五道java面试题之mysql数据库篇(四)

目录&#xff1a; 第一题&#xff1a; Hash索引和B树所有有什么区别或者说优劣呢?第二题&#xff1a;数据库为什么使用B树而不是B树&#xff1f;第三题&#xff1a;B树在满足聚簇索引和覆盖索引的时候不需要回表查询数据&#xff1f;第四题&#xff1a;什么是聚簇索引&#xf…

案例介绍:汽车维修系统的信息抽取技术与数据治理应用(开源)

一、引言 在当今汽车产业的快速发展中&#xff0c;软件已经成为提升车辆性能、安全性和用户体验的关键因素。从车载操作系统到智能驾驶辅助系统&#xff0c;软件技术的进步正在重塑我们对汽车的传统认知。我有幸参与了一个创新项目&#xff0c;该项目专注于开发和集成先进的汽…

每日一题 — 盛水最多的容器

11. 盛最多水的容器 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a; 因为体积是长度乘高&#xff0c;所以运用双指针&#xff0c;一个在最左&#xff0c;一个在最右&#xff0c;每次都记录体积 V &#xff0c;然后比较左边的长度和右边的长度&#xff0c;左边的长度…

浅析扩散模型与图像生成【应用篇】(五)——SDEdit

5. SDEdit: Guided Image Synthesis and Editing With Stochastic Differential Equations 该文提出一种基于SDE扩散模型的引导图像生成和编辑方法。通过使用者在原图上给出一些引导&#xff0c;比如在图像上涂鸦或者增加一个图块&#xff0c;甚至可以不给定原图&#xff0c;直…

图像剪辑|Linux|ImageMagick的初步使用--素描,毛玻璃等特效

前言&#xff1a; ImageMagick在图像剪辑领域的地位基本等同于FFmpeg&#xff0c;和FFmpeg基本一样&#xff0c;在Linux下使用此工具的原因是该工具可以使用shell脚本批量剪辑&#xff0c;在Windows下就会比较麻烦一些了 那么&#xff0c;本文主要是记录一下ImageMagick的一些…

简单聊聊http协议头参数之Content-Type和http状态码 415错误

大家好&#xff0c;我是G探险者。 今天聊一下http的状态码&#xff0c;415错误&#xff0c;因为项目里面使用了httpclient进行了远程服务调用&#xff0c;调用发送时&#xff0c;会有一个http header的参数设置。由于参数设置的问题经常会出现错误&#xff0c;导致调用失败&am…

基于51单片机微波炉简易控制仿真设计数码管显示proteus仿真+程序+设计报告+讲解视频)

基于51单片机微波炉简易控制仿真设计数码管显示 1. 主要功能&#xff1a;2. 讲解视频&#xff1a;3. 仿真4. 程序代码延时函数定时器初始化定时器中断产生PWM显示函数 5. 设计报告6. 设计资料内容清单&&下载链接资料下载链接&#xff1a; 基于51单片机微波炉简易控制仿…

xfce任务栏图标挤到一起了

分隔符&#xff0c;扩展&#xff0c;撑开任务栏

2024东南大学553复试真题及笔记

2023年真题知识点 引用指针 题目为 传递一个指针的引用做修改&#xff0c;输出指针指向的结果&#xff0c;但是指针被修改&#xff0c;结果就不一样了。 static 静态变量 类里面的静态成员变量&#xff0c;很简单的题目 for循环 看循环的内容输出字符串 try try catch捕…

Launch学习

参考博客&#xff1a; (1) 史上最全的launch的解析来啦&#xff0c;木有之一欧 1 ROS工作空间简介 2 元功能包 src目录下可以包含多个功能包&#xff0c;假设需要使用机器人导航模块&#xff0c;但是这个模块中包含着地图、定位、路径规划等不同的功能包&#xff0c;它们的逻…

Vue3和ElementPlus封装table组件

最近学习vue3.2并自己在写一个项目&#xff0c;然后发现好几个页面都是列表页&#xff0c;重复写table和column也是觉得累&#xff0c;学习的项目列表页不算多&#xff0c;要是公司项目就不一样了&#xff0c;所以就想着自己封装一个table组件&#xff0c;免去大量重复工作和co…
最新文章