Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Comment thread
beiwei30 marked this conversation as resolved.
Outdated
* Copyright (C) 2009-2018 Hangzhou FanDianEr Technology Co., Ltd. All rights reserved
*/
package org.apache.dubbo.rpc.cluster.router;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.cluster.Router;

/**
* AbstractRouter
*
* @since 2018-12-07
*/
public abstract class AbstractRouter implements Router {
protected int priority;
protected URL url;

protected AbstractRouter(int priority, URL url) {
this.priority = priority;
this.url = url;
}

protected AbstractRouter() {
}

@Override
public Integer getPriority() {
return priority;
}

@Override
public URL getUrl() {
return url;
}

@Override
public int compareTo(Router o) {
if (o == null) {
throw new IllegalArgumentException();
}
if (this.priority == o.getPriority()) {
if (o.getUrl() == null) {
return -1;
}
return url.toFullString().compareTo(o.getUrl().toFullString());
} else {
return this.priority > o.getPriority() ? 1 : -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public <T> List<Invoker<T>> route(final List<Invoker<T>> invokers,
return invokers;
}

@Override
public Integer getPriority() {
return Integer.MAX_VALUE;
}

private <T> List<Invoker<T>> getMockedInvokers(final List<Invoker<T>> invokers) {
if (!hasMockProviders(invokers)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Router;
import org.apache.dubbo.rpc.cluster.router.AbstractRouter;

import java.text.ParseException;
import java.util.ArrayList;
Expand All @@ -40,21 +40,17 @@

/**
* ConditionRouter
*
*/
public class ConditionRouter implements Router {
public class ConditionRouter extends AbstractRouter {

private static final Logger logger = LoggerFactory.getLogger(ConditionRouter.class);
private static Pattern ROUTE_PATTERN = Pattern.compile("([&!=,]*)\\s*([^&!=,\\s]+)");
private final URL url;
private final int priority;
private final boolean force;
private final Map<String, MatchPair> whenCondition;
private final Map<String, MatchPair> thenCondition;

public ConditionRouter(URL url) {
this.url = url;
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
super(url.getParameter(Constants.PRIORITY_KEY, 0), url);
this.force = url.getParameter(Constants.FORCE_KEY, false);
try {
String rule = url.getParameterAndDecoded(Constants.RULE_KEY);
Expand Down Expand Up @@ -177,20 +173,6 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
return invokers;
}

@Override
public URL getUrl() {
return url;
}

@Override
public int compareTo(Router o) {
if (o == null || o.getClass() != ConditionRouter.class) {
return 1;
}
ConditionRouter c = (ConditionRouter) o;
return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
}

boolean matchWhen(URL url, Invocation invocation) {
return whenCondition == null || whenCondition.isEmpty() || matchCondition(whenCondition, url, null, invocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Router;
import org.apache.dubbo.rpc.cluster.router.AbstractRouter;

import javax.script.Bindings;
import javax.script.Compilable;
Expand All @@ -40,26 +41,20 @@

/**
* ScriptRouter
*
*/
public class ScriptRouter implements Router {
public class ScriptRouter extends AbstractRouter {

private static final Logger logger = LoggerFactory.getLogger(ScriptRouter.class);

private static final Map<String, ScriptEngine> engines = new ConcurrentHashMap<String, ScriptEngine>();

private final ScriptEngine engine;

private final int priority;

private final String rule;

private final URL url;

public ScriptRouter(URL url) {
this.url = url;
super(url.getParameter(Constants.PRIORITY_KEY, 0), url);
String type = url.getParameter(Constants.TYPE_KEY);
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
String rule = url.getParameterAndDecoded(Constants.RULE_KEY);
if (type == null || type.length() == 0) {
type = Constants.DEFAULT_SCRIPT_TYPE_KEY;
Expand All @@ -79,11 +74,6 @@ public ScriptRouter(URL url) {
this.rule = rule;
}

@Override
public URL getUrl() {
return url;
}

@Override
@SuppressWarnings("unchecked")
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
Expand Down Expand Up @@ -116,11 +106,17 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation

@Override
public int compareTo(Router o) {
if (o == null || o.getClass() != ScriptRouter.class) {
return 1;
if (o == null) {
Comment thread
beiwei30 marked this conversation as resolved.
throw new IllegalArgumentException();
}
if (this.priority == o.getPriority()) {
if (o instanceof ScriptRouter) {
ScriptRouter c = (ScriptRouter) o;
return rule.compareTo(c.rule);
}
return 0;
} else {
return this.priority > o.getPriority() ? 1 : -1;
}
ScriptRouter c = (ScriptRouter) o;
return this.priority == c.priority ? rule.compareTo(c.rule) : (this.priority > c.priority ? 1 : -1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,26 @@
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 org.apache.dubbo.rpc.cluster.router.AbstractRouter;

import java.util.ArrayList;
import java.util.List;

/**
* TagRouter
*/
public class TagRouter implements Router {
public class TagRouter extends AbstractRouter {

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);
super(url.getParameter(Constants.PRIORITY_KEY, 0), url);
}

public TagRouter() {
this.url = ROUTER_URL;
this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
}

@Override
public URL getUrl() {
return url;
super(ROUTER_URL.getParameter(Constants.PRIORITY_KEY, 0), ROUTER_URL);
}

@Override
Expand All @@ -80,7 +70,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
}
}
}
// Normal request
// Normal request
} else {
for (Invoker<T> invoker : invokers) {
// Can't access tag invoker,only normal invoker should be selected
Expand All @@ -96,13 +86,4 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
// Downgrade to all invokers
return invokers;
}

@Override
public int compareTo(Router o) {
if (o == null || o.getClass() != TagRouter.class) {
return 1;
}
TagRouter c = (TagRouter) o;
return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
}
}