diff --git a/pom.xml b/pom.xml
index 479a474..89317cb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -216,11 +216,12 @@
org.apache.maven.plugins
maven-compiler-plugin
- 3.1
+ 3.8.1
${java.version}
${project.build.sourceEncoding}
+ true
diff --git a/sync-admin/src/main/java/com/lyr/web/controller/tool/DataSourceManagerController.java b/sync-admin/src/main/java/com/lyr/web/controller/tool/DataSourceManagerController.java
index 0fb2229..e88766d 100644
--- a/sync-admin/src/main/java/com/lyr/web/controller/tool/DataSourceManagerController.java
+++ b/sync-admin/src/main/java/com/lyr/web/controller/tool/DataSourceManagerController.java
@@ -1,11 +1,7 @@
package com.lyr.web.controller.tool;
-import com.lyr.framework.manager.DataSourceManagerService;
-import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
-import javax.annotation.Resource;
-
/**
* @author liyc
* @date 2024/10/30
diff --git a/sync-common/src/main/java/com/lyr/common/annotation/DataSource.java b/sync-common/src/main/java/com/lyr/common/annotation/DataSource.java
index 94468e6..d159fd9 100644
--- a/sync-common/src/main/java/com/lyr/common/annotation/DataSource.java
+++ b/sync-common/src/main/java/com/lyr/common/annotation/DataSource.java
@@ -16,8 +16,18 @@ import java.lang.annotation.*;
@Documented
@Inherited
public @interface DataSource {
+
/**
* 切换数据源名称
*/
DataSourceType value() default DataSourceType.MASTER;
+
+ /**
+ * 数据源名称 (优先使用这个数据源,否则使用value)
+ *
+ * @return
+ */
+ String name() default "";
+
+
}
diff --git a/sync-common/src/main/java/com/lyr/common/enums/DataSourceType.java b/sync-common/src/main/java/com/lyr/common/enums/DataSourceType.java
index 5f84803..e4e1cb0 100644
--- a/sync-common/src/main/java/com/lyr/common/enums/DataSourceType.java
+++ b/sync-common/src/main/java/com/lyr/common/enums/DataSourceType.java
@@ -14,5 +14,11 @@ public enum DataSourceType {
/**
* 从库
*/
- SLAVE
+ SLAVE,
+
+ /**
+ * 外部
+ */
+ EXTERNAL
+
}
diff --git a/sync-common/src/main/java/com/lyr/common/utils/AssertUtil.java b/sync-common/src/main/java/com/lyr/common/utils/AssertUtil.java
new file mode 100644
index 0000000..6f1c659
--- /dev/null
+++ b/sync-common/src/main/java/com/lyr/common/utils/AssertUtil.java
@@ -0,0 +1,104 @@
+package com.lyr.common.utils;
+
+
+import com.lyr.common.exception.ServiceException;
+import org.apache.poi.ss.formula.functions.T;
+import org.springframework.util.CollectionUtils;
+
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @author Admin
+ */
+public class AssertUtil {
+
+
+ public static void notEmpty(Long val, String s, Integer... codes) throws ServiceException {
+ if (val == null) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void notEmpty(Collection c, String s, Integer... codes) throws ServiceException {
+ if (CollectionUtils.isEmpty(c)) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void notEmpty(String val, String s, Integer... codes) throws ServiceException {
+ if (StringUtils.isEmpty(val)) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void notEmpty(Object val, String s, Integer... codes) throws ServiceException {
+ if (val == null) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ /**
+ * 不为空时抛出异常信息
+ *
+ * @param obj
+ * @param s
+ */
+ public static void empty(Object obj, String s, Integer... codes) {
+ if (null != obj) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void empty(Object obj, String s, List t, Integer... codes) {
+ if (null != obj) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void empty(Collection c, String s) {
+ if (!CollectionUtils.isEmpty(c)) {
+ throw new ServiceException(s, 500);
+ }
+ }
+
+ public static void notEqual(boolean val, String s, Integer... codes) {
+ if (val) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void equal(boolean val, String s) {
+ notEqual(!val, s);
+ }
+
+
+ public static void isFalse(boolean b, String s, Integer... codes) {
+ if (b) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+ public static void isTrue(boolean b, String s, Integer... codes) throws ServiceException {
+ if (!b) {
+ Integer code = (codes.length == 0) ? 500 : codes[0];
+ throw new ServiceException(s, code);
+ }
+ }
+
+
+ public static void test() {
+ throw new ServiceException("222");
+ }
+}
+
+
+
diff --git a/sync-common/src/main/java/com/lyr/common/utils/SpElUtil.java b/sync-common/src/main/java/com/lyr/common/utils/SpElUtil.java
new file mode 100644
index 0000000..f89c25c
--- /dev/null
+++ b/sync-common/src/main/java/com/lyr/common/utils/SpElUtil.java
@@ -0,0 +1,49 @@
+package com.lyr.common.utils;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.expression.ExpressionParser;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.expression.spel.support.StandardEvaluationContext;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+
+/**
+ * spel 工具类
+ *
+ * @author liyc
+ * @date 2024/10/30
+ * @description TODO
+ **/
+@Component
+public class SpElUtil {
+
+ @Resource
+ public ExpressionParser expressionParser;
+
+ @Bean
+ public ExpressionParser expressionParser() {
+ return new SpelExpressionParser();
+ }
+
+
+ /**
+ * 解析表达式
+ *
+ * @param method
+ * @param args
+ * @param expression
+ * @return
+ */
+ public String parseExpression(Method method, Object[] args, String expression) {
+ StandardEvaluationContext context = new StandardEvaluationContext();
+ Parameter[] parameters = method.getParameters();
+ for (int i = 0; i < parameters.length; i++) {
+ Parameter parameter = parameters[i];
+ context.setVariable(parameter.getName(), args[i]);
+ }
+ return expressionParser.parseExpression(expression).getValue(context, String.class);
+ }
+}
diff --git a/sync-common/target/classes/com/lyr/common/annotation/DataSource.class b/sync-common/target/classes/com/lyr/common/annotation/DataSource.class
index d35eb21..e3a497b 100644
Binary files a/sync-common/target/classes/com/lyr/common/annotation/DataSource.class and b/sync-common/target/classes/com/lyr/common/annotation/DataSource.class differ
diff --git a/sync-common/target/classes/com/lyr/common/enums/DataSourceType.class b/sync-common/target/classes/com/lyr/common/enums/DataSourceType.class
index f0f26a3..2c2f0a1 100644
Binary files a/sync-common/target/classes/com/lyr/common/enums/DataSourceType.class and b/sync-common/target/classes/com/lyr/common/enums/DataSourceType.class differ
diff --git a/sync-framework/pom.xml b/sync-framework/pom.xml
index edf5d4b..4fe857d 100644
--- a/sync-framework/pom.xml
+++ b/sync-framework/pom.xml
@@ -74,6 +74,11 @@
com.lyr
sync-system
+
+ org.projectlombok
+ lombok
+ provided
+
diff --git a/sync-framework/src/main/java/com/lyr/framework/aspectj/DataSourceAspect.java b/sync-framework/src/main/java/com/lyr/framework/aspectj/DataSourceAspect.java
index 2c86407..85c9c35 100644
--- a/sync-framework/src/main/java/com/lyr/framework/aspectj/DataSourceAspect.java
+++ b/sync-framework/src/main/java/com/lyr/framework/aspectj/DataSourceAspect.java
@@ -1,8 +1,11 @@
package com.lyr.framework.aspectj;
import com.lyr.common.annotation.DataSource;
+import com.lyr.common.exception.ServiceException;
+import com.lyr.common.utils.SpElUtil;
import com.lyr.common.utils.StringUtils;
import com.lyr.framework.datasource.DynamicDataSourceContextHolder;
+import com.lyr.framework.manager.DataSourceManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@@ -14,6 +17,8 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
+import javax.annotation.Resource;
+import java.lang.reflect.Method;
import java.util.Objects;
/**
@@ -27,28 +32,46 @@ import java.util.Objects;
public class DataSourceAspect {
protected Logger logger = LoggerFactory.getLogger(getClass());
- @Pointcut("@annotation(com.lyr.common.annotation.DataSource)"
- + "|| @within(com.lyr.common.annotation.DataSource)")
+ @Resource
+ private SpElUtil spElUtil;
+ @Resource
+ private DataSourceManager dataSourceManager;
+
+ @Pointcut("@annotation(com.lyr.common.annotation.DataSource)" + "|| @within(com.lyr.common.annotation.DataSource)")
public void dsPointCut() {
}
@Around("dsPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
+ String value = null;
DataSource dataSource = getDataSource(point);
-
if (StringUtils.isNotNull(dataSource)) {
- DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
+ value = dataSource.value().name();
+ String name = dataSource.name();
+ if (StringUtils.isNotEmpty(name)) {
+ // 解析spEl表达式
+ Object[] args = point.getArgs();
+ MethodSignature signature = (MethodSignature) point.getSignature();
+ Method method = signature.getMethod();
+ value = spElUtil.parseExpression(method, args, dataSource.name());
+ }
+ DynamicDataSourceContextHolder.setDataSourceType(value);
}
-
try {
return point.proceed();
+ } catch (Exception e) {
+ logger.error("数据源切换失败", e);
+ logger.debug("要切换的数据源:{}", value);
+ dataSourceManager.print();
+ throw new ServiceException("切换数据源到[" + value + "]失败");
} finally {
// 销毁数据源 在执行方法之后
DynamicDataSourceContextHolder.clearDataSourceType();
}
}
+
/**
* 获取需要切换的数据源
*/
@@ -58,7 +81,8 @@ public class DataSourceAspect {
if (Objects.nonNull(dataSource)) {
return dataSource;
}
-
return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
}
+
+
}
diff --git a/sync-framework/src/main/java/com/lyr/framework/datasource/DynamicDataSource.java b/sync-framework/src/main/java/com/lyr/framework/datasource/DynamicDataSource.java
index f0f9e6f..1a2f980 100644
--- a/sync-framework/src/main/java/com/lyr/framework/datasource/DynamicDataSource.java
+++ b/sync-framework/src/main/java/com/lyr/framework/datasource/DynamicDataSource.java
@@ -1,10 +1,9 @@
package com.lyr.framework.datasource;
-import com.alibaba.druid.pool.DruidDataSource;
import com.lyr.common.utils.spring.SpringUtils;
+import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
-import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -13,6 +12,7 @@ import java.util.concurrent.ConcurrentHashMap;
*
* @author ruoyi
*/
+@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource {
private static Map