jdk17 SpringBoot JPA集成多数据库

switchRegion(切换地区)功能, 客户端可手动切换地区 , 查询不同的数据库, 后台根据地区切换数据库, 请求头添加region的key

配置类


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;/*** @author Wang*/
@Configuration
public class DynamicDataSourceConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource.us")public DataSource usDataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix = "spring.datasource.ca")public DataSource caDataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix = "spring.datasource.other")public DataSource otherDataSource() {return DataSourceBuilder.create().build();}@Bean@Primarypublic DynamicDataSource dataSource(DataSource usDataSource, DataSource caDataSource, DataSource otherDataSource) {Map<Object, Object> targetDataSources = new HashMap<>(3);targetDataSources.put(DataSourceEnum.US.toString().toLowerCase(), usDataSource);targetDataSources.put(DataSourceEnum.CA.toString().toLowerCase(), caDataSource);targetDataSources.put(DataSourceEnum.OTHER.toString().toLowerCase(), otherDataSource);DynamicDataSource dynamicDataSource = new DynamicDataSource(usDataSource, targetDataSources);dynamicDataSource.afterPropertiesSet();return dynamicDataSource;}
}

import com.woodare.cdw.component.context.RequestContextHolder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import javax.sql.DataSource;
import java.util.Map;/*** @author Wang*/
public class DynamicDataSource extends AbstractRoutingDataSource {public DynamicDataSource(DataSource firstDataSource, Map<Object, Object> targetDataSources) {setDefaultTargetDataSource(firstDataSource);setTargetDataSources(targetDataSources);afterPropertiesSet();}@Overrideprotected Object determineCurrentLookupKey() {return RequestContextHolder.getRegion();}
}
public enum DataSourceEnum {/*** region*/US,CA,OTHER;
}

请求拦截器

import com.woodare.cdw.component.context.RequestContextHolder;
import com.woodare.cdw.core.HeaderCons;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;/*** @author Wang*/
public class RequestInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {String region = request.getHeader(HeaderCons.X_REGION);if (region != null) {RequestContextHolder.setRegion(region);}return true;}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {RequestContextHolder.clearRegion();}
}

import com.woodare.cdw.interceptor.RequestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** @author Wang*/
@Configuration
public class ResourceConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new RequestInterceptor());}
}

线程上下文

/*** @author Wang*/
public class RequestContextHolder {private RequestContextHolder(){}private static final ThreadLocal<String> REQUEST_HOLDER = new ThreadLocal<>();public static String getRegion() {return REQUEST_HOLDER.get();}public static void setRegion(String region) {REQUEST_HOLDER.set(region);}public static void clearRegion() {REQUEST_HOLDER.remove();}
}

application 配置

#jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true# PostgreSQL
spring.datasource.us.jdbc-url=jdbc:postgresql://127.0.0.1:5432/cdw_us
spring.datasource.us.pool-name=cdw_us
spring.datasource.us.username=postgres
spring.datasource.us.password=123
spring.datasource.us.driver-class-name=org.postgresql.Driverspring.datasource.ca.jdbc-url=jdbc:postgresql://127.0.0.1:5432/cdw_ca
spring.datasource.ca.pool-name=cdw_ca
spring.datasource.ca.username=postgres
spring.datasource.ca.password=123
spring.datasource.ca.driver-class-name=org.postgresql.Driverspring.datasource.other.jdbc-url=jdbc:postgresql://127.0.0.1:5432/cdw_other
spring.datasource.other.pool-name=cdw_other
spring.datasource.other.username=postgres
spring.datasource.other.password=123
spring.datasource.other.driver-class-name=org.postgresql.Driver

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络—TCP

这里写目录标题 TCP头格式有哪些为什么需要TCP&#xff0c;TCP工作在哪什么是TCP什么是TCP连接如何确定一个TCP连接TCP和UDP的区别&#xff0c;以及场景TCP和UDP能共用一个端口&#xff1f;TCP的建立TCP三次握手过程为什么是三次握手、不是两次、四次why每次建立连接&#xff0…

2023年游戏买量能怎么玩?

疫情过后&#xff0c;一地鸡毛。游戏行业的日子也不好过。来看看移动游戏收入&#xff1a;2022年&#xff0c;移动游戏收入达到920亿美元&#xff0c;同比下降6.4%。这告诉我们&#xff0c;2022年对移动游戏市场来说是一个小挫折。 但不管是下挫还是上升&#xff0c;移动游戏市…

python技术栈 之 单元测试中mock的使用

一、什么是mock&#xff1f; mock测试就是在测试过程中&#xff0c;对于某些不容易构造或者不容易获取的对象&#xff0c;用一个虚拟的对象来创建以便测试的测试方法。 二、mock的作用 特别是开发过程中上下游未完成的工序导致当前无法测试&#xff0c;需要虚拟某些特定对象…

机器学习深度学习——RNN的从零开始实现与简洁实现

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位即将上大四&#xff0c;正专攻机器学习的保研er &#x1f30c;上期文章&#xff1a;机器学习&&深度学习——循环神经网络RNN &#x1f4da;订阅专栏&#xff1a;机器学习&&深度学习 希望文章对你们有所帮…

React实现关键字高亮

先看效果&#xff1a; 实现很简单通过以下这个函数&#xff1a; highLight (text, keyword ) > {return text.split(keyword).flatMap(str > [<span style{{ color: red, fontWeight: bold }}>{keyword}</span>, str]).slice(1);}展示某段文本时调用该函数…

完成图像反差处理

