diff --git a/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/CustomMessageConverter.java b/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/CustomMessageConverter.java new file mode 100644 index 000000000..94d1e454e --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/CustomMessageConverter.java @@ -0,0 +1,65 @@ +package com.logpm.factorydata.base.config; + +import cn.hutool.json.JSONUtil; +import org.springblade.core.secure.BladeUser; +import org.springblade.core.secure.utils.AuthUtil; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.core.MessageProperties; +import org.springframework.amqp.support.converter.MessageConversionException; +import org.springframework.amqp.support.converter.MessageConverter; +import org.springframework.http.HttpHeaders; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.util.Map; + +/** + * @author zhaoqiaobo + * @create 2024-05-08 + */ +public class CustomMessageConverter implements MessageConverter { + private String name; + + @Override + public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException { + HttpHeaders headers = (HttpHeaders) ThreadLocalUtil.get("bladeContext"); + if (headers != null && !headers.isEmpty()) { + headers.forEach((key, values) -> { + values.forEach((value) -> { + messageProperties.setHeader(key, new String[]{value}); + }); + }); + } + BladeUser user = AuthUtil.getUser(); + BladeUser bladeUser = new BladeUser(); + bladeUser.setTenantId(user.getTenantId()); + bladeUser.setUserId(user.getUserId()); + bladeUser.setAccount(user.getAccount()); + bladeUser.setRoleId(user.getRoleId()); + messageProperties.setHeader("bladeUser", JSONUtil.toJsonStr(bladeUser)); + return new Message(o.toString().getBytes(), messageProperties); + } + + @Override + public Object fromMessage(Message message) throws MessageConversionException { + MessageProperties messageProperties = message.getMessageProperties(); + Map headers = messageProperties.getHeaders(); + ThreadLocalUtil.put("bladeContext", headers); + Object bladeUser = headers.get("bladeUser"); + MockHttpServletRequest request = new MockHttpServletRequest(); + BladeUser bladeUser1 = JSONUtil.toBean(bladeUser.toString(), BladeUser.class); + request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser1); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + return new String(message.getBody()); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/RabbitConfig.java b/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/RabbitConfig.java new file mode 100644 index 000000000..31b0350c5 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/RabbitConfig.java @@ -0,0 +1,17 @@ +package com.logpm.factorydata.base.config; + +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class RabbitConfig { + + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory factory) { + RabbitTemplate template = new RabbitTemplate(factory); + template.setMessageConverter(new CustomMessageConverter()); + return template; + } +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/resources/application-dev.yml b/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/resources/application-dev.yml index 617a0c5de..799bda9d3 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/resources/application-dev.yml +++ b/blade-service/logpm-factory-data/logpm-factory-data-base/src/main/resources/application-dev.yml @@ -59,13 +59,13 @@ spring: url: ${blade.datasource.factorydata-base.master.url} username: ${blade.datasource.factorydata-base.master.username} password: ${blade.datasource.factorydata-base.master.password} - slave: + 627683: druid: #独立校验配置 validation-query: select 1 #oracle校验 #validation-query: select 1 from dual - url: ${blade.datasource.factorydata-base.slave.url} - username: ${blade.datasource.factorydata-base.slave.username} - password: ${blade.datasource.factorydata-base.slave.password} + url: ${blade.datasource.factorydata-base.627683.url} + username: ${blade.datasource.factorydata-base.627683.username} + password: ${blade.datasource.factorydata-base.627683.password} 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-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AnnotationAspect.java new file mode 100644 index 000000000..b8ad44dc6 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AnnotationAspect.java @@ -0,0 +1,33 @@ +package com.logpm.factorydata.olo.aspect; + +import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; +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 +public class AnnotationAspect { + + /** + * 该方法为一个切面,用于对标记了@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; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/CustomMessageConverter.java b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/CustomMessageConverter.java new file mode 100644 index 000000000..b63f5038f --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/CustomMessageConverter.java @@ -0,0 +1,57 @@ +package com.logpm.factorydata.olo.config; + +import cn.hutool.json.JSONUtil; +import org.springblade.core.secure.BladeUser; +import org.springblade.core.secure.utils.AuthUtil; +import org.springblade.core.tool.utils.ThreadLocalUtil; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.core.MessageProperties; +import org.springframework.amqp.support.converter.MessageConversionException; +import org.springframework.amqp.support.converter.MessageConverter; +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.Map; + +/** + * @author zhaoqiaobo + * @create 2024-05-08 + */ +@Component +public class CustomMessageConverter implements MessageConverter { + @Override + public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException { + HttpHeaders headers = (HttpHeaders) ThreadLocalUtil.get("bladeContext"); + if (headers != null && !headers.isEmpty()) { + headers.forEach((key, values) -> { + values.forEach((value) -> { + messageProperties.setHeader(key, new String[]{value}); + }); + }); + } + BladeUser user = AuthUtil.getUser(); + BladeUser bladeUser = new BladeUser(); + bladeUser.setTenantId(user.getTenantId()); + bladeUser.setUserId(user.getUserId()); + bladeUser.setAccount(user.getAccount()); + bladeUser.setRoleId(user.getRoleId()); + messageProperties.setHeader("bladeUser", JSONUtil.toJsonStr(bladeUser)); + return new Message(o.toString().getBytes(), messageProperties); + } + + @Override + public Object fromMessage(Message message) throws MessageConversionException { + MessageProperties messageProperties = message.getMessageProperties(); + Map headers = messageProperties.getHeaders(); + ThreadLocalUtil.put("bladeContext", headers); + Object bladeUser = headers.get("bladeUser"); + MockHttpServletRequest request = new MockHttpServletRequest(); + BladeUser bladeUser1 = JSONUtil.toBean(bladeUser.toString(), BladeUser.class); + request.setAttribute("_BLADE_USER_REQUEST_ATTR_", bladeUser1); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + return new String(message.getBody()); + } +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/RabbitConfig.java b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/RabbitConfig.java new file mode 100644 index 000000000..46ef3cfc9 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/RabbitConfig.java @@ -0,0 +1,18 @@ +package com.logpm.factorydata.olo.config; + +import com.logpm.factorydata.olo.config.CustomMessageConverter; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class RabbitConfig { + + @Bean + public RabbitTemplate rabbitTemplate(ConnectionFactory factory, CustomMessageConverter customMessageConverter) { + RabbitTemplate template = new RabbitTemplate(factory); + template.setMessageConverter(customMessageConverter); + return template; + } +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/resources/application-dev.yml b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/resources/application-dev.yml index 618fe53f8..ddf6d2ffe 100644 --- a/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/resources/application-dev.yml +++ b/blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/resources/application-dev.yml @@ -59,13 +59,15 @@ spring: url: ${blade.datasource.factorydata-olo.master.url} username: ${blade.datasource.factorydata-olo.master.username} password: ${blade.datasource.factorydata-olo.master.password} - slave: + 627683: druid: #独立校验配置 validation-query: select 1 #oracle校验 #validation-query: select 1 from dual - url: ${blade.datasource.factorydata-olo.slave.url} - username: ${blade.datasource.factorydata-olo.slave.username} - password: ${blade.datasource.factorydata-olo.slave.password} - + url: ${blade.datasource.factorydata-olo.627683.url} + username: ${blade.datasource.factorydata-olo.627683.username} + password: ${blade.datasource.factorydata-olo.627683.password} +blade: + data-scope: + enabled: false diff --git a/blade-service/pom.xml b/blade-service/pom.xml index 20439db5b..c7b5c2aac 100644 --- a/blade-service/pom.xml +++ b/blade-service/pom.xml @@ -95,7 +95,10 @@ blade-scope-api ${bladex.project.version} - + + org.springframework + spring-test +