You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/09/03 10:05:25 UTC
[GitHub] hupuxiaojun commented on issue #2228: [Dubbo- support tag router
feature] Add a new Router implement -- TagRouter
hupuxiaojun commented on issue #2228: [Dubbo- support tag router feature] Add a new Router implement -- TagRouter
URL: https://github.com/apache/incubator-dubbo/pull/2228#issuecomment-418067095
TagRouter feature is very good, but i think an LoadBalance Wrapper can get same result ,and has smaller changes with source code:
```java
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.cluster.LoadBalance;
import java.util.ArrayList;
import java.util.List;
public class LoadbalanceTagFilterWrapper implements LoadBalance {
private static final Logger logger = LoggerFactory.getLogger(LoadbalanceTagFilterWrapper.class);
private LoadBalance loadBalance;
public LoadbalanceTagFilterWrapper(LoadBalance loadBalance) {
this.loadBalance = loadBalance;
}
@Override
public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
List<Invoker<T>> filteredInvokers = filterInvokers(invokers);
return loadBalance.select(filteredInvokers, url, invocation);
}
private <T> List<Invoker<T>> filterInvokers(List<Invoker<T>> invokers) {
// filter
List<Invoker<T>> result = new ArrayList<>();
// all invokers that don't have "tag" parameter in url is a normal invoker
List<Invoker<T>> normalResult = new ArrayList<>();
try {
// Dynamic param
String tag = RpcContext.getContext().getAttachment(Constants.REQUEST_TAG_KEY);
for (Invoker<T> invoker : invokers) {
if (StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))) {
// all invokers that don't have "tag" parameter in url is a normal invoker
normalResult.add(invoker);
} else {
if (invoker.getUrl().getParameter(Constants.TAG_KEY).equals(tag)) {
result.add(invoker);
}
}
}
// If no invoker be selected, downgrade to normal invokers
if (result.isEmpty()) {
return normalResult;
}
return result;
} catch (Exception e) {
logger.error("Route by tag error,return all invokers.", e);
}
// Downgrade to all invokers
return invokers;
}
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org