bmp图像的前54字节为图像头&#xff0c;第19个字节开始4字节为图像宽&#xff0c;第23字节开始4字节为图像高&#xff0c;图像大小为&#xff1a;972*720*3542099574&#xff0c;为宽*高*像素点头&#xff0c;如下&#xff1a; 图像的反差处理

Android系统-ServiceManager2

目录 引言&#xff1a; 获取ServiceManager 流程图 注册系统服务 获取系统服务 引言&#xff1a; 注册或使用服务之前&#xff0c;需要通过ServiceManager这个DNS来找到对应的服务。那怎么找到ServiceManager呢&#xff1f; 怎么注册系统服务&#xff1f; 怎么获取系统…

Golang 函数定义及使用

文章目录 一、函数定义格式二、函数定义及使用 一、函数定义格式 //func: 函数定义关键字 //function_name&#xff1a;函数名称 //parameter_List: 函数参数列表&#xff0c;多个时使用逗号拆分 //return_types&#xff1a;函数返回类型&#xff0c;返回多个值时使用逗号拆分…

高等数学:泰勒公式

注&#xff1a;第三条 e x e^x ex的展开式&#xff0c;在 1 1 1和 1 2 x 2 \frac{1}{2}x^2 21​x2之间添上一个 x x x。 1 1 − x ∑ n 0 ∞ x n 1 x x 2 x 3 ο ( x 3 ) , x ∈ ( − 1 , 1 ) . \begin{aligned}\frac{1}{1-x}\sum_{n0}^\infty x^n1xx^2x^3\omicron(x^…

SpringBoot 2.1.7.RELEASE + Activiti 5.18.0 喂饭级练习手册

环境准备 win10 eclipse 2023-03 eclipse Activiti插件 Mysql 5.x Activiti的作用等不再赘叙&#xff0c;直接上代码和细节 POM <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId>…

web前端之CSS操作

文章目录 一、CSS操作1.1 html元素的style属性1.2 元素节点的style属性1.3 cssText属性 二、事件2.1 事件处理程序2.1.1 html事件2.1.2 DOM0事件&#xff08;适合单个事件&#xff09;2.1.3 DOM2事件&#xff08;适合多个事件&#xff09; 2.2 事件之鼠标事件2.3 事件之Event事…

Python分享之 Spider

一、网络爬虫 网络爬虫又被称为网络蜘蛛&#xff0c;我们可以把互联网想象成一个蜘蛛网&#xff0c;每一个网站都是一个节点&#xff0c;我们可以使用一只蜘蛛去各个网页抓取我们想要的资源。举一个最简单的例子&#xff0c;你在百度和谷歌中输入‘Python&#xff0c;会有大量和…

网络爬虫选择代理IP的标准

Hey&#xff0c;小伙伴们&#xff01;作为一家http代理产品供应商&#xff0c;我知道网络爬虫在选择代理IP时可能会遇到些问题&#xff0c;毕竟市面上有很多选择。别担心&#xff01;今天我要给大家分享一些实用的建议&#xff0c;帮助你们选择适合网络爬虫的代理IP。一起来看看…

选择最适合自己的笔记本

选择最适合自己的笔记本电脑 一、了解笔记本品牌一线品牌准一线品牌二线品牌三线品牌 二、笔记本入手渠道笔记本入手渠道 三、根据需求选择机型使用需求1.日常使用2.商务办公、财务3.轻度剪辑、ps4.代码5.创意设计6.游戏 四、笔记本电脑配置如何选1.cpu2.显卡&#xff08;GPU&a…

Vue响应式数据的原理

在 vue2 的响应式中&#xff0c;存在着添加属性、删除属性、以及通过下标修改数组&#xff0c;但页面不会自动更新的问题。而这些问题在 vue3 中都得以解决。 vue3 采用了 proxy 代理&#xff0c;用于拦截对象中任意属性的变化&#xff0c;包括&#xff1a;属性的读写、属性的…

UTONMOS:元宇宙在网络游戏领域得到充分运用

元宇宙到底是个啥&#xff1f;这个词大概意思应该就是人类能从真实世界进入一个虚拟世界体验另一种生活&#xff0c;这个虚拟的世界就叫“元宇宙”。 从科幻走入现实&#xff0c;元宇宙究竟有什么用途&#xff1f;它离我们到底还有多远&#xff1f;又将给我们的生活带来哪些变…

EVE-NG 配置 静态IP

打开interfaces 配置文件 vi /etc/network/interfaces 将pent0 的dhcp 修改为 static&#xff0c;并添加IP&#xff0c;掩码&#xff0c;网关和DNS # The primary network interface iface eth0 inet manual auto pnet0 #iface pnet0 inet dhcp iface pnet0 inet staticpre-u…

“深入解析JVM:Java虚拟机原理和内部结构“

标题&#xff1a;深入解析JVM&#xff1a;Java虚拟机原理和内部结构 摘要&#xff1a;本文将深入解析JVM&#xff08;Java虚拟机&#xff09;的原理和内部结构。我们将从JVM的基础概念开始&#xff0c;逐步介绍其组成部分&#xff0c;包括类加载器、运行时数据区、字节码解释器…

微服务——数据同步

问题分析 mysql和redis之间有数据同步问题&#xff0c;ES和mysql之间也有数据同步问题。 单体项目可以在crud时就直接去修改&#xff0c;但在微服务里面不同的服务不行。 方案一 方案二 方案三 总结 导入酒店管理项目 倒入完成功启动后可以看见数据成功获取到了 声明队列和…

【STM32】FreeRTOS开启后,不再进入主函数的while(1)

开启freertos后&#xff0c;想在主函数的while(1)中实现led的翻转&#xff0c;发现无法实现。 int main(void) {/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, …