Skip to content

Commit 3863623

Browse files
Leishunyukhanimteyaz
authored andcommitted
create AbstractRouter (apache#2909)
* create AbstractRouter * router default method * router default method * router default method * mockinvoker
1 parent ee81c37 commit 3863623

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Router.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,31 @@ public interface Router extends Comparable<Router> {
5151
*/
5252
<T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
5353

54+
/**
55+
* priority
56+
*
57+
* @return
58+
*/
59+
int getPriority();
60+
61+
/**
62+
* compare Router
63+
*
64+
* @param o
65+
* @return
66+
*/
67+
@Override
68+
default int compareTo(Router o) {
69+
if (o == null) {
70+
throw new IllegalArgumentException();
71+
}
72+
if (this.getPriority() == o.getPriority()) {
73+
if (o.getUrl() == null) {
74+
return -1;
75+
}
76+
return getUrl().toFullString().compareTo(o.getUrl().toFullString());
77+
} else {
78+
return getPriority() > o.getPriority() ? 1 : -1;
79+
}
80+
}
5481
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/MockInvokersSelector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public <T> List<Invoker<T>> route(final List<Invoker<T>> invokers,
4949
return invokers;
5050
}
5151

52+
@Override
53+
public int getPriority() {
54+
return Integer.MAX_VALUE;
55+
}
56+
5257
private <T> List<Invoker<T>> getMockedInvokers(final List<Invoker<T>> invokers) {
5358
if (!hasMockProviders(invokers)) {
5459
return null;

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/condition/ConditionRouter.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
/**
4242
* ConditionRouter
43-
*
4443
*/
4544
public class ConditionRouter implements Router {
4645

@@ -178,17 +177,13 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
178177
}
179178

180179
@Override
181-
public URL getUrl() {
182-
return url;
180+
public int getPriority() {
181+
return priority;
183182
}
184183

185184
@Override
186-
public int compareTo(Router o) {
187-
if (o == null || o.getClass() != ConditionRouter.class) {
188-
return 1;
189-
}
190-
ConditionRouter c = (ConditionRouter) o;
191-
return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
185+
public URL getUrl() {
186+
return url;
192187
}
193188

194189
boolean matchWhen(URL url, Invocation invocation) {

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
/**
4242
* ScriptRouter
43-
*
4443
*/
4544
public class ScriptRouter implements Router {
4645

@@ -114,13 +113,24 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
114113
}
115114
}
116115

116+
@Override
117+
public int getPriority() {
118+
return priority;
119+
}
120+
117121
@Override
118122
public int compareTo(Router o) {
119-
if (o == null || o.getClass() != ScriptRouter.class) {
120-
return 1;
123+
if (o == null) {
124+
throw new IllegalArgumentException();
125+
}
126+
if (this.priority == o.getPriority()) {
127+
if (o instanceof ScriptRouter) {
128+
ScriptRouter c = (ScriptRouter) o;
129+
return rule.compareTo(c.rule);
130+
}
131+
return 0;
132+
} else {
133+
return this.priority > o.getPriority() ? 1 : -1;
121134
}
122-
ScriptRouter c = (ScriptRouter) o;
123-
return this.priority == c.priority ? rule.compareTo(c.rule) : (this.priority > c.priority ? 1 : -1);
124135
}
125-
126136
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
8080
}
8181
}
8282
}
83-
// Normal request
83+
// Normal request
8484
} else {
8585
for (Invoker<T> invoker : invokers) {
8686
// Can't access tag invoker,only normal invoker should be selected
@@ -98,11 +98,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
9898
}
9999

100100
@Override
101-
public int compareTo(Router o) {
102-
if (o == null || o.getClass() != TagRouter.class) {
103-
return 1;
104-
}
105-
TagRouter c = (TagRouter) o;
106-
return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
101+
public int getPriority() {
102+
return priority;
107103
}
108104
}

0 commit comments

Comments
 (0)