完善 yudao-spring-boot-starter-env 组件,完成 dubbo 组件

pull/4/head
YunaiV 2022-06-25 22:01:38 +08:00
parent 2faaa65325
commit 917746a1a7
7 changed files with 36 additions and 24 deletions

View File

@ -247,6 +247,11 @@
<artifactId>dubbo-common</artifactId> <!-- 兜底,保证在不引入 spring-cloud-starter-dubbo 时,注解等不报错 -->
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-cluster</artifactId> <!-- 兜底,保证在不引入 spring-cloud-starter-dubbo 时,注解等不报错 -->
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-rpc</artifactId>

View File

@ -56,6 +56,15 @@
<artifactId>feign-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-cluster</artifactId>
</dependency>
<!-- Registry 注册中心相关 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>

View File

@ -1,16 +1,15 @@
package cn.iocoder.mall.dubbo.core.cluster.interceptor;
package cn.iocoder.yudao.framework.env.core.dubbo;
import cn.iocoder.common.framework.util.StringUtils;
import cn.iocoder.mall.dubbo.core.filter.DubboProviderRouterTagFilter;
import cn.iocoder.mall.dubbo.core.router.DubboRouterTagContextHolder;
import cn.iocoder.yudao.framework.env.core.context.EnvContextHolder;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor;
import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
import org.springframework.util.StringUtils;
/**
* Consumer Provider {@link DubboRouterTagContextHolder} Tag Dubbo
* Consumer Provider {@link EnvContextHolder} Tag Dubbo
*
* {@link DubboProviderRouterTagFilter}
*
@ -22,9 +21,9 @@ public class DubboConsumerRouterTagClusterInterceptor implements ClusterIntercep
@Override
public void before(AbstractClusterInvoker<?> clusterInvoker, Invocation invocation) {
// 设置 Dubbo Tag 到 Dubbo 隐式传参
String dubboTag = DubboRouterTagContextHolder.getTag();
if (StringUtils.hasText(dubboTag)) {
invocation.setAttachment(CommonConstants.TAG_KEY, dubboTag);
String tag = EnvContextHolder.getTag();
if (StringUtils.hasText(tag)) {
invocation.setAttachment(CommonConstants.TAG_KEY, tag);
}
}

View File

@ -1,8 +1,7 @@
package cn.iocoder.mall.dubbo.core.filter;
package cn.iocoder.yudao.framework.env.core.dubbo;
import cn.iocoder.common.framework.util.StringUtils;
import cn.iocoder.mall.dubbo.core.cluster.interceptor.DubboConsumerRouterTagClusterInterceptor;
import cn.iocoder.mall.dubbo.core.router.DubboRouterTagContextHolder;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.env.core.context.EnvContextHolder;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
@ -15,9 +14,9 @@ import org.apache.dubbo.rpc.cluster.router.tag.TagRouter;
* 2. TODO 绿
*
*
* 1. Consumer Provider {@link DubboConsumerRouterTagClusterInterceptor} {@link DubboRouterTagContextHolder} Tag Dubbo
* 1. Consumer Provider {@link DubboConsumerRouterTagClusterInterceptor} {@link EnvContextHolder} Tag Dubbo
* Dubbo {@link TagRouter} Tag Provider
* 2. Provider Dubbo Tag {@link DubboRouterTagContextHolder}
* 2. Provider Dubbo Tag {@link EnvContextHolder}
* Provider Consumer Provider
*/
@Activate(group = {CommonConstants.PROVIDER, CommonConstants.CONSUMER}, order = -1000)
@ -25,20 +24,19 @@ public class DubboProviderRouterTagFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// 从 Dubbo 隐式传参获得 Dubbo Tag
String dubboTag = invocation.getAttachment(CommonConstants.TAG_KEY);
boolean hasDubboTag = StringUtils.hasText(dubboTag);
if (hasDubboTag) {
invocation.setAttachment(CommonConstants.TAG_KEY, dubboTag);
// 情况一,没有 tag 时,直接调用即可
String tag = invocation.getAttachment(CommonConstants.TAG_KEY);
if (StrUtil.isEmpty(tag)) {
return invoker.invoke(invocation);
}
// 情况二,有 tag 时,从 Dubbo 隐式传参获得 Dubbo Tag
EnvContextHolder.setTag(tag);
// 继续调用
try {
return invoker.invoke(invocation);
} finally {
// 清理
if (hasDubboTag) {
DubboRouterTagContextHolder.clear();
}
EnvContextHolder.removeTag();
}
}

View File

@ -0,0 +1 @@
dubboProviderRouterTagFilter=cn.iocoder.yudao.framework.env.core.dubbo.DubboProviderRouterTagFilter

View File

@ -0,0 +1 @@
dubboConsumerRouterTagClusterInterceptor=cn.iocoder.yudao.framework.env.core.dubbo.DubboConsumerRouterTagClusterInterceptor

View File

@ -1 +0,0 @@
dubboConsumerRouterTagClusterInterceptor=cn.iocoder.mall.dubbo.core.cluster.interceptor.DubboConsumerRouterTagClusterInterceptor