Browse Source

feat(all): 修改mq发送消息根据租户切换的问题

dist.1.3.0
zhaoqiaobo 11 months ago
parent
commit
ad317385be
  1. 70
      blade-biz-common/src/main/java/org/springblade/common/model/HeaderValueHolder.java
  2. 1056
      blade-biz-common/src/main/java/org/springblade/common/model/MockHttpServletRequest.java
  3. 68
      blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/CustomMessageConverter.java
  4. 17
      blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/RabbitConfig.java
  5. 8
      blade-service/logpm-factory-data/logpm-factory-data-base/src/main/resources/application-dev.yml
  6. 8
      blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/resources/application-dev.yml
  7. 33
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/aspect/AnnotationAspect.java
  8. 57
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/CustomMessageConverter.java
  9. 18
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/java/com/logpm/factorydata/olo/config/RabbitConfig.java
  10. 13
      blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/resources/application-dev.yml

70
blade-biz-common/src/main/java/org/springblade/common/model/HeaderValueHolder.java

@ -0,0 +1,70 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springblade.common.model;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
class HeaderValueHolder {
private final List<Object> values = new LinkedList();
HeaderValueHolder() {
}
void setValue(@Nullable Object value) {
this.values.clear();
if (value != null) {
this.values.add(value);
}
}
void addValue(Object value) {
this.values.add(value);
}
void addValues(Collection<?> values) {
this.values.addAll(values);
}
void addValueArray(Object values) {
CollectionUtils.mergeArrayIntoCollection(values, this.values);
}
List<Object> getValues() {
return Collections.unmodifiableList(this.values);
}
List<String> getStringValues() {
List<String> stringList = new ArrayList(this.values.size());
Iterator var2 = this.values.iterator();
while(var2.hasNext()) {
Object value = var2.next();
stringList.add(value.toString());
}
return Collections.unmodifiableList(stringList);
}
@Nullable
Object getValue() {
return !this.values.isEmpty() ? this.values.get(0) : null;
}
@Nullable
String getStringValue() {
return !this.values.isEmpty() ? String.valueOf(this.values.get(0)) : null;
}
}

1056
blade-biz-common/src/main/java/org/springblade/common/model/MockHttpServletRequest.java

File diff suppressed because it is too large Load Diff

68
blade-service/logpm-factory-data/logpm-factory-data-base/src/main/java/com/logpm/factorydata/base/config/CustomMessageConverter.java

@ -0,0 +1,68 @@
package com.logpm.factorydata.base.config;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springblade.common.model.MockHttpServletRequest;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springblade.core.tool.utils.WebUtil;
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.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
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<String, Object> 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;
}
}

17
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;
}
}

8
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}

8
blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/resources/application-dev.yml

@ -59,13 +59,13 @@ spring:
url: ${blade.datasource.factorydata-jinpai.master.url}
username: ${blade.datasource.factorydata-jinpai.master.username}
password: ${blade.datasource.factorydata-jinpai.master.password}
slave:
627683:
druid:
#独立校验配置
validation-query: select 1
#oracle校验
#validation-query: select 1 from dual
url: ${blade.datasource.factorydata-jinpai.slave.url}
username: ${blade.datasource.factorydata-jinpai.slave.username}
password: ${blade.datasource.factorydata-jinpai.slave.password}
url: ${blade.datasource.factorydata-jinpai.627683.url}
username: ${blade.datasource.factorydata-jinpai.627683.username}
password: ${blade.datasource.factorydata-jinpai.627683.password}

33
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;
}
}

57
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.common.model.MockHttpServletRequest;
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.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<String, Object> 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());
}
}

18
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;
}
}

13
blade-service/logpm-factory-data/logpm-factory-data-olo/src/main/resources/application-dev.yml

@ -1,7 +1,6 @@
#服务器端口
server:
port: 18930
#数据源配置
#spring:
# datasource:
@ -59,13 +58,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

Loading…
Cancel
Save