From 917746a1a756e553a3b581942f6c11c17f1bd550 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 25 Jun 2022 22:01:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20yudao-spring-boot-starter-?= =?UTF-8?q?env=20=E7=BB=84=E4=BB=B6=EF=BC=8C=E5=AE=8C=E6=88=90=20dubbo=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-dependencies/pom.xml | 5 ++++ .../yudao-spring-boot-starter-env/pom.xml | 9 ++++++ ...boConsumerRouterTagClusterInterceptor.java | 15 +++++----- .../dubbo}/DubboProviderRouterTagFilter.java | 28 +++++++++---------- .../dubbo/com.alibaba.dubbo.rpc.Filter | 1 + ...rpc.cluster.interceptor.ClusterInterceptor | 1 + ...rpc.cluster.interceptor.ClusterInterceptor | 1 - 7 files changed, 36 insertions(+), 24 deletions(-) rename {归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/cluster/interceptor => yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo}/DubboConsumerRouterTagClusterInterceptor.java (64%) rename {归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter => yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo}/DubboProviderRouterTagFilter.java (64%) create mode 100644 yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter create mode 100644 yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor delete mode 100644 归档/common/mall-spring-boot-starter-dubbo/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index d4a21d3c..910f4146 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -247,6 +247,11 @@ dubbo-common ${dubbo.version} + + org.apache.dubbo + dubbo-cluster + ${dubbo.version} + cn.iocoder.cloud yudao-spring-boot-starter-rpc diff --git a/yudao-framework/yudao-spring-boot-starter-env/pom.xml b/yudao-framework/yudao-spring-boot-starter-env/pom.xml index 3b5ca077..5e92fc08 100644 --- a/yudao-framework/yudao-spring-boot-starter-env/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-env/pom.xml @@ -56,6 +56,15 @@ feign-core + + org.apache.dubbo + dubbo-common + + + org.apache.dubbo + dubbo-cluster + + com.alibaba.cloud diff --git a/归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/cluster/interceptor/DubboConsumerRouterTagClusterInterceptor.java b/yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo/DubboConsumerRouterTagClusterInterceptor.java similarity index 64% rename from 归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/cluster/interceptor/DubboConsumerRouterTagClusterInterceptor.java rename to yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo/DubboConsumerRouterTagClusterInterceptor.java index 4991f0da..b03c7c2c 100644 --- a/归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/cluster/interceptor/DubboConsumerRouterTagClusterInterceptor.java +++ b/yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo/DubboConsumerRouterTagClusterInterceptor.java @@ -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); } } diff --git a/归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderRouterTagFilter.java b/yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo/DubboProviderRouterTagFilter.java similarity index 64% rename from 归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderRouterTagFilter.java rename to yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo/DubboProviderRouterTagFilter.java index a7dc1162..d24f79bd 100644 --- a/归档/common/mall-spring-boot-starter-dubbo/src/main/java/cn/iocoder/mall/dubbo/core/filter/DubboProviderRouterTagFilter.java +++ b/yudao-framework/yudao-spring-boot-starter-env/src/main/java/cn/iocoder/yudao/framework/env/core/dubbo/DubboProviderRouterTagFilter.java @@ -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(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter b/yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter new file mode 100644 index 00000000..c0984fdb --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/com.alibaba.dubbo.rpc.Filter @@ -0,0 +1 @@ +dubboProviderRouterTagFilter=cn.iocoder.yudao.framework.env.core.dubbo.DubboProviderRouterTagFilter diff --git a/yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor b/yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor new file mode 100644 index 00000000..03d7a576 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-env/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor @@ -0,0 +1 @@ +dubboConsumerRouterTagClusterInterceptor=cn.iocoder.yudao.framework.env.core.dubbo.DubboConsumerRouterTagClusterInterceptor diff --git a/归档/common/mall-spring-boot-starter-dubbo/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor b/归档/common/mall-spring-boot-starter-dubbo/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor deleted file mode 100644 index bba11339..00000000 --- a/归档/common/mall-spring-boot-starter-dubbo/src/main/resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.interceptor.ClusterInterceptor +++ /dev/null @@ -1 +0,0 @@ -dubboConsumerRouterTagClusterInterceptor=cn.iocoder.mall.dubbo.core.cluster.interceptor.DubboConsumerRouterTagClusterInterceptor