Skip to content

Commit

Permalink
Merge pull request #1453 from chengyouling/develop-dubbo
Browse files Browse the repository at this point in the history
add dubbo3 route plugin
  • Loading branch information
Sherlockhan authored Mar 18, 2024
2 parents da3e237 + 7dbfc32 commit 6da99fd
Show file tree
Hide file tree
Showing 41 changed files with 752 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.huaweicloud.sermant.router.dubbo.declarer;

import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;

/**
* 增强AbstractDirectory的子类的doList方法,筛选标签应用的地址
Expand All @@ -34,6 +35,8 @@ public class AbstractDirectoryDeclarer extends AbstractDeclarer {

private static final String METHOD_NAME = "doList";

private static final int PARAMETER_COUNT = 1;

/**
* 构造方法
*/
Expand All @@ -45,4 +48,14 @@ public AbstractDirectoryDeclarer() {
public ClassMatcher getClassMatcher() {
return ClassMatcher.isExtendedFrom(APACHE_ENHANCE_CLASS).or(ClassMatcher.isExtendedFrom(ALIBABA_ENHANCE_CLASS));
}

/**
* 获取方法匹配器
*
* @return 方法匹配器
*/
@Override
public MethodMatcher getMethodMatcher() {
return super.getMethodMatcher().and(MethodMatcher.paramCountEquals(PARAMETER_COUNT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.common.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.dubbo.service.LaneContextFilterService;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;

import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
import com.huaweicloud.sermant.core.utils.LogUtils;
import com.huaweicloud.sermant.router.dubbo.service.AbstractDirectoryService;
import com.huaweicloud.sermant.router.common.service.AbstractDirectoryService;

/**
* 增强AbstractDirectory的子类的doList方法,筛选标签应用的地址
Expand All @@ -29,6 +29,7 @@
* @since 2021-06-28
*/
public class AbstractDirectoryInterceptor extends AbstractInterceptor {
private static final int INDEX = 0;
private final AbstractDirectoryService abstractDirectoryService;

/**
Expand All @@ -46,7 +47,8 @@ public ExecuteContext before(ExecuteContext context) {

@Override
public ExecuteContext after(ExecuteContext context) {
context.changeResult(abstractDirectoryService.selectInvokers(context.getObject(), context.getArguments(),
Object[] arguments = context.getArguments();
context.changeResult(abstractDirectoryService.selectInvokers(context.getObject(), arguments[INDEX],
context.getResult()));
LogUtils.printDubboRequestAfterPoint(context);
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.config.RouterConfig;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.common.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.dubbo.utils.ParametersUtils;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
import com.huaweicloud.sermant.router.common.handler.Handler;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.common.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils;
import com.huaweicloud.sermant.router.dubbo.handler.AbstractContextFilterHandler;
import com.huaweicloud.sermant.router.dubbo.handler.LaneContextFilterHandler;
import com.huaweicloud.sermant.router.dubbo.handler.RouteContextFilterHandler;
import com.huaweicloud.sermant.router.dubbo.service.DubboConfigService;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;

import java.util.ArrayList;
import java.util.Comparator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager;
import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.service.DubboConfigService;

import java.util.concurrent.atomic.AtomicBoolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package com.huaweicloud.sermant.router.dubbo.utils;

import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.config.RouterConfig;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;

import java.util.HashMap;
import java.util.Locale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.huaweicloud.sermant.core.service.ServiceManager;
import com.huaweicloud.sermant.router.dubbo.TestDubboConfigService;
import com.huaweicloud.sermant.router.dubbo.service.LaneContextFilterService;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtilsTest.ApacheInvoker;

import org.apache.dubbo.rpc.RpcInvocation;
import org.junit.AfterClass;
Expand Down Expand Up @@ -53,6 +52,9 @@ public class LaneContextFilterHandlerTest {

private final ApacheInvoker<?> invoker;

private static final org.apache.dubbo.common.URL APACHE_URL = org.apache.dubbo.common.URL
.valueOf("dubbo://localhost:8081/com.huaweicloud.foo.FooTest?foo=bar&version=0.0.1");

/**
* UT执行前进行mock
*/
Expand Down Expand Up @@ -130,4 +132,31 @@ public void setReturnEmpty(boolean returnEmpty) {
this.returnEmpty = returnEmpty;
}
}

public static class ApacheInvoker<T> implements org.apache.dubbo.rpc.Invoker<T> {
@Override
public Class<T> getInterface() {
return null;
}

@Override
public org.apache.dubbo.rpc.Result invoke(org.apache.dubbo.rpc.Invocation invocation)
throws org.apache.dubbo.rpc.RpcException {
return null;
}

@Override
public org.apache.dubbo.common.URL getUrl() {
return APACHE_URL;
}

@Override
public boolean isAvailable() {
return false;
}

@Override
public void destroy() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package com.huaweicloud.sermant.router.dubbo.interceptor;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.config.RouterConfig;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;

import com.alibaba.dubbo.config.ApplicationConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.config.RouterConfig;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.common.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.common.utils.FlowContextUtils;
import com.huaweicloud.sermant.router.config.cache.ConfigCache;
import com.huaweicloud.sermant.router.config.entity.Match;
Expand All @@ -32,10 +34,8 @@
import com.huaweicloud.sermant.router.config.entity.ValueMatch;
import com.huaweicloud.sermant.router.config.utils.FlowRuleUtils;
import com.huaweicloud.sermant.router.config.utils.RuleUtils;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.strategy.RuleStrategyHandler;
import com.huaweicloud.sermant.router.dubbo.strategy.TypeStrategyChooser;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;

import java.util.Collections;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.huaweicloud.sermant.router.dubbo.handler;

import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.config.cache.ConfigCache;
Expand All @@ -27,7 +28,6 @@
import com.huaweicloud.sermant.router.config.entity.Rule;
import com.huaweicloud.sermant.router.config.entity.ValueMatch;
import com.huaweicloud.sermant.router.config.utils.TagRuleUtils;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.strategy.RuleStrategyHandler;

import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package com.huaweicloud.sermant.router.dubbo.service;

import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.request.RequestTag;
import com.huaweicloud.sermant.router.common.service.AbstractDirectoryService;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.common.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.handler.HandlerChainEntry;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;

import java.util.List;
import java.util.Map;
Expand All @@ -47,7 +48,7 @@ public class AbstractDirectoryServiceImpl implements AbstractDirectoryService {
* 筛选标签invoker
*
* @param registryDirectory RegistryDirectory
* @param arguments 参数
* @param invocation 参数
* @param result invokers
* @return invokers
* @see com.alibaba.dubbo.registry.integration.RegistryDirectory
Expand All @@ -56,14 +57,13 @@ public class AbstractDirectoryServiceImpl implements AbstractDirectoryService {
* @see org.apache.dubbo.rpc.Invoker
*/
@Override
public Object selectInvokers(Object registryDirectory, Object[] arguments, Object result) {
if (arguments == null || arguments.length == 0) {
public Object selectInvokers(Object registryDirectory, Object invocation, Object result) {
if (invocation == null) {
return result;
}
if (!(result instanceof List<?>)) {
return result;
}
Object invocation = arguments[0];
putAttachment(invocation);
List<Object> invokers = (List<Object>) result;
Map<String, String> queryMap = DubboReflectUtils.getQueryMap(registryDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.huaweicloud.sermant.router.dubbo.service;

import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;
import com.huaweicloud.sermant.router.common.utils.DubboReflectUtils;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.router.dubbo.service;

import com.huaweicloud.sermant.router.common.mapper.AbstractMetadataMapper;
import com.huaweicloud.sermant.router.common.service.InvokerRuleStrategyService;
import com.huaweicloud.sermant.router.dubbo.strategy.RuleStrategyHandler;

/**
* 设置InvokerRuleStrategy的service
*
* @author chengyouling
* @since 2024-02-23
*/
public class InvokerRuleStrategyServiceImpl implements InvokerRuleStrategyService {
@Override
public void builedDubbo3RuleStrategy(AbstractMetadataMapper<Object> mapper) {
RuleStrategyHandler.INSTANCE.builedDubbo3Mapper(mapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package com.huaweicloud.sermant.router.dubbo.service;

import com.huaweicloud.sermant.router.common.cache.DubboCache;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.config.cache.ConfigCache;
import com.huaweicloud.sermant.router.config.entity.Protocol;
import com.huaweicloud.sermant.router.config.entity.Route;
import com.huaweicloud.sermant.router.config.entity.RouterConfiguration;
import com.huaweicloud.sermant.router.config.entity.Rule;
import com.huaweicloud.sermant.router.config.utils.RuleUtils;
import com.huaweicloud.sermant.router.dubbo.cache.DubboCache;
import com.huaweicloud.sermant.router.dubbo.utils.RouteUtils;

import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.huaweicloud.sermant.router.dubbo.strategy;

import com.huaweicloud.sermant.router.common.mapper.AbstractMetadataMapper;
import com.huaweicloud.sermant.router.common.mapper.DefaultMapper;
import com.huaweicloud.sermant.router.config.entity.Rule;
import com.huaweicloud.sermant.router.config.strategy.RuleStrategy;
import com.huaweicloud.sermant.router.dubbo.strategy.rule.InvokerRuleStrategy;
Expand All @@ -35,10 +37,15 @@ public enum RuleStrategyHandler {
*/
INSTANCE;

private final RuleStrategy<Object> ruleStrategy;
private RuleStrategy<Object> ruleStrategy = new InvokerRuleStrategy(new DefaultMapper());

RuleStrategyHandler() {
this.ruleStrategy = new InvokerRuleStrategy();
/**
* 构建dubbo3.x应用注册时的metadata处理类
*
* @param mapper 元数据meta处理mapper
*/
public void builedDubbo3Mapper(AbstractMetadataMapper<Object> mapper) {
ruleStrategy = new InvokerRuleStrategy(mapper);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.huaweicloud.sermant.router.dubbo.strategy.rule;

import com.huaweicloud.sermant.router.common.mapper.AbstractMetadataMapper;
import com.huaweicloud.sermant.router.config.strategy.AbstractRuleStrategy;
import com.huaweicloud.sermant.router.dubbo.strategy.instance.MatchInstanceStrategy;
import com.huaweicloud.sermant.router.dubbo.strategy.instance.MismatchInstanceStrategy;
import com.huaweicloud.sermant.router.dubbo.utils.DubboReflectUtils;

/**
* 路由规则匹配策略
Expand All @@ -30,9 +30,10 @@
public class InvokerRuleStrategy extends AbstractRuleStrategy<Object> {
/**
* 构造方法
*
* @param mapper metadata处理
*/
public InvokerRuleStrategy() {
super("dubbo", new MatchInstanceStrategy(), new MismatchInstanceStrategy(),
obj -> DubboReflectUtils.getParameters(DubboReflectUtils.getUrl(obj)));
public InvokerRuleStrategy(AbstractMetadataMapper<Object> mapper) {
super("dubbo", new MatchInstanceStrategy(), new MismatchInstanceStrategy(), mapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

com.huaweicloud.sermant.router.dubbo.service.AbstractDirectoryServiceImpl
com.huaweicloud.sermant.router.dubbo.service.ClusterUtilsServiceImpl
com.huaweicloud.sermant.router.dubbo.service.LaneContextFilterServiceImpl
com.huaweicloud.sermant.router.dubbo.service.LaneContextFilterServiceImpl
com.huaweicloud.sermant.router.dubbo.service.InvokerRuleStrategyServiceImpl
Loading

0 comments on commit 6da99fd

Please sign in to comment.