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 09:36:29 UTC
[GitHub] hupuxiaojun commented on a change in pull request #2228: [Dubbo-
support tag router feature] Add a new Router implement -- TagRouter
hupuxiaojun commented on a change in pull request #2228: [Dubbo- support tag router feature] Add a new Router implement -- TagRouter
URL: https://github.com/apache/incubator-dubbo/pull/2228#discussion_r214629281
##########
File path: dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java
##########
@@ -0,0 +1,100 @@
+package org.apache.dubbo.rpc.cluster.router.tag;
+
+
+import org.apache.dubbo.common.Constants;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.cluster.Router;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author kirito.moe@foxmail.com
+ * Date 2018-08-03
+ */
+public class TagRouter implements Router, Comparable<Router> {
+
+ private static final Logger logger = LoggerFactory.getLogger(TagRouter.class);
+
+ private final int priority;
+ private final URL url;
+
+ public static final URL ROUTER_URL =
+ new URL("tag"
+ , Constants.ANYHOST_VALUE, 0
+ , Constants.ANY_VALUE)
+ .addParameters(
+ Constants.RUNTIME_KEY, "true"
+ );
+
+ public TagRouter(URL url) {
+ this.url = url;
+ this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
+ }
+
+ public TagRouter() {
+ this.url = ROUTER_URL;
+ this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
+ }
+
+ @Override
+ public URL getUrl() {
+ return url;
+ }
+
+ @Override
+ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
+ // filter
+ List<Invoker<T>> result = new ArrayList<>();
+ try {
+ // Dynamic param
+ String tag = RpcContext.getContext().getAttachment(Constants.REQUEST_TAG_KEY);
+ // Tag request
+ if (!StringUtils.isEmpty(tag)) {
+ // Select tag invokers first
+ for (Invoker<T> invoker : invokers) {
Review comment:
@lexburner @kimmking
i think tree for statement can be combined one too, my impl:
@Override
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
// 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(REQUEST_TAG_KEY);
for (Invoker<T> invoker : invokers) {
if (StringUtils.isEmpty(invoker.getUrl().getParameter(TAG_KEY))) {
// all invokers that don't have "tag" parameter in url is a normal invoker
normalResult.add(invoker);
} else {
if (invoker.getUrl().getParameter(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