소스 검색

数据源支持集群化部署改造。

aokunsang 2 주 전
부모
커밋
15099d3828

+ 45 - 0
platomix-gmetry-bi-api/src/main/java/org/springblade/aop/DsAspect.java

@@ -0,0 +1,45 @@
+package org.springblade.aop;
+
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.springblade.datasource.support.DynamicDataSourceSupport;
+import org.springblade.report.entity.ReportDatasource;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 数据源切面---执行数据源相关的sql时先判断是否有该数据源
+ */
+@Aspect
+@Component
+@Slf4j
+public class DsAspect {
+
+    @Autowired
+    private DynamicDataSourceSupport dynamicDataSourceSupport;
+
+    private void addDs(ReportDatasource ds) {
+        boolean dsExists = dynamicDataSourceSupport.exists(ds.getPoolName());
+        log.info("检测数据源是否存在|poolname={},dsExists={}", ds.getPoolName(), dsExists);
+        if (!dsExists) {
+            dynamicDataSourceSupport.addDs(ds);
+        }
+    }
+
+    @Before("execution(* org.springblade.cube.service.DynamicTableService..*(..))")
+    public void beforeMethod(JoinPoint joinPoint) {
+        Object[] args = joinPoint.getArgs();
+        for (Object arg : args) {
+            if (arg instanceof ReportDatasource) {
+                ReportDatasource ds = (ReportDatasource) arg;
+                addDs(ds);
+                break;
+            }
+        }
+    }
+
+
+}

+ 1 - 0
platomix-gmetry-bi-api/src/main/java/org/springblade/datasource/support/BladexDataSourceProvider.java

