基于多数据源零代码同时生成多个数据库CRUD增删改查RESTful API接口
多数据源
回顾
通过前面文章的介绍,目前已经支持主流数据库,包括MySql,PostgreSql,Oracle,Microsoft SQL Server等,通过配置零代码实现了CRUD增删改查RESTful API。采用抽象工厂设计模式,可以无缝切换不同类型的数据库。
但是如果需要同时支持不同类型的数据库,如何通过配置进行管理呢?这时候引入多数据源功能就很有必要了。
简介
利用spring boot多数据源功能,可以同时支持不同类型数据库mysql,oracle,postsql,sql server等,以及相同类型数据库不同的schema。零代码同时生成不同类型数据库增删改查RESTful api,且支持同一接口中跨库数据访问二次开发。
UI界面
配置一个数据源,多个从数据源,每一个数据源相互独立配置和访问。
核心原理
配置数据库连接串
配置application.properties,spring.datasource为默认主数据源,spring.datasource.hikari.data-sources[]数组为从数据源
#primary
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/crudapi?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
#postgresql
spring.datasource.hikari.data-sources[0].postgresql.driverClassName=org.postgresql.Driver
spring.datasource.hikari.data-sources[0].postgresql.url=jdbc:postgresql://localhost:5432/crudapi
spring.datasource.hikari.data-sources[0].postgresql.username=postgres
spring.datasource.hikari.data-sources[0].postgresql.password=postgres
#sqlserver
spring.datasource.hikari.data-sources[1].sqlserver.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.hikari.data-sources[1].sqlserver.url=jdbc:sqlserver://localhost:1433;SelectMethod=cursor;DatabaseName=crudapi
spring.datasource.hikari.data-sources[1].sqlserver.username=sa
spring.datasource.hikari.data-sources[1].sqlserver.password=Mssql1433
#oracle
spring.datasource.hikari.data-sources[2].oracle.url=jdbc:oracle:thin:@//localhost:1521/XEPDB1
spring.datasource.hikari.data-sources[2].oracle.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.hikari.data-sources[2].oracle.username=crudapi
spring.datasource.hikari.data-sources[2].oracle.password=crudapi
#mysql
spring.datasource.hikari.data-sources[3].mysql.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.data-sources[3].mysql.url=jdbc:mysql://localhost:3306/crudapi2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.hikari.data-sources[3].mysql.username=root
spring.datasource.hikari.data-sources[3].mysql.password=root
动态数据源——DynamicDataSource
Spring boot提供了抽象类AbstractRoutingDataSource,复写接口determineCurrentLookupKey, 可以在执行查询之前,设置使用的数据源,从而实现动态切换数据源。
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
数据源Context——DataSourceContextHolder
默认主数据源名称为datasource,从数据源名称保存在ThreadLocal变量CONTEXT_HOLDER里面,ThreadLocal叫做线程变量, 意思是ThreadLocal中填充的变量属于当前线程, 该变量对其他线程而言是隔离的, 也就是说该变量是当前线程独有的变量。
在RestController里面根据需要提前设置好当前需要访问的数据源key,即调用setDataSource方法,访问数据的时候调用getDataSource方法获取到数据源key,最终传递给DynamicDataSource。
public class DataSourceContextHolder {
//默认数据源primary=dataSource
private static final String DEFAULT_DATASOURCE = "dataSource";
//保存线程连接的数据源
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
private static final ThreadLocal<String> HEADER_HOLDER = new ThreadLocal<>();
public static String getDataSource() {
String dataSoure = CONTEXT_HOLDER.get();
if (dataSoure != null) {
return dataSoure;
} else {
return DEFAULT_DATASOURCE;
}
}
public static void setDataSource(String key) {
if ("primary".equals(key)) {
key = DEFAULT_DATASOURCE;
}
CONTEXT_HOLDER.set(key);
}
public static void cleanDataSource() {
CONTEXT_HOLDER.remove();
}
public static void setHeaderDataSource(String key) {
HEADER_HOLDER.set(key);
}
public static String getHeaderDataSource() {
String dataSoure = HEADER_HOLDER.get();
if (dataSoure != null) {
return dataSoure;
} else {
return DEFAULT_DATASOURCE;
}
}
}
动态数据库提供者——DynamicDataSourceProvider
程序启动时候,读取配置文件application.properties中数据源信息,构建DataSource并通过接口setTargetDataSources设置从数据源。数据源的key和DataSourceContextHolder中key一一对应
@Component
@EnableConfigurationProperties(DataSourceProperties.class)
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public class DynamicDataSourceProvider implements DataSourceProvider {
@Autowired
private DynamicDataSource dynamicDataSource;
private List<Map<String, DataSourceProperties>> dataSources;
private Map<Object,Object> targetDataSourcesMap;
@Resource
private DataSourceProperties dataSourceProperties;
private DataSource buildDataSource(DataSourceProperties prop) {
DataSourceBuilder<?> builder = DataSourceBuilder.create();
builder.driverClassName(prop.getDriverClassName());
builder.username(prop.getUsername());
builder.password(prop.getPassword());
builder.url(prop.getUrl());
return builder.build();
}
@Override
public List<DataSource> provide() {
Map<Object,Object> targetDataSourcesMap = new HashMap<>();
List<DataSource> res = new ArrayList<>();
if (dataSources != null) {
dataSources.forEach(map -> {
Set<String> keys = map.keySet();
keys.forEach(key -> {
DataSourceProperties properties = map.get(key);
DataSource dataSource = buildDataSource(properties);
targetDataSourcesMap.put(key, dataSource);
});
});
//更新dynamicDataSource
this.targetDataSourcesMap = targetDataSourcesMap;
dynamicDataSource.setTargetDataSources(targetDataSourcesMap);
dynamicDataSource.afterPropertiesSet();
}
return res;
}
@PostConstruct
public void init() {
provide();
}
public List<Map<String, DataSourceProperties>> getDataSources() {
return dataSources;
}
public void setDataSources(List<Map<String, DataSourceProperties>> dataSources) {
this.dataSources = dataSources;
}
public List<Map<String, String>> getDataSourceNames() {
List<Map<String, String>> dataSourceNames = new ArrayList<Map<String, String>>();
Map<String, String> dataSourceNameMap = new HashMap<String, String>();
dataSourceNameMap.put("name", "primary");
dataSourceNameMap.put("caption", "主数据源");
dataSourceNameMap.put("database", parseDatabaseName(dataSourceProperties));
dataSourceNames.add(dataSourceNameMap);
if (dataSources != null) {
dataSources.forEach(map -> {
Set<Map.Entry<String, DataSourceProperties>> entrySet = map.entrySet();
for (Map.Entry<String, DataSourceProperties> entry : entrySet) {
Map<String, String> t = new HashMap<String, String>();
t.put("name", entry.getKey());
t.put("caption", entry.getKey());
DataSourceProperties p = entry.getValue();
t.put("database", parseDatabaseName(p));
dataSourceNames.add(t);
}
});
}
return dataSourceNames;
}
public String getDatabaseName() {
List<Map<String, String>> dataSourceNames = this.getDataSourceNames();
String dataSource = DataSourceContextHolder.getDataSource();
Optional<Map<String, String>> op = dataSourceNames.stream()
.filter(t -> t.get("name").toString().equals(dataSource))
.findFirst();
if (op.isPresent()) {
return op.get().get("database");
} else {
return dataSourceNames.stream()
.filter(t -> t.get("name").toString().equals("primary"))
.findFirst().get().get("database");
}
}
private String parseDatabaseName(DataSourceProperties p) {
String url = p.getUrl();
String databaseName = "";
if (url.toLowerCase().indexOf("databasename") >= 0) {
String[] urlArr = p.getUrl().split(";");
for (String u : urlArr) {
if (u.toLowerCase().indexOf("databasename") >= 0) {
String[] uArr = u.split("=");
databaseName = uArr[uArr.length - 1];
}
}
} else {
String[] urlArr = p.getUrl().split("\\?")[0].split("/");
databaseName = urlArr[urlArr.length - 1];
}
return databaseName;
}
public Map<Object,Object> getTargetDataSourcesMap() {
return targetDataSourcesMap;
}
}
动态数据源配置——DynamicDataSourceConfig
首先取消系统自动数据库配置,设置exclude = { DataSourceAutoConfiguration.class }
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
然后自定义Bean,分别定义主数据源dataSource和动态数据源dynamicDataSource,并且注入到JdbcTemplate,NamedParameterJdbcTemplate,和DataSourceTransactionManager中,在访问数据时候自动识别对应的数据源。
//数据源配置类
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DynamicDataSourceConfig {
private static final Logger log = LoggerFactory.getLogger(DynamicDataSourceConfig.class);
@Resource
private DataSourceProperties dataSourceProperties;
@Bean(name = "dataSource")
public DataSource getDataSource(){
DataSourceBuilder<?> builder = DataSourceBuilder.create();
builder.driverClassName(dataSourceProperties.getDriverClassName());
builder.username(dataSourceProperties.getUsername());
builder.password(dataSourceProperties.getPassword());
builder.url(dataSourceProperties.getUrl());
return builder.build();
}
@Primary //当相同类型的实现类存在时,选择该注解标记的类
@Bean("dynamicDataSource")
public DynamicDataSource dynamicDataSource(){
DynamicDataSource dynamicDataSource = new DynamicDataSource();
//默认数据源
dynamicDataSource.setDefaultTargetDataSource(getDataSource());
Map<Object,Object> targetDataSourcesMap = new HashMap<>();
dynamicDataSource.setTargetDataSources(targetDataSourcesMap);
return dynamicDataSource;
}
//事务管理器DataSourceTransactionManager构造参数需要DataSource
//这里可以看到我们给的是dynamicDS这个bean
@Bean
public PlatformTransactionManager transactionManager(){
return new DataSourceTransactionManager(dynamicDataSource());
}
//这里的JdbcTemplate构造参数同样需要一个DataSource,为了实现数据源切换查询,
//这里使用的也是dynamicDS这个bean
@Bean(name = "jdbcTemplate")
public JdbcTemplate getJdbc(){
return new JdbcTemplate(dynamicDataSource());
}
//这里的JdbcTemplate构造参数同样需要一个DataSource,为了实现数据源切换查询,
//这里使用的也是dynamicDS这个bean
@Bean(name = "namedParameterJdbcTemplate")
public NamedParameterJdbcTemplate getNamedJdbc(){
return new NamedParameterJdbcTemplate(dynamicDataSource());
}
}
请求头过滤器——HeadFilter
拦截所有http请求,从header里面解析出当前需要访问的数据源,然后设置到线程变量HEADER_HOLDER中。
@WebFilter(filterName = "headFilter", urlPatterns = "/*")
public class HeadFilter extends OncePerRequestFilter {
private static final Logger log = LoggerFactory.getLogger(HeadFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (!"/api/auth/login".equals(request.getRequestURI())
&& !"/api/auth/jwt/login".equals(request.getRequestURI())
&& !"/api/auth/logout".equals(request.getRequestURI())
&& !"/api/metadata/dataSources".equals(request.getRequestURI())) {
String dataSource = request.getParameter("dataSource");
HeadRequestWrapper headRequestWrapper = new HeadRequestWrapper(request);
if (StringUtils.isEmpty(dataSource)) {
dataSource = headRequestWrapper.getHeader("dataSource");
if (StringUtils.isEmpty(dataSource)) {
dataSource = "primary";
headRequestWrapper.addHead("dataSource", dataSource);
}
}
DataSourceContextHolder.setHeaderDataSource(dataSource);
// finish
filterChain.doFilter(headRequestWrapper, response);
} else {
filterChain.doFilter(request, response);
}
}
}
实际应用
前面动态数据源配置准备工作已经完成,最后我们定义切面DataSourceAspect
@Aspect
public class DataSourceAspect {
private static final Logger log = LoggerFactory.getLogger(DataSourceAspect.class);
@Pointcut("within(cn.crudapi.api.controller..*)")
public void applicationPackagePointcut() {
}
@Around("applicationPackagePointcut()")
public Object dataSourceAround(ProceedingJoinPoint joinPoint) throws Throwable {
String dataSource = DataSourceContextHolder.getHeaderDataSource();
DataSourceContextHolder.setDataSource(dataSource);
try {
return joinPoint.proceed();
} finally {
DataSourceContextHolder.cleanDataSource();
}
}
}
在API对应的controller中拦截,获取当前的请求头数据源key,然后执行joinPoint.proceed(),最后再恢复数据源。当然在service内部还可以多次切换数据源,只需要调用DataSourceContextHolder.setDataSource()即可。比如可以从mysql数据库读取数据,然后保存到oracle数据库中。
前端集成
在请求头里面设置dataSource为对应的数据源,比如primary表示主数据源,postgresql表示从数据源postgresql,具体可以名称和application.properties配置保持一致。
首先调用的地方配置dataSource
const table = {
list: function(dataSource, tableName, page, rowsPerPage, search, query, filter) {
return axiosInstance.get("/api/business/" + tableName,
{
params: {
offset: (page - 1) * rowsPerPage,
limit: rowsPerPage,
search: search,
...query,
filter: filter
},
dataSource: dataSource
}
);
},
}
然后在axios里面统一拦截配置
axiosInstance.interceptors.request.use(
function(config) {
if (config.dataSource) {
console.log("config.dataSource = " + config.dataSource);
config.headers["dataSource"] = config.dataSource;
}
return config;
},
function(error) {
return Promise.reject(error);
}
);
效果如下
小结
本文主要介绍了多数据源功能,在同一个Java程序中,通过多数据源功能,不需要一行代码,我们就可以得到不同数据库的基本crud功能,包括API和UI。
- 点赞
- 收藏
- 关注作者
评论(0)