diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java index 5addb8e4911..70fd8faa3a6 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java @@ -85,4 +85,22 @@ default void notify(List> invokers) { * @return router's priority */ int getPriority(); + + @Override + default int compareTo(Router o) { + if (o == null) { + throw new IllegalArgumentException(); + } + if (this.getPriority() == o.getPriority()) { + if (o.getUrl() == null) { + return 1; + } + if (getUrl() == null) { + return -1; + } + return getUrl().toFullString().compareTo(o.getUrl().toFullString()); + } else { + return getPriority() > o.getPriority() ? 1 : -1; + } + } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java index da4bd7c37eb..aa47d654519 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/AbstractRouter.java @@ -20,9 +20,6 @@ import org.apache.dubbo.configcenter.DynamicConfiguration; import org.apache.dubbo.rpc.cluster.Router; -/** - * TODO Extract more code to here if necessary - */ public abstract class AbstractRouter implements Router { protected int priority; protected boolean force = false; @@ -65,11 +62,6 @@ public void setForce(boolean force) { this.force = force; } - @Override - public int compareTo(Router o) { - return (this.getPriority() >= o.getPriority()) ? 1 : -1; - } - public int getPriority() { return priority; } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java index 896638a2c04..d6961d630bd 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/mock/MockInvokersSelector.java @@ -22,7 +22,6 @@ 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.util.ArrayList; @@ -95,15 +94,9 @@ private boolean hasMockProviders(final List> invokers) { return hasMockProvider; } - /** - * Always stay on the top of the list - * - * @param o - * @return - */ @Override - public int compareTo(Router o) { - return 1; + public int getPriority() { + return Integer.MAX_VALUE; } } diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java index 496a0dce122..7d4717d8388 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java @@ -24,6 +24,7 @@ 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 javax.script.Bindings; @@ -120,4 +121,13 @@ public boolean isRuntime() { public boolean isForce() { return url.getParameter(Constants.FORCE_KEY, false); } + + @Override + public int compareTo(Router o) { + if (o == null || o.getClass() != ScriptRouter.class) { + return 1; + } + ScriptRouter c = (ScriptRouter) o; + return this.priority == c.priority ? rule.compareTo(c.rule) : (this.priority > c.priority ? 1 : -1); + } } diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java index 2fb30d1bb35..d8822bcd86e 100644 --- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java +++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/cluster/Router.java @@ -26,7 +26,7 @@ import java.util.stream.Collectors; @Deprecated -public interface Router extends org.apache.dubbo.rpc.cluster.Router { +public interface Router extends org.apache.dubbo.rpc.cluster.Router{ @Override com.alibaba.dubbo.common.URL getUrl(); @@ -36,7 +36,7 @@ List> route(List List> route(List> invokers, URL url, Invocation invocation) throws RpcException { + return null; + } + + @Override + public int compareTo(Router o) { + return 0; + } +} diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java new file mode 100644 index 00000000000..86269062e0a --- /dev/null +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/NewRouter.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.rpc.cluster; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.RpcException; + +import java.util.List; + +/** + * + */ +public class NewRouter implements Router { + @Override + public URL getUrl() { + return null; + } + + @Override + public List> route(List> invokers, URL url, Invocation invocation) throws RpcException { + return null; + } + + @Override + public boolean isRuntime() { + return false; + } + + @Override + public boolean isForce() { + return false; + } + + @Override + public int getPriority() { + return 0; + } +} diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java new file mode 100644 index 00000000000..10c9bcc76ef --- /dev/null +++ b/dubbo-compatible/src/test/java/org/apache/dubbo/rpc/cluster/RouterTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.rpc.cluster; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * + */ +public class RouterTest { + + private static List routers = new ArrayList<>(); + + @BeforeClass + public static void setUp () { + CompatibleRouter compatibleRouter = new CompatibleRouter(); + routers.add(compatibleRouter); + CompatibleRouter2 compatibleRouter2 = new CompatibleRouter2(); + routers.add(compatibleRouter2); + NewRouter newRouter = new NewRouter(); + routers.add(newRouter); + } + + @Test + public void testCompareTo () { + try { + Collections.sort(routers); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.assertFalse(false); + } + } +}