jdk17 SpringBoot JPA集成多数据库的示例详解
作者:宁漂打工仔
这篇文章主要介绍了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 @Primary public 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(); } @Override protected 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 { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { String region = request.getHeader(HeaderCons.X_REGION); if (region != null) { RequestContextHolder.setRegion(region); } return true; } @Override public 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 { @Override public 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.Driver spring.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.Driver spring.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
到此这篇关于jdk17 SpringBoot JPA集成多数据库的文章就介绍到这了,更多相关jdk17 SpringBoot JPA集成多数据库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!