完善 yudao-spring-boot-starter-env 组件,完成 dubbo 组件
parent
2faaa65325
commit
917746a1a7
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
dubboProviderRouterTagFilter=cn.iocoder.yudao.framework.env.core.dubbo.DubboProviderRouterTagFilter
|
|
@ -0,0 +1 @@
|
|||
dubboConsumerRouterTagClusterInterceptor=cn.iocoder.yudao.framework.env.core.dubbo.DubboConsumerRouterTagClusterInterceptor
|
|
@ -1 +0,0 @@
|
|||
dubboConsumerRouterTagClusterInterceptor=cn.iocoder.mall.dubbo.core.cluster.interceptor.DubboConsumerRouterTagClusterInterceptor
|
Loading…
Reference in New Issue