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

This commit is contained in:
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