switchRegion(切换地区)功能, 客户端可手动切换地区 , 查询不同的数据库, 后台根据地区切换数据库, 请求头添加region的key文章来源地址https://www.toymoban.com/news/detail-633951.html
配置类
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.test.test.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.test.test.component.context.RequestContextHolder;
import com.test.test.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.test.test.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/test_us
spring.datasource.us.pool-name=test_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/test_ca
spring.datasource.ca.pool-name=test_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/test_other
spring.datasource.other.pool-name=test_other
spring.datasource.other.username=postgres
spring.datasource.other.password=123
spring.datasource.other.driver-class-name=org.postgresql.Driver
文章来源:https://www.toymoban.com/news/detail-633951.html
到了这里,关于jdk17 SpringBoot JPA集成多数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!