feat(workflow): 添加 Dubbo租户上下文过滤器

- 新增 DubboTenantFilter 类,用于在 Dubbo 服务调用时传递和设置租户上下文
- 实现了 Filter 接口,通过 RpcContext 获取租户 ID 并设置到 TenantHelper
- 在 META-INF/dubbo/org.apache.dubbo.rpc.Filter 文件中注册过滤器
master
zch 2 weeks ago
parent 7caa10f7a0
commit d45241c20f

@ -0,0 +1,44 @@
package org.dromara.workflow.filter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.tenant.helper.TenantHelper;
/**
* Dubbo
* Dubbo
*
* @author Claude
*/
@Slf4j
@Activate(group = {CommonConstants.PROVIDER})
public class DubboTenantFilter implements Filter {
private static final String TENANT_ID_KEY = "tenantId";
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// 获取调用方传递的租户ID
String tenantId = RpcContext.getContext().getAttachment(TENANT_ID_KEY);
if (StringUtils.isNotBlank(tenantId)) {
// 设置租户上下文
TenantHelper.setDynamic(tenantId);
log.debug("Dubbo服务端设置租户上下文: {}", tenantId);
}
try {
// 执行服务调用
return invoker.invoke(invocation);
} finally {
// 清理租户上下文,避免线程复用时的污染
if (StringUtils.isNotBlank(tenantId)) {
TenantHelper.clearDynamic();
log.debug("Dubbo服务端清理租户上下文: {}", tenantId);
}
}
}
}

@ -0,0 +1 @@
dubboTenantFilter=org.dromara.workflow.filter.DubboTenantFilter
Loading…
Cancel
Save