diff --git a/blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java index fec13c592..385589b92 100644 --- a/blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/AsyncAnnotationAspect.java @@ -1,41 +1,24 @@ package com.logpm.aftersales.aspect; -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; -import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; -import org.springblade.common.cache.CacheNames; -import org.springblade.common.constant.LauncherConstant; -import org.springblade.core.launch.constant.AppConstant; -import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springblade.core.tool.utils.ThreadLocalUtil; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpHeaders; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; @Aspect @Component -@AllArgsConstructor +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { - private final BladeRedis bladeRedis; - private final Environment environment; + /** * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 @@ -48,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -58,91 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - } - - - @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") - public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - - JSONObject jsonObject = mockLogin(); - MockHttpServletRequest mockRequest = new MockHttpServletRequest(); - mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); - - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); - httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - ThreadLocalUtil.put("bladeContext", httpHeaders); - - DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - - return result; - - - } - - private JSONObject mockLogin(){ - - String account ="shujutongbu"; - JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); - if(Objects.isNull(data)){ - String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; - HttpRequest urlRequest = HttpRequest.post(url); - urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - urlRequest.header("Tenant-Id", "627683"); - - Map params = new HashMap<>(); - params.put("grant_type", "local_server"); - params.put("scope", "all"); - params.put("username", account); - params.put("tenantId", "627683"); - HttpResponse execute = urlRequest.form(params).execute(); - String body = execute.body(); - data = JSONObject.parseObject(body); - bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); - } - return data; - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") -// public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") -// public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - - } diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/JobAnnotationAspect.java similarity index 58% rename from blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/AsyncAnnotationAspect.java rename to blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/JobAnnotationAspect.java index a8e258ba0..609decb6e 100644 --- a/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/JobAnnotationAspect.java @@ -1,4 +1,4 @@ -package com.logpm.patch.aspect; +package com.logpm.aftersales.aspect; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; @@ -8,9 +8,10 @@ import lombok.AllArgsConstructor; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springblade.common.annotations.LogpmAsync; import org.springblade.common.cache.CacheNames; import org.springblade.common.constant.LauncherConstant; -import org.springblade.core.launch.constant.AppConstant; import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.ThreadLocalUtil; @@ -21,6 +22,7 @@ import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -28,62 +30,29 @@ import java.util.Objects; @Aspect @Component @AllArgsConstructor -public class AsyncAnnotationAspect { +public class JobAnnotationAspect { private final BladeRedis bladeRedis; private final Environment environment; - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - - @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { - JSONObject jsonObject = mockLogin(); - MockHttpServletRequest mockRequest = new MockHttpServletRequest(); - mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); + JSONObject jsonObject = mockLogin(); + MockHttpServletRequest mockRequest = new MockHttpServletRequest(); + mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); ThreadLocalUtil.put("bladeContext", httpHeaders); - DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); + DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); return result; @@ -114,5 +83,4 @@ public class AsyncAnnotationAspect { } - } diff --git a/blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java index 8434e606e..e8024cf24 100644 --- a/blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-basic/src/main/java/com/logpm/basic/aspect/AsyncAnnotationAspect.java @@ -1,20 +1,22 @@ package com.logpm.basic.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { /** @@ -29,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -39,41 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - } - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") -// public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") -// public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - } diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java index 6c565bde7..41137ed9c 100644 --- a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/AsyncAnnotationAspect.java @@ -1,41 +1,24 @@ package com.logpm.basicdata.aspect; -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; -import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; -import org.springblade.common.cache.CacheNames; -import org.springblade.common.constant.LauncherConstant; -import org.springblade.core.launch.constant.AppConstant; -import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springblade.core.tool.utils.ThreadLocalUtil; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpHeaders; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; @Aspect @Component -@AllArgsConstructor +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { - private final BladeRedis bladeRedis; - private final Environment environment; + /** * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 @@ -48,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -58,88 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - } - @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") - public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - - JSONObject jsonObject = mockLogin(); - MockHttpServletRequest mockRequest = new MockHttpServletRequest(); - mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); - - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); - httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - ThreadLocalUtil.put("bladeContext", httpHeaders); - - DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - - return result; - - - } - - private JSONObject mockLogin(){ - - String account ="shujutongbu"; - JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); - if(Objects.isNull(data)){ - String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; - HttpRequest urlRequest = HttpRequest.post(url); - urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - urlRequest.header("Tenant-Id", "627683"); - - Map params = new HashMap<>(); - params.put("grant_type", "local_server"); - params.put("scope", "all"); - params.put("username", account); - params.put("tenantId", "627683"); - HttpResponse execute = urlRequest.form(params).execute(); - String body = execute.body(); - data = JSONObject.parseObject(body); - bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); - } - return data; - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") -// public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") -// public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - } diff --git a/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/JobAnnotationAspect.java b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/JobAnnotationAspect.java new file mode 100644 index 000000000..9409e560c --- /dev/null +++ b/blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/JobAnnotationAspect.java @@ -0,0 +1,82 @@ +package com.logpm.basicdata.aspect; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.AllArgsConstructor; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.common.cache.CacheNames; +import org.springblade.common.constant.LauncherConstant; +import org.springblade.core.redis.cache.BladeRedis; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Aspect +@Component +@AllArgsConstructor +public class JobAnnotationAspect { + private final BladeRedis bladeRedis; + private final Environment environment; + + @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") + public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + + JSONObject jsonObject = mockLogin(); + MockHttpServletRequest mockRequest = new MockHttpServletRequest(); + mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); + httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + ThreadLocalUtil.put("bladeContext", httpHeaders); + + DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + + return result; + + + } + + private JSONObject mockLogin(){ + + String account ="shujutongbu"; + JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); + if(Objects.isNull(data)){ + String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; + HttpRequest urlRequest = HttpRequest.post(url); + urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + urlRequest.header("Tenant-Id", "627683"); + + Map params = new HashMap<>(); + params.put("grant_type", "local_server"); + params.put("scope", "all"); + params.put("username", account); + params.put("tenantId", "627683"); + HttpResponse execute = urlRequest.form(params).execute(); + String body = execute.body(); + data = JSONObject.parseObject(body); + bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); + } + return data; + } + + +} diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java index 760044112..ae1119cbb 100644 --- a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/AsyncAnnotationAspect.java @@ -9,6 +9,7 @@ import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.StringUtil; +import org.springframework.core.annotation.Order; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @@ -17,6 +18,7 @@ import java.lang.reflect.Method; @Aspect @Component @Slf4j +@Order(-1) public class AsyncAnnotationAspect { /** @@ -45,41 +47,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; } } diff --git a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AnnotationAspect.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/RabbitAnnotationAspect.java similarity index 54% rename from blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AnnotationAspect.java rename to blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/RabbitAnnotationAspect.java index c9b546289..f01919884 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AnnotationAspect.java +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/aspect/RabbitAnnotationAspect.java @@ -1,51 +1,23 @@ -package com.logpm.factorydata.olo.aspect; +package com.logpm.distribution.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; +import org.springblade.common.annotations.LogpmAsync; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component -public class AnnotationAspect { +@Slf4j +public class RabbitAnnotationAspect { - /** - * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 - * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 - */ - @Around("@annotation(org.springframework.scheduling.annotation.Async)") - public Object asyncAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - - Async myAsync = method.getAnnotation(Async.class); - String annotationValue = myAsync.value(); - - if(StringUtil.isNotBlank(annotationValue) && annotationValue.equals("asyncExecutor")){ - // 在方法执行前的操作 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - - // 执行原方法 - Object result = joinPoint.proceed(); - - // 在方法执行后的操作 - DynamicDataSourceContextHolder.poll(); - - return result; - }else{ - return joinPoint.proceed(); - } - - } /** * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 @@ -55,7 +27,7 @@ public class AnnotationAspect { * @throws Throwable 如果执行过程中发生异常,则抛出。 */ @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行前,获取当前租户ID并设置到数据源上下文中 String tenantId = AuthUtil.getTenantId(); DynamicDataSourceContextHolder.push(tenantId); @@ -67,7 +39,7 @@ public class AnnotationAspect { } @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行前,获取当前租户ID并设置到数据源上下文中 String tenantId = AuthUtil.getTenantId(); DynamicDataSourceContextHolder.push(tenantId); diff --git a/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/AnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/AnnotationAspect.java deleted file mode 100644 index c00bab508..000000000 --- a/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/AnnotationAspect.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.logpm.factorydata.jinpai.aspect; - -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import com.alibaba.fastjson.JSONObject; -import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; -import lombok.AllArgsConstructor; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.reflect.MethodSignature; -import org.springblade.common.annotations.LogpmAsync; -import org.springblade.common.cache.CacheNames; -import org.springblade.common.constant.LauncherConstant; -import org.springblade.core.redis.cache.BladeRedis; -import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springblade.core.tool.utils.ThreadLocalUtil; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpHeaders; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -@Aspect -@Component -@AllArgsConstructor -public class AnnotationAspect { - private final BladeRedis bladeRedis; - private final Environment environment; - - - /** - * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 - * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 - */ - @Around("@annotation(org.springblade.common.annotations.LogpmAsync)") - public Object asyncAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - - LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); - String annotationValue = myAsync.value(); - - // 在方法执行前的操作 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - - // 执行原方法 - Object result = joinPoint.proceed(); - - // 在方法执行后的操作 - DynamicDataSourceContextHolder.poll(); - - return result; - - - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") - public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - - JSONObject jsonObject = mockLogin(); - MockHttpServletRequest mockRequest = new MockHttpServletRequest(); - mockRequest.addHeader("Blade-Auth", "bearer " + jsonObject.get("access_token")); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); - - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add("Blade-Auth", "bearer " + jsonObject.get("access_token")); - httpHeaders.add("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - ThreadLocalUtil.put("bladeContext", httpHeaders); - - DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - - return result; - - - } - - private JSONObject mockLogin() { - - String account = "shujutongbu"; - JSONObject data = bladeRedis.get(CacheNames.LOCAL_SERVER_USER + account); - if (Objects.isNull(data)) { - String url = "http://" + LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0])) + "/blade-auth/oauth/token"; - HttpRequest urlRequest = HttpRequest.post(url); - urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - urlRequest.header("Tenant-Id", "627683"); - - Map params = new HashMap<>(); - params.put("grant_type", "local_server"); - params.put("scope", "all"); - params.put("username", account); - params.put("tenantId", "627683"); - HttpResponse execute = urlRequest.form(params).execute(); - String body = execute.body(); - data = JSONObject.parseObject(body); - bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER + account, data, 3200L); - } - return data; - } - -} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/AsyncAnnotationAspect.java new file mode 100644 index 000000000..bfe13ad27 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/AsyncAnnotationAspect.java @@ -0,0 +1,51 @@ +package com.logpm.factorydata.jinpai.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springblade.common.annotations.LogpmAsync; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +@Aspect +@Component +@Slf4j +@Order(-1) +public class AsyncAnnotationAspect { + + /** + * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 + * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 + */ + @Around("@annotation(org.springblade.common.annotations.LogpmAsync)") + public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + + LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); + String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); + + // 在方法执行前的操作 + String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); + DynamicDataSourceContextHolder.push(tenantId); + + // 执行原方法 + Object result = joinPoint.proceed(); + + // 在方法执行后的操作 + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/JobAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/JobAnnotationAspect.java new file mode 100644 index 000000000..250c4d8d6 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/JobAnnotationAspect.java @@ -0,0 +1,82 @@ +package com.logpm.factorydata.jinpai.aspect; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.AllArgsConstructor; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.common.cache.CacheNames; +import org.springblade.common.constant.LauncherConstant; +import org.springblade.core.redis.cache.BladeRedis; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Aspect +@Component +@AllArgsConstructor +public class JobAnnotationAspect { + private final BladeRedis bladeRedis; + private final Environment environment; + + @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") + public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + + JSONObject jsonObject = mockLogin(); + MockHttpServletRequest mockRequest = new MockHttpServletRequest(); + mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); + httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + ThreadLocalUtil.put("bladeContext", httpHeaders); + + DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + + return result; + + + } + + private JSONObject mockLogin(){ + + String account ="shujutongbu"; + JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); + if(Objects.isNull(data)){ + String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; + HttpRequest urlRequest = HttpRequest.post(url); + urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + urlRequest.header("Tenant-Id", "627683"); + + Map params = new HashMap<>(); + params.put("grant_type", "local_server"); + params.put("scope", "all"); + params.put("username", account); + params.put("tenantId", "627683"); + HttpResponse execute = urlRequest.form(params).execute(); + String body = execute.body(); + data = JSONObject.parseObject(body); + bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); + } + return data; + } + + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/RabbitAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/RabbitAnnotationAspect.java new file mode 100644 index 000000000..fa2f303bb --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/RabbitAnnotationAspect.java @@ -0,0 +1,48 @@ +package com.logpm.factorydata.jinpai.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RabbitAnnotationAspect { + + + /** + * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 + * + * @param joinPoint 切面的连接点,表示当前被拦截的方法。 + * @return 返回被拦截方法的执行结果。 + * @throws Throwable 如果执行过程中发生异常,则抛出。 + */ + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AsyncAnnotationAspect.java new file mode 100644 index 000000000..4de5e3e08 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AsyncAnnotationAspect.java @@ -0,0 +1,51 @@ +package com.logpm.factorydata.olo.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springblade.common.annotations.LogpmAsync; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +@Aspect +@Component +@Slf4j +@Order(-1) +public class AsyncAnnotationAspect { + + /** + * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 + * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 + */ + @Around("@annotation(org.springblade.common.annotations.LogpmAsync)") + public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + + LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); + String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); + + // 在方法执行前的操作 + String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); + DynamicDataSourceContextHolder.push(tenantId); + + // 执行原方法 + Object result = joinPoint.proceed(); + + // 在方法执行后的操作 + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/RabbitAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/RabbitAnnotationAspect.java new file mode 100644 index 000000000..cd968e36d --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/RabbitAnnotationAspect.java @@ -0,0 +1,48 @@ +package com.logpm.factorydata.olo.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RabbitAnnotationAspect { + + + /** + * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 + * + * @param joinPoint 切面的连接点,表示当前被拦截的方法。 + * @return 返回被拦截方法的执行结果。 + * @throws Throwable 如果执行过程中发生异常,则抛出。 + */ + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/AsyncAnnotationAspect.java new file mode 100644 index 000000000..b7eb3ac74 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/AsyncAnnotationAspect.java @@ -0,0 +1,51 @@ +package com.logpm.factorydata.suofeiya.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springblade.common.annotations.LogpmAsync; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +@Aspect +@Component +@Slf4j +@Order(-1) +public class AsyncAnnotationAspect { + + /** + * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 + * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 + */ + @Around("@annotation(org.springblade.common.annotations.LogpmAsync)") + public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + + LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); + String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); + + // 在方法执行前的操作 + String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); + DynamicDataSourceContextHolder.push(tenantId); + + // 执行原方法 + Object result = joinPoint.proceed(); + + // 在方法执行后的操作 + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/AnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/RabbitAnnotationAspect.java similarity index 53% rename from blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/AnnotationAspect.java rename to blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/RabbitAnnotationAspect.java index 9a262a914..bed29b6e4 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/AnnotationAspect.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-suofeiya/src/main/java/com/logpm/factorydata/suofeiya/aspect/RabbitAnnotationAspect.java @@ -1,51 +1,18 @@ package com.logpm.factorydata.suofeiya.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.reflect.MethodSignature; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; -import java.lang.reflect.Method; - @Aspect @Component -public class AnnotationAspect { - - /** - * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 - * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 - */ - @Around("@annotation(org.springframework.scheduling.annotation.Async)") - public Object asyncAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - - Async myAsync = method.getAnnotation(Async.class); - String annotationValue = myAsync.value(); +@Slf4j +public class RabbitAnnotationAspect { - if(StringUtil.isNotBlank(annotationValue) && annotationValue.equals("asyncExecutor")){ - // 在方法执行前的操作 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - - // 执行原方法 - Object result = joinPoint.proceed(); - - // 在方法执行后的操作 - DynamicDataSourceContextHolder.poll(); - - return result; - }else{ - return joinPoint.proceed(); - } - - } /** * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 @@ -55,7 +22,7 @@ public class AnnotationAspect { * @throws Throwable 如果执行过程中发生异常,则抛出。 */ @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行前,获取当前租户ID并设置到数据源上下文中 String tenantId = AuthUtil.getTenantId(); DynamicDataSourceContextHolder.push(tenantId); @@ -67,7 +34,7 @@ public class AnnotationAspect { } @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行前,获取当前租户ID并设置到数据源上下文中 String tenantId = AuthUtil.getTenantId(); DynamicDataSourceContextHolder.push(tenantId); diff --git a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/AsyncAnnotationAspect.java new file mode 100644 index 000000000..2f48b3750 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/AsyncAnnotationAspect.java @@ -0,0 +1,51 @@ +package com.logpm.factorydata.zbom.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springblade.common.annotations.LogpmAsync; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; + +@Aspect +@Component +@Slf4j +@Order(-1) +public class AsyncAnnotationAspect { + + /** + * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 + * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 + */ + @Around("@annotation(org.springblade.common.annotations.LogpmAsync)") + public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + + LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); + String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); + + // 在方法执行前的操作 + String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); + DynamicDataSourceContextHolder.push(tenantId); + + // 执行原方法 + Object result = joinPoint.proceed(); + + // 在方法执行后的操作 + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/AnnotationAspect.java b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/RabbitAnnotationAspect.java similarity index 53% rename from blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/AnnotationAspect.java rename to blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/RabbitAnnotationAspect.java index 3265b3946..c9f433a93 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/AnnotationAspect.java +++ b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/aspect/RabbitAnnotationAspect.java @@ -1,51 +1,18 @@ package com.logpm.factorydata.zbom.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.reflect.MethodSignature; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; -import java.lang.reflect.Method; - @Aspect @Component -public class AnnotationAspect { - - /** - * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 - * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 - */ - @Around("@annotation(org.springframework.scheduling.annotation.Async)") - public Object asyncAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); - - Async myAsync = method.getAnnotation(Async.class); - String annotationValue = myAsync.value(); +@Slf4j +public class RabbitAnnotationAspect { - if(StringUtil.isNotBlank(annotationValue) && annotationValue.equals("asyncExecutor")){ - // 在方法执行前的操作 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - - // 执行原方法 - Object result = joinPoint.proceed(); - - // 在方法执行后的操作 - DynamicDataSourceContextHolder.poll(); - - return result; - }else{ - return joinPoint.proceed(); - } - - } /** * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 @@ -55,7 +22,7 @@ public class AnnotationAspect { * @throws Throwable 如果执行过程中发生异常,则抛出。 */ @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行前,获取当前租户ID并设置到数据源上下文中 String tenantId = AuthUtil.getTenantId(); DynamicDataSourceContextHolder.push(tenantId); @@ -67,7 +34,7 @@ public class AnnotationAspect { } @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { // 在方法执行前,获取当前租户ID并设置到数据源上下文中 String tenantId = AuthUtil.getTenantId(); DynamicDataSourceContextHolder.push(tenantId); diff --git a/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java index 676e7fe52..a7bb19520 100644 --- a/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/AsyncAnnotationAspect.java @@ -1,50 +1,28 @@ package com.logpm.factory.aspect; -import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; -import com.xxl.job.core.handler.annotation.XxlJob; -import lombok.AllArgsConstructor; -import org.aspectj.lang.JoinPoint; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; -import org.springblade.common.cache.CacheNames; -import org.springblade.common.constant.LauncherConstant; -import org.springblade.core.launch.constant.AppConstant; -import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.ObjectUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springblade.core.tool.utils.ThreadLocalUtil; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpHeaders; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; @Aspect @Component -@AllArgsConstructor +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { - private final BladeRedis bladeRedis; - private final Environment environment; + /** + * 定义一个切点,匹配所有带有@LogpmAsync("asyncExecutor")注解的方法。 + * 注意:实际上Spring Framework自带对@LogpmAsync("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。 + */ @Around("@annotation(org.springblade.common.annotations.LogpmAsync)") public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable { @@ -53,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -63,94 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - - - - @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") - public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { - - - JSONObject jsonObject = mockLogin(); - MockHttpServletRequest mockRequest = new MockHttpServletRequest(); - mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); - - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); - httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - ThreadLocalUtil.put("bladeContext", httpHeaders); - - DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - - return result; - - - } - - private JSONObject mockLogin(){ - - String account ="shujutongbu"; - JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); - if(Objects.isNull(data)){ - String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; - cn.hutool.http.HttpRequest urlRequest = HttpRequest.post(url); - urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); - urlRequest.header("Tenant-Id", "627683"); - - Map params = new HashMap<>(); - params.put("grant_type", "local_server"); - params.put("scope", "all"); - params.put("username", account); - params.put("tenantId", "627683"); - HttpResponse execute = urlRequest.form(params).execute(); - String body = execute.body(); - data = JSONObject.parseObject(body); - bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); - } - return data; } - - } diff --git a/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/JobAnnotationAspect.java b/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/JobAnnotationAspect.java new file mode 100644 index 000000000..e90fe3e7e --- /dev/null +++ b/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/JobAnnotationAspect.java @@ -0,0 +1,82 @@ +package com.logpm.factory.aspect; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.AllArgsConstructor; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.common.cache.CacheNames; +import org.springblade.common.constant.LauncherConstant; +import org.springblade.core.redis.cache.BladeRedis; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Aspect +@Component +@AllArgsConstructor +public class JobAnnotationAspect { + private final BladeRedis bladeRedis; + private final Environment environment; + + @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") + public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + + JSONObject jsonObject = mockLogin(); + MockHttpServletRequest mockRequest = new MockHttpServletRequest(); + mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); + httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + ThreadLocalUtil.put("bladeContext", httpHeaders); + + DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + + return result; + + + } + + private JSONObject mockLogin(){ + + String account ="shujutongbu"; + JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); + if(Objects.isNull(data)){ + String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; + HttpRequest urlRequest = HttpRequest.post(url); + urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + urlRequest.header("Tenant-Id", "627683"); + + Map params = new HashMap<>(); + params.put("grant_type", "local_server"); + params.put("scope", "all"); + params.put("username", account); + params.put("tenantId", "627683"); + HttpResponse execute = urlRequest.form(params).execute(); + String body = execute.body(); + data = JSONObject.parseObject(body); + bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); + } + return data; + } + + +} diff --git a/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/RabbitAnnotationAspect.java b/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/RabbitAnnotationAspect.java new file mode 100644 index 000000000..eceec509f --- /dev/null +++ b/blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/RabbitAnnotationAspect.java @@ -0,0 +1,48 @@ +package com.logpm.factory.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RabbitAnnotationAspect { + + + /** + * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 + * + * @param joinPoint 切面的连接点,表示当前被拦截的方法。 + * @return 返回被拦截方法的执行结果。 + * @throws Throwable 如果执行过程中发生异常,则抛出。 + */ + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/JobAnnotationAspect.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/JobAnnotationAspect.java new file mode 100644 index 000000000..0e315907c --- /dev/null +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/JobAnnotationAspect.java @@ -0,0 +1,82 @@ +package com.logpm.patch.aspect; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.AllArgsConstructor; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.common.cache.CacheNames; +import org.springblade.common.constant.LauncherConstant; +import org.springblade.core.redis.cache.BladeRedis; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Aspect +@Component +@AllArgsConstructor +public class JobAnnotationAspect { + private final BladeRedis bladeRedis; + private final Environment environment; + + @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") + public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { + + + JSONObject jsonObject = mockLogin(); + MockHttpServletRequest mockRequest = new MockHttpServletRequest(); + mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); + httpHeaders.add( "Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + ThreadLocalUtil.put("bladeContext", httpHeaders); + + DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + + return result; + + + } + + private JSONObject mockLogin(){ + + String account ="shujutongbu"; + JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); + if(Objects.isNull(data)){ + String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; + HttpRequest urlRequest = HttpRequest.post(url); + urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); + urlRequest.header("Tenant-Id", "627683"); + + Map params = new HashMap<>(); + params.put("grant_type", "local_server"); + params.put("scope", "all"); + params.put("username", account); + params.put("tenantId", "627683"); + HttpResponse execute = urlRequest.form(params).execute(); + String body = execute.body(); + data = JSONObject.parseObject(body); + bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); + } + return data; + } + + +} diff --git a/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/RabbitAnnotationAspect.java b/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/RabbitAnnotationAspect.java new file mode 100644 index 000000000..7fac6567b --- /dev/null +++ b/blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/RabbitAnnotationAspect.java @@ -0,0 +1,48 @@ +package com.logpm.patch.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RabbitAnnotationAspect { + + + /** + * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 + * + * @param joinPoint 切面的连接点,表示当前被拦截的方法。 + * @return 返回被拦截方法的执行结果。 + * @throws Throwable 如果执行过程中发生异常,则抛出。 + */ + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java index 57e004033..5fcbeef6e 100644 --- a/blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-supervise/src/main/java/com/logpm/supervise/aspect/AsyncAnnotationAspect.java @@ -1,20 +1,22 @@ package com.logpm.supervise.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { /** @@ -29,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -39,41 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - } - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") -// public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - -// @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") -// public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { -// // 在方法执行前,获取当前租户ID并设置到数据源上下文中 -// String tenantId = AuthUtil.getTenantId(); -// DynamicDataSourceContextHolder.push(tenantId); -// // 执行原方法 -// Object result = joinPoint.proceed(); -// // 在方法执行后,从数据源上下文中移除租户ID -// DynamicDataSourceContextHolder.poll(); -// return result; -// } - } diff --git a/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java index 9f0d24146..23ac93519 100644 --- a/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/AsyncAnnotationAspect.java @@ -1,20 +1,22 @@ package com.logpm.trunkline.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { /** @@ -29,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -39,41 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; } } diff --git a/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/RabbitAnnotationAspect.java b/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/RabbitAnnotationAspect.java new file mode 100644 index 000000000..78eca8351 --- /dev/null +++ b/blade-service/logpm-trunkline/src/main/java/com/logpm/trunkline/aspect/RabbitAnnotationAspect.java @@ -0,0 +1,48 @@ +package com.logpm.trunkline.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RabbitAnnotationAspect { + + + /** + * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 + * + * @param joinPoint 切面的连接点,表示当前被拦截的方法。 + * @return 返回被拦截方法的执行结果。 + * @throws Throwable 如果执行过程中发生异常,则抛出。 + */ + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + +} diff --git a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java index 11a0d9e59..5e5895f27 100644 --- a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java +++ b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/AsyncAnnotationAspect.java @@ -1,20 +1,22 @@ package com.logpm.warehouse.aspect; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springblade.common.annotations.LogpmAsync; import org.springblade.core.secure.utils.AuthUtil; -import org.springblade.core.tool.utils.StringUtil; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component +@Slf4j +@Order(-1) public class AsyncAnnotationAspect { /** @@ -29,9 +31,13 @@ public class AsyncAnnotationAspect { LogpmAsync myAsync = method.getAnnotation(LogpmAsync.class); String annotationValue = myAsync.value(); + log.info(">>>>>>>>>>>>>>>>>> annotationValue={}",annotationValue); + + log.info("@LogpmAsync注解的值为asyncExecutor,进行数据源切换 "); // 在方法执行前的操作 String tenantId = AuthUtil.getTenantId(); + log.info(">> tenandId {} ",tenantId); DynamicDataSourceContextHolder.push(tenantId); // 执行原方法 @@ -39,40 +45,7 @@ public class AsyncAnnotationAspect { // 在方法执行后的操作 DynamicDataSourceContextHolder.poll(); - return result; - - } - - /** - * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 - * - * @param joinPoint 切面的连接点,表示当前被拦截的方法。 - * @return 返回被拦截方法的执行结果。 - * @throws Throwable 如果执行过程中发生异常,则抛出。 - */ - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") - public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; - } - - @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") - public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { - // 在方法执行前,获取当前租户ID并设置到数据源上下文中 - String tenantId = AuthUtil.getTenantId(); - DynamicDataSourceContextHolder.push(tenantId); - // 执行原方法 - Object result = joinPoint.proceed(); - // 在方法执行后,从数据源上下文中移除租户ID - DynamicDataSourceContextHolder.poll(); - return result; } } diff --git a/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/RabbitAnnotationAspect.java b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/RabbitAnnotationAspect.java new file mode 100644 index 000000000..c8617a0c0 --- /dev/null +++ b/blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/aspect/RabbitAnnotationAspect.java @@ -0,0 +1,48 @@ +package com.logpm.warehouse.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springblade.core.secure.utils.AuthUtil; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RabbitAnnotationAspect { + + + /** + * 该方法为一个切面,用于对标记了@RabbitListener注解的方法进行增强,主要增加了日志记录和数据源上下文处理的功能。 + * + * @param joinPoint 切面的连接点,表示当前被拦截的方法。 + * @return 返回被拦截方法的执行结果。 + * @throws Throwable 如果执行过程中发生异常,则抛出。 + */ + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)") + public Object rabbitMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + + @Around("@annotation(org.springframework.amqp.rabbit.annotation.RabbitHandler)") + public Object rabbitHandlerMethods(ProceedingJoinPoint joinPoint) throws Throwable { + // 在方法执行前,获取当前租户ID并设置到数据源上下文中 + String tenantId = AuthUtil.getTenantId(); + DynamicDataSourceContextHolder.push(tenantId); + // 执行原方法 + Object result = joinPoint.proceed(); + // 在方法执行后,从数据源上下文中移除租户ID + DynamicDataSourceContextHolder.poll(); + return result; + } + +}