SpringCloud之nacos共享配置文件实现多数据源灵活切换
目录
前言
1.引入Springboot相关的aop切面依赖
2.创建自定义注解@DataSourceKey
3.创建对ThreadLocal类
4.创建aop切面
5.创建动态数据源类
6.创建多数据库连接配置类
7.关键代码讲解
8.nacos主要配置
前言
通过Spring AOP(面向切面编程)的功能来动态地切换数据源。使用@Aspect和@Component注解,通过切面扫描自定义注解,获取数据源的key,可以在不修改原有业务代码的情况下,在service里面的类方法中加入@DataSourceKey注解,即可访问指定的数据源。
1.引入Springboot相关的aop切面依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>
2.创建自定义注解@DataSourceKey
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSourceKey {//默认使用auth数据库String value() default "dataSourceSystem";
}
3.创建对ThreadLocal类
通过线程隔离的方式,实现数据源的切换。
package com.example.auth.datasource;/*** 数据库上下文切换对象,针对每个线程做不同操作*/
public class DataSourceContextHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();public static void setDataSourceKey(String dataSourceKey) {contextHolder.set(dataSourceKey);}public static String getDataSourceKey() {return contextHolder.get();}public static void clearDataSourceKey() {contextHolder.remove();}
}
4.创建aop切面
通过包扫描动态切换数据源,主要通过扫描注解的方式获取数据源的key值,即数据源名称。
package com.example.auth.datasource;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/*** 多数据源切面*/
@Aspect
@Component
public class DatasourceAspect {private Logger logger = LoggerFactory.getLogger(DatasourceAspect.class);@Before("@annotation(dataSourceKey) && execution(* com.example.auth.datasource.*.*(..))")public void beforeSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {String key = dataSourceKey.value();logger.info("key:{}",key);DataSourceContextHolder.setDataSourceKey(key);}@Before("@annotation(dataSourceKey) && execution(* com.example.auth.service.*.*(..))")public void beforeServiceSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {String key = dataSourceKey.value();logger.info("key:{}",key);DataSourceContextHolder.setDataSourceKey(key);}@After("@annotation(dataSourceKey) && execution(* com.example.auth.service.*.*(..))")public void afterServiceSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {String key = dataSourceKey.value();logger.info("key:{}",key);DataSourceContextHolder.clearDataSourceKey();}@After("@annotation(dataSourceKey) && execution(* com.example.auth.datasource.*.*(..)) ")public void afterSwitchDataSource(JoinPoint joinPoint, DataSourceKey dataSourceKey) {logger.info("key:{}",dataSourceKey.value());DataSourceContextHolder.clearDataSourceKey();}}
5.创建动态数据源类
通过该类,可动态改变数据源名称。
package com.example.auth.datasource;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class DynamicDataSource extends AbstractRoutingDataSource {private Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);@Overrideprotected Object determineCurrentLookupKey() {String key = DataSourceContextHolder.getDataSourceKey();logger.info("数据源:{}",key);return DataSourceContextHolder.getDataSourceKey();}
}
6.创建多数据库连接配置类
package com.example.auth.datasource;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.activation.DataContentHandler;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;/*** 多数据源配置*/
@Configuration
@MapperScan(basePackages = "com.example.auth.mapper")
public class MultiDataSourceConfig {private Logger logger = LoggerFactory.getLogger(MultiDataSourceConfig.class);@Autowiredprivate DataSource dataSourceAuth;@Autowiredprivate DataSource dataSourceConsumer;@Autowiredprivate DataSource dataSourceMq;@Autowiredprivate DataSource dataSourceGateway;@Autowiredprivate DataSource dataSourceSystem;@Autowired@Qualifier("dynamicDataSource")private DataSource dynamicDataSource;@Autowiredprivate StringEncryptor stringEncryptor;@Bean(name = "dataSourceAuth")@ConfigurationProperties(prefix = "spring.datasource.auth")public DataSource dataSourceAuth() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceConsumer")@ConfigurationProperties(prefix = "spring.datasource.consumer")public DataSource dataSourceConsumer() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceMq")@ConfigurationProperties(prefix = "spring.datasource.mq")public DataSource dataSourceMq() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceGateway")@ConfigurationProperties(prefix = "spring.datasource.gateway")public DataSource dataSourceGateway() {return DataSourceBuilder.create().build();}@Bean(name = "dataSourceSystem")@ConfigurationProperties(prefix = "spring.datasource.system")public DataSource dataSourceSystem() {return DataSourceBuilder.create().build();}@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());//注册乐观锁插件interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}@Beanpublic StringEncryptor stringEncryptor() {PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword("encryptionkey"); // 加密密钥config.setAlgorithm("PBEWithHmacSHA512AndAES_256");config.setKeyObtentionIterations("1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);return encryptor;}@PostConstructpublic void init(){/* String enStr = stringEncryptor.encrypt("Root@123");String deSTr = stringEncryptor.decrypt("N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6");System.out.println("enStr==="+enStr);System.out.println("deSTr==="+deSTr);*/}/*** 不加* @param interceptor* @return* @throws Exception*/@Beanpublic SqlSessionFactory sqlSessionFactory (MybatisPlusInterceptor interceptor) throws Exception {MybatisSqlSessionFactoryBean ssfb = new MybatisSqlSessionFactoryBean();ssfb.setDataSource(dynamicDataSource); // 使用 DynamicDataSourcessfb.setPlugins(interceptor);ssfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*Mapper.xml"));return ssfb.getObject();}@Beanpublic DataSource dynamicDataSource() {DynamicDataSource dynamicDataSource = new DynamicDataSource();// 假设你有多个数据源,需要在这里将它们添加到 targetDataSources 中Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put("dataSourceSystem", dataSourceSystem);targetDataSources.put("dataSourceAuth", dataSourceAuth);targetDataSources.put("dataSourceConsumer", dataSourceConsumer);targetDataSources.put("dataSourceMq", dataSourceMq);targetDataSources.put("dataSourceGateway",dataSourceGateway);dynamicDataSource.setTargetDataSources(targetDataSources);dynamicDataSource.setDefaultTargetDataSource(dataSourceSystem);// 设置默认数据源return dynamicDataSource;}}
7.关键代码讲解
注入dynamicDataSource实体,通过该实体bean动态获取数据源,从而达到随意切换数据源的目的。
单个dataSource的注入,如 dataSourceAuth,主要是给动态数据源的切换提前准备多数据源。
8.nacos主要配置
spring:datasource:system: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/system?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000auth: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/auth?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000consumer: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/consumer?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000mq: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/mq?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000gateway: driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/gateway?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&nullCatalogMeansCurrent=trueusername: rootpassword: ENC(N8VBWG5nOHvy5efX3/mlPAmdBykE7iDZFl362LyeaPRXMbLT0PzEIlB/KDXrNYz6)type: com.alibaba.druid.pool.DruidDataSourcedruid:initial-size: 5min-idle: 1max-active: 10max-wait: 60000validation-query: SELECT 1 FROM DUALtest-on-borrow: falsetest-on-return: falsetest-while-idle: truetime-between-eviction-runs-millis: 60000
相关文章:
SpringCloud之nacos共享配置文件实现多数据源灵活切换
目录 前言 1.引入Springboot相关的aop切面依赖 2.创建自定义注解DataSourceKey 3.创建对ThreadLocal类 4.创建aop切面 5.创建动态数据源类 6.创建多数据库连接配置类 7.关键代码讲解 8.nacos主要配置 前言 通过Spring AOP(面向切面编程)的功能来动…...
原生小程序生成二维码方法之一
效果图: 第一步:下载对应的包并构建(工具---》构建npm) npm install weapp-qrcode --save 第二步:在wxml页面声明canvas <canvas style"width: 200px; height: 200px;margin:0 auto;" canvas-id"myQ…...
Kubernetes k8s Pod容器 探针 健康探测
目录 Pod容器健康探测 为什么要对容器做探测? 启动探测startupprobe 存活性探测livenessProbe 就绪性探测readinessProbe ReadinessProbe LivenessProbe startupProbe配合使用示例一般程序中需要设置三种探针结合使用,并且也要结合实际情况ÿ…...
Conformal low power-2.电源感知等效性检查
电源感知等效性检查 ■ 第24页:电源感知等效性检查概述 ■ 第24页:启动低功耗(等效性检查)软件 ■ 第25页:电源感知等效性检查流程 ■ 第28页:电源感知等效性检查示例Do文件 电源感知等效性检查概述…...
【密码学】从有限状态自动机到密钥流生成器
本文是对流密码内容的拓展,在流密码中种子密钥通过一个伪随机数生成器产生一个与明文等长的伪随机密钥流。而本文的内容就是在回答这样两个问题: 伪随机密钥流是如何生成的?流密码、流密钥生成器和有限状态自动机之间是什么关系?…...
3.相机标定原理及代码实现(opencv)
1.相机标定原理 相机参数的确定过程就叫做相机标定。 1.1 四大坐标系及关系 (1)像素坐标系(单位:像素(pixel)) 像素坐标系是指相机拍到的图片的坐标系,以图片的左上角为坐标原点&a…...
Centos7 安装Docker步骤及报错信息(不敢说最全,但是很全)
一、操作系统要求: 要安装Docker Engine,您需要CentOS 7及以上的维护版本。存档版本不受支持或测试。必须启用centos临时存储库。默认情况下,此存储库已启用,但如果已禁用,则需要重新启用它。建议使用overlay2存储驱动…...
【C语言】符号优先级详解
C语言符号优先级详细解析 在C语言中,不同的运算符具有不同的优先级和结合性,这决定了在表达式中运算符的计算顺序。理解这些优先级和结合性是正确编写和理解C语言程序的基础。本文将详细解析C语言中的符号优先级,包括各类运算符的优先级、结…...
天翼云高级运维工程师202407回忆题库 最新出炉
备考天翼云高级运维工程师 必须备考天翼云 之前觉得外企牛批 然后民企,拔地而起,民企也不错,工资高,有钱途 现在看来看去,还是国企好,体制内的,有保障,树大根深 有必要备考下天…...
在Python中什么是上下文管理器以及如何使用with语句来管理资源
什么是上下文管理器? 在Python中,上下文管理器(Context Manager)是一种支持with语句的协议,允许对象管理资源,如文件、线程锁的获取和释放、数据库连接等。上下文管理器负责资源的分配和释放,确…...
(四)、python程序--贪吃蛇游戏
一、绪论 贪吃蛇游戏。 已实现功能: 1、上下左右移动; 2、吃食物,随机生成食物; 3、碰撞检测,判断是否游戏结束。 二、代码分享 1、main.py import pygame import sys import food as c_food import snake as c…...
什么是DNS欺骗
DNS欺骗(DNS Spoofing),也称为DNS缓存中毒(DNS Cache Poisoning),是一种网络攻击形式,攻击者通过操纵DNS记录,将用户重定向到一个伪造的、恶意的网站。这些恶意网站可能看起来与用户…...
C++实现对结构体信息排序
思路解读: 定义结构体 Student: 结构体 Student 用来表示学生信息,包含两个成员变量:name(学生姓名)和 score(学生分数)。Student 结构体定义了一个构造函数,用于初始化 name 和 sco…...
[CTF]-PWN:House of Cat堆题型综合解析
原理: 调用顺序: exit->_IO_wfile_jumps->_IO_wfile_seekoff->_IO_switch_to_wget_mode _IO_wfile_seekoff源码: off64_t _IO_wfile_seekoff (FILE *fp, off64_t offset, int dir, int mode) {off64_t result;off64_t delta, new…...
18.按键消抖模块设计(使用状态机,独热码编码)
(1)设计意义:按键消抖主要针对的时机械弹性开关,当机械触点断开、闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子就断开。因而在闭合以及断开的瞬…...
【Hec-HMS】第一期:模型简介及软件安装
HEC-HMS模型简介及软件安装 HEC-HMS模型简介建模思路 HEC-HMS软件安装步骤1:安装InstallShield Wizard步骤2:安装HEC-HMS 参考 HEC-HMS模型简介 HEC-HMS(The Hydrologic Engineering Center’s-Hydrologic Modelimng System),美国陆军工程兵…...
逻辑回归不是回归吗?那为什么叫回归?
RNN 逻辑回归不是回归吗?那为什么叫回归?逻辑回归的基本原理逻辑函数(Sigmoid函数)二元分类 为什么叫做“回归”?逻辑回归的应用场景总结 逻辑回归不是回归吗?那为什么叫回归? 逻辑回归&#x…...
Activity对象的部分常见成员变量
在Android开发中,Activity 类是一个非常重要的类,它代表了应用程序中的一个屏幕。每个Activity都有一系列的成员变量和方法,这些成员变量通常用于控制和管理活动生命周期、UI界面元素、应用资源等。虽然具体的成员变量会根据Android的不同版本…...
量化交易策略:赌徒在股市会运用凯利公式(附python代码)
一、凯利公式的历史 凯利公式(Kelly Criterion)是由美国贝尔实验室物理学家约翰拉里凯利(John Larry Kelly)于1956年提出的,用于计算最优投资比例的一种数学公式。凯利公式的核心思想是:在期望收益和风险之间找到一个平衡点,使得投资者在承担一定风险的情况下,能够获得…...
信息系统项目管理师【一】英文选择题词汇大全(1)
一、计算机相关词汇 数据挖掘 Data Mining分布式计算 Distributed Computing云计算 Cloud Computing物联网 IOT Internet of Things大数据 Big Data人工智能 artificial intelligence互联网 Internet plus区块链 Blockchain5G 5th-Generation感知层 sensing layer机器学习 mac…...
如何轻松掌握开源OCR插件的实用技巧:5步快速上手指南
如何轻松掌握开源OCR插件的实用技巧:5步快速上手指南 【免费下载链接】Umi-OCR_plugins Umi-OCR 插件库 项目地址: https://gitcode.com/gh_mirrors/um/Umi-OCR_plugins 你是否曾被纸质文档的数字化问题困扰?或者需要从图片中提取数学公式却找不到…...
NeuroSynth脑成像元分析:Python神经影像数据处理终极指南
NeuroSynth脑成像元分析:Python神经影像数据处理终极指南 【免费下载链接】neurosynth Neurosynth core tools 项目地址: https://gitcode.com/gh_mirrors/ne/neurosynth NeuroSynth是一个功能强大的Python包,专门用于大规模功能性神经影像数据的…...
TEdit地图编辑器:从零开始掌握泰拉瑞亚世界创作
TEdit地图编辑器:从零开始掌握泰拉瑞亚世界创作 【免费下载链接】Terraria-Map-Editor TEdit - Terraria Map Editor - TEdit is a stand alone, open source map editor for Terraria. It lets you edit maps just like (almost) paint! It also lets you change w…...
第七部分-容器安全与监控——33. 镜像安全
33. 镜像安全 1. 镜像安全概述 镜像是容器的基石,镜像安全问题直接影响容器运行时安全。镜像安全涵盖基础镜像选择、镜像构建过程、镜像存储和分发等环节。 ┌─────────────────────────────────────────────────…...
AI编程助手配置统一管理:code-agnostic实现多编辑器配置同步
1. 项目概述:告别配置碎片化,一个中心管理所有AI编辑器如果你和我一样,同时在使用Cursor、OpenCode、Codex甚至Claude Code这些AI编程助手,那你一定对配置管理的混乱深有体会。每个编辑器都有一套自己的配置格式和存放位置&#x…...
一次搞清楚:Agent、Skill、Prompt、MCP
文章深入探讨了AI Agent在落地过程中面临的三大核心痛点:Prompt的临时性与不可复用性、Agent专业能力的难以沉淀与迁移、以及AI能力无法融入现有工程化流程。文章提出Agent Skills作为AI Agent的专业能力说明书,通过标准化能力描述与执行框架,…...
如何轻松完成ESP8266固件烧录:NodeMCU PyFlasher图形化工具详解
如何轻松完成ESP8266固件烧录:NodeMCU PyFlasher图形化工具详解 【免费下载链接】nodemcu-pyflasher Self-contained NodeMCU flasher with GUI based on esptool.py and wxPython. 项目地址: https://gitcode.com/gh_mirrors/no/nodemcu-pyflasher NodeMCU …...
从零手写CNN:理解卷积网络的生物学原理与工程逻辑
1. 项目概述:从人眼到机器之眼,一次真实的视觉理解之旅你有没有盯着一张照片发过呆?比如朋友刚发来的旅行照——蓝天、雪山、一只歪头的雪豹。你几乎是一瞬间就认出了“雪豹”,甚至能判断它“在看镜头”“毛很厚”“可能刚睡醒”。…...
反AI招聘平台hire-humans:重塑以人为本的招聘体验
1. 项目概述:当AI遇见“真人”招聘最近在GitHub上看到一个挺有意思的项目,叫“hire-humans”。光看名字,你可能会觉得这是个普通的招聘网站模板或者人力资源工具。但点进去仔细琢磨,你会发现它的内核远不止于此。这个项目本质上是…...
从内核恐慌到系统恢复:一次NMI watchdog触发的soft lockup深度诊断
1. 当服务器突然卡死:从NMI watchdog错误说起 那天下午3点,机房警报突然响起。我冲到服务器前,屏幕上赫然显示着刺眼的红色错误:"NMI watchdog: BUG: soft lockup - CPU#2 stuck for 23s!"。这台承载着核心业务的服务器…...