@@ -60,6 +60,7 @@ public class BladexDataSourceProvider extends AbstractJdbcDataSourceProvider {
 				dataSourceProperty.setUrl(url);
 				dataSourceProperty.setUsername(dsUser);
 				dataSourceProperty.setPassword(dsPassword);
+				//懒加载数据源
 				dataSourceProperty.setLazy(true);
 				//校验连接成功,允许初始化添加到动态数据源
 				ReportDatasource reportDs = new ReportDatasource();

+ 12 - 0
platomix-gmetry-bi-api/src/main/java/org/springblade/datasource/support/DynamicDataSourceSupport.java

@@ -8,8 +8,10 @@ import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Properties;
 
 import javax.sql.DataSource;
@@ -106,6 +108,16 @@ public class DynamicDataSourceSupport {
 	}
 
 	/**
+	 * 判断是否有数据源
+	 *
+	 * @param poolName
+	 * @return
+	 */
+	public boolean exists(String poolName) {
+		return Optional.ofNullable(dynamicRoutingDataSource.getDataSources()).orElse(new HashMap<>()).containsKey(poolName);
+	}
+
+	/**
 	 * 测试数据源
 	 * 
 	 * @param ds

+ 3 - 7
platomix-gmetry-bi-api/src/main/java/org/springblade/report/controller/DataApiController.java

@@ -18,7 +18,6 @@ package org.springblade.report.controller;
 
 import java.util.ArrayList;
 import java.util.Base64;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
@@ -28,13 +27,13 @@ import java.util.stream.Stream;
 
 import javax.servlet.http.HttpServletRequest;
 
-import cn.hutool.core.lang.Assert;
 import org.springblade.core.boot.ctrl.BladeController;
 import org.springblade.core.mp.support.Condition;
 import org.springblade.core.mp.support.Query;
 import org.springblade.core.secure.BladeUser;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.Func;
+import org.springblade.cube.service.DynamicTableService;
 import org.springblade.cube.utils.CubeUtils;
 import org.springblade.report.dto.request.DataApiDTO;
 import org.springblade.report.entity.ApiConfig;
@@ -46,8 +45,6 @@ import org.springblade.report.service.IApiConfigDetailService;
 import org.springblade.report.service.IApiConfigService;
 import org.springblade.report.service.IDataApiService;
 import org.springblade.report.service.IReportDatasourceService;
-import org.springblade.report.service.ITestService;
-import org.springblade.report.util.EncryptionUtil;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -89,7 +86,7 @@ public class DataApiController extends BladeController {
 //	private final DataSource dataSource;
 //	private final DataSourceCreator dataSourceCreator;
 
-	private final ITestService testService;
+	private final DynamicTableService dynamicTableService;
 
 	private final IApiConfigService apiConfigService;
 	private final IApiConfigDetailService apiConfigDetailService;
@@ -340,8 +337,7 @@ public class DataApiController extends BladeController {
 				}
 	
 			}
-	
-			List<LinkedHashMap<String, Object>> list = testService.selectTest(poolName, querySql);
+			List<Map<String, Object>> list = dynamicTableService.selectList(datasource, querySql);
 			return R.data(list);
 		}
 

+ 0 - 50
platomix-gmetry-bi-api/src/main/java/org/springblade/report/mapper/TestMapper.java

@@ -1,50 +0,0 @@
-/*
- *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
- *
- *  Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions are met:
- *
- *  Redistributions of source code must retain the above copyright notice,
- *  this list of conditions and the following disclaimer.
- *  Redistributions in binary form must reproduce the above copyright
- *  notice, this list of conditions and the following disclaimer in the
- *  documentation and/or other materials provided with the distribution.
- *  Neither the name of the dreamlu.net developer nor the names of its
- *  contributors may be used to endorse or promote products derived from
- *  this software without specific prior written permission.
- *  Author: Chill 庄骞 (smallchill@163.com)
- */
-package org.springblade.report.mapper;
-
-import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import org.apache.ibatis.annotations.Param;
-import org.springblade.report.entity.Test;
-import org.springblade.report.vo.TestVO;
-
-import java.util.LinkedHashMap;
-import java.util.List;
-
-/**
- * Mapper 接口
- *
- * @author BladeX
- * @since 2021-03-20
- */
-@InterceptorIgnore(tenantLine = "true")
-public interface TestMapper extends BaseMapper<Test> {
-
-	/**
-	 * 自定义分页
-	 *
-	 * @param page
-	 * @param test
-	 * @return
-	 */
-	List<TestVO> selectTestPage(IPage page, TestVO test);
-
-	List<LinkedHashMap<String, Object>> selectTest(@Param("sql") String sql);//, @Param("params")Map<String, Object> params
-	
-	List<LinkedHashMap<String, Object>> selectPage(IPage<?> page, @Param("sql") String sql);
-}

+ 0 - 23
platomix-gmetry-bi-api/src/main/java/org/springblade/report/mapper/TestMapper.xml

@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="org.springblade.report.mapper.TestMapper">
-
-    <!-- 通用查询映射结果 -->
-    <resultMap id="testResultMap" type="org.springblade.report.entity.Test">
-        <result column="id" property="id"/>
-        <result column="name" property="name"/>
-    </resultMap>
-
-
-    <select id="selectTestPage" resultMap="testResultMap">
-        select * from test where is_deleted = 0
-    </select>
-
-    <select id="selectTest" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
-        ${sql}
-    </select>
-    
-    <select id="selectPage" parameterType="java.util.Map" resultType="java.util.LinkedHashMap">
-        ${sql}
-    </select>
-</mapper>

+ 48 - 48
platomix-gmetry-bi-api/src/main/java/org/springblade/report/service/ITestService.java

@@ -1,48 +1,48 @@
-/*
- *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
- *
- *  Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions are met:
- *
- *  Redistributions of source code must retain the above copyright notice,
- *  this list of conditions and the following disclaimer.
- *  Redistributions in binary form must reproduce the above copyright
- *  notice, this list of conditions and the following disclaimer in the
- *  documentation and/or other materials provided with the distribution.
- *  Neither the name of the dreamlu.net developer nor the names of its
- *  contributors may be used to endorse or promote products derived from
- *  this software without specific prior written permission.
- *  Author: Chill 庄骞 (smallchill@163.com)
- */
-package org.springblade.report.service;
-
-import java.util.LinkedHashMap;
-import java.util.List;
-
-import org.springblade.report.entity.Test;
-import org.springblade.report.vo.TestVO;
-
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.extension.service.IService;
-
-/**
- * 服务类
- *
- * @author BladeX
- * @since 2021-03-20
- */
-public interface ITestService extends IService<Test> {
-
-	/**
-	 * 自定义分页
-	 *
-	 * @param page
-	 * @param test
-	 * @return
-	 */
-	IPage<TestVO> selectTestPage(IPage<TestVO> page, TestVO test);
-
-	List<LinkedHashMap<String, Object>> selectTest(String poolName, String sql);//, Map<String, Object> map
-
-	List<LinkedHashMap<String, Object>> selectPage(IPage<?> page, String poolName, String sql);
-}
+///*
+// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+// *
+// *  Redistribution and use in source and binary forms, with or without
+// *  modification, are permitted provided that the following conditions are met:
+// *
+// *  Redistributions of source code must retain the above copyright notice,
+// *  this list of conditions and the following disclaimer.
+// *  Redistributions in binary form must reproduce the above copyright
+// *  notice, this list of conditions and the following disclaimer in the
+// *  documentation and/or other materials provided with the distribution.
+// *  Neither the name of the dreamlu.net developer nor the names of its
+// *  contributors may be used to endorse or promote products derived from
+// *  this software without specific prior written permission.
+// *  Author: Chill 庄骞 (smallchill@163.com)
+// */
+//package org.springblade.report.service;
+//
+//import java.util.LinkedHashMap;
+//import java.util.List;
+//
+//import org.springblade.report.entity.Test;
+//import org.springblade.report.vo.TestVO;
+//
+//import com.baomidou.mybatisplus.core.metadata.IPage;
+//import com.baomidou.mybatisplus.extension.service.IService;
+//
+///**
+// * 服务类
+// *
+// * @author BladeX
+// * @since 2021-03-20
+// */
+//public interface ITestService extends IService<Test> {
+//
+//	/**
+//	 * 自定义分页
+//	 *
+//	 * @param page
+//	 * @param test
+//	 * @return
+//	 */
+//	IPage<TestVO> selectTestPage(IPage<TestVO> page, TestVO test);
+//
+//	List<LinkedHashMap<String, Object>> selectTest(String poolName, String sql);//, Map<String, Object> map
+//
+//	List<LinkedHashMap<String, Object>> selectPage(IPage<?> page, String poolName, String sql);
+//}

+ 65 - 65
platomix-gmetry-bi-api/src/main/java/org/springblade/report/service/impl/TestServiceImpl.java

@@ -1,65 +1,65 @@
-/*
- *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
- *
- *  Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions are met:
- *
- *  Redistributions of source code must retain the above copyright notice,
- *  this list of conditions and the following disclaimer.
- *  Redistributions in binary form must reproduce the above copyright
- *  notice, this list of conditions and the following disclaimer in the
- *  documentation and/or other materials provided with the distribution.
- *  Neither the name of the dreamlu.net developer nor the names of its
- *  contributors may be used to endorse or promote products derived from
- *  this software without specific prior written permission.
- *  Author: Chill 庄骞 (smallchill@163.com)
- */
-package org.springblade.report.service.impl;
-
-import java.util.LinkedHashMap;
-import java.util.List;
-
-import javax.annotation.Resource;
-
-import org.springblade.report.entity.Test;
-import org.springblade.report.mapper.TestMapper;
-import org.springblade.report.service.ITestService;
-import org.springblade.report.vo.TestVO;
-import org.springframework.stereotype.Service;
-
-import com.baomidou.dynamic.datasource.annotation.DS;
-import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-
-/**
- * 服务实现类
- *
- * @author BladeX
- * @since 2021-03-20
- */
-@Service
-//@DS("#session.tenantName")
-public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {
-
-	@Resource
-	private TestMapper testMapper;
-
-	@Override
-	public IPage<TestVO> selectTestPage(IPage<TestVO> page, TestVO test) {
-		return page.setRecords(baseMapper.selectTestPage(page, test));
-	}
-
-	@DS("#poolName")
-	@Override
-	public List<LinkedHashMap<String, Object>> selectTest(String poolName, String sql) {
-		sql = sql.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&le;", "<=").replaceAll("&ge;", ">=");
-		return testMapper.selectTest(sql);
-	}
-
-	@DS("#poolName")
-	@Override
-	public List<LinkedHashMap<String, Object>> selectPage(IPage<?> page, String poolName, String sql) {
-		sql = sql.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&le;", "<=").replaceAll("&ge;", ">=");
-		return testMapper.selectPage(page, sql);
-	}
-}
+///*
+// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+// *
+// *  Redistribution and use in source and binary forms, with or without
+// *  modification, are permitted provided that the following conditions are met:
+// *
+// *  Redistributions of source code must retain the above copyright notice,
+// *  this list of conditions and the following disclaimer.
+// *  Redistributions in binary form must reproduce the above copyright
+// *  notice, this list of conditions and the following disclaimer in the
+// *  documentation and/or other materials provided with the distribution.
+// *  Neither the name of the dreamlu.net developer nor the names of its
+// *  contributors may be used to endorse or promote products derived from
+// *  this software without specific prior written permission.
+// *  Author: Chill 庄骞 (smallchill@163.com)
+// */
+//package org.springblade.report.service.impl;
+//
+//import java.util.LinkedHashMap;
+//import java.util.List;
+//
+//import javax.annotation.Resource;
+//
+//import org.springblade.report.entity.Test;
+//import org.springblade.report.mapper.TestMapper;
+//import org.springblade.report.service.ITestService;
+//import org.springblade.report.vo.TestVO;
+//import org.springframework.stereotype.Service;
+//
+//import com.baomidou.dynamic.datasource.annotation.DS;
+//import com.baomidou.mybatisplus.core.metadata.IPage;
+//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+//
+///**
+// * 服务实现类
+// *
+// * @author BladeX
+// * @since 2021-03-20
+// */
+//@Service
+////@DS("#session.tenantName")
+//public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {
+//
+//	@Resource
+//	private TestMapper testMapper;
+//
+//	@Override
+//	public IPage<TestVO> selectTestPage(IPage<TestVO> page, TestVO test) {
+//		return page.setRecords(baseMapper.selectTestPage(page, test));
+//	}
+//
+//	@DS("#poolName")
+//	@Override
+//	public List<LinkedHashMap<String, Object>> selectTest(String poolName, String sql) {
+//		sql = sql.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&le;", "<=").replaceAll("&ge;", ">=");
+//		return testMapper.selectTest(sql);
+//	}
+//
+//	@DS("#poolName")
+//	@Override
+//	public List<LinkedHashMap<String, Object>> selectPage(IPage<?> page, String poolName, String sql) {
+//		sql = sql.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&le;", "<=").replaceAll("&ge;", ">=");
+//		return testMapper.selectPage(page, sql);
+//	}
+//}