-
Notifications
You must be signed in to change notification settings - Fork 26.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dubbo- support tag router feature] Add a new Router implement -- TagRouter #2228
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
04ce01d
tagRouter feature
lexburner c3c9d48
update dubbo.xsd
lexburner 307dcd2
remove reference router param
lexburner fc4ef14
add Unit Test
lexburner 4f73d77
rollback pom.xml for merge
lexburner 5d6677c
rollback pom.xml for merge
lexburner b3cc038
fix checkstyle
lexburner c887624
fix checkstyle
lexburner be39988
fix unit test
lexburner b70c32c
format import style
lexburner bf3915c
resolve conflict from master
lexburner b93fbfc
Merge remote-tracking branch 'upstream/master' into tag_route_feature
lexburner 8a20925
add license&remove author info
lexburner 376c0ca
trigger again
lexburner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.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; | ||
|
||
/** | ||
* TagRouter | ||
*/ | ||
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) { | ||
if (tag.equals(invoker.getUrl().getParameter(Constants.TAG_KEY))) { | ||
result.add(invoker); | ||
} | ||
} | ||
// If no invoker be selected, downgrade to normal invokers | ||
if (result.isEmpty()) { | ||
for (Invoker<T> invoker : invokers) { | ||
if (StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))) { | ||
result.add(invoker); | ||
} | ||
} | ||
} | ||
// Normal request | ||
} else { | ||
for (Invoker<T> invoker : invokers) { | ||
// Can't access tag invoker,only normal invoker should be selected | ||
if (StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))) { | ||
result.add(invoker); | ||
} | ||
} | ||
} | ||
return result; | ||
} catch (Exception e) { | ||
logger.error("Route by tag error,return all invokers.", e); | ||
} | ||
// 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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.router.tag; | ||
|
||
|
||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.rpc.cluster.Router; | ||
import org.apache.dubbo.rpc.cluster.RouterFactory; | ||
|
||
public class TagRouterFactory implements RouterFactory { | ||
|
||
public static final String NAME = "tag"; | ||
|
||
@Override | ||
public Router getRouter(URL url) { | ||
return new TagRouter(url); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...ter/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.RouterFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
file=org.apache.dubbo.rpc.cluster.router.file.FileRouterFactory | ||
script=org.apache.dubbo.rpc.cluster.router.script.ScriptRouterFactory | ||
condition=org.apache.dubbo.rpc.cluster.router.condition.ConditionRouterFactory | ||
condition=org.apache.dubbo.rpc.cluster.router.condition.ConditionRouterFactory | ||
tag=org.apache.dubbo.rpc.cluster.router.tag.TagRouterFactory |
169 changes: 169 additions & 0 deletions
169
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/tag/TagRouterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* 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.router.tag; | ||
|
||
import org.apache.dubbo.common.Constants; | ||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.common.extension.ExtensionLoader; | ||
import org.apache.dubbo.common.utils.NetUtils; | ||
import org.apache.dubbo.rpc.Invoker; | ||
import org.apache.dubbo.rpc.RpcContext; | ||
import org.apache.dubbo.rpc.RpcInvocation; | ||
import org.apache.dubbo.rpc.cluster.Router; | ||
import org.apache.dubbo.rpc.cluster.RouterFactory; | ||
import org.apache.dubbo.rpc.cluster.router.MockInvoker; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TagRouterTest { | ||
|
||
private URL tagUrl = new URL("tag" | ||
, Constants.ANYHOST_VALUE, 0 | ||
, Constants.ANY_VALUE) | ||
.addParameters( | ||
Constants.RUNTIME_KEY, "true" | ||
); | ||
|
||
@BeforeClass | ||
public static void setUpBeforeClass() throws Exception { | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testRoute_matchTag() { | ||
|
||
RpcContext.getContext().setAttachment(Constants.REQUEST_TAG_KEY, "red"); | ||
|
||
List<Invoker<String>> invokers = new ArrayList<>(); | ||
Invoker<String> redInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.1:20880/com.foo.BarService?tag=red")); | ||
Invoker<String> yellowInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.2:20880/com.foo.BarService?tag=yellow")); | ||
Invoker<String> blueInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.3:20880/com.foo.BarService?tag=blue")); | ||
Invoker<String> defaultInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.4:20880/com.foo.BarService")); | ||
|
||
invokers.add(redInvoker); | ||
invokers.add(yellowInvoker); | ||
invokers.add(blueInvoker); | ||
invokers.add(defaultInvoker); | ||
|
||
Router tagRouter = new TagRouterFactory().getRouter(tagUrl); | ||
List<Invoker<String>> filteredInvokers = tagRouter.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); | ||
Assert.assertTrue(filteredInvokers.contains(redInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(yellowInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(blueInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(defaultInvoker)); | ||
} | ||
|
||
@Test | ||
public void testRoute_matchDefault() { | ||
|
||
RpcContext.getContext().setAttachment(Constants.REQUEST_TAG_KEY, ""); | ||
|
||
List<Invoker<String>> invokers = new ArrayList<>(); | ||
Invoker<String> redInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.1:20880/com.foo.BarService?tag=red")); | ||
Invoker<String> yellowInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.2:20880/com.foo.BarService?tag=yellow")); | ||
Invoker<String> blueInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.3:20880/com.foo.BarService?tag=blue")); | ||
Invoker<String> defaultInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.4:20880/com.foo.BarService")); | ||
|
||
invokers.add(redInvoker); | ||
invokers.add(yellowInvoker); | ||
invokers.add(blueInvoker); | ||
invokers.add(defaultInvoker); | ||
|
||
Router tagRouter = new TagRouterFactory().getRouter(tagUrl); | ||
List<Invoker<String>> filteredInvokers = tagRouter.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); | ||
Assert.assertTrue(filteredInvokers.contains(defaultInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(yellowInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(blueInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(redInvoker)); | ||
} | ||
|
||
@Test | ||
public void testRoute_requestWithTag_shouldDowngrade() { | ||
|
||
RpcContext.getContext().setAttachment(Constants.REQUEST_TAG_KEY, "black"); | ||
|
||
List<Invoker<String>> invokers = new ArrayList<>(); | ||
Invoker<String> redInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.1:20880/com.foo.BarService?tag=red")); | ||
Invoker<String> yellowInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.2:20880/com.foo.BarService?tag=yellow")); | ||
Invoker<String> blueInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.3:20880/com.foo.BarService?tag=blue")); | ||
Invoker<String> defaultInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.4:20880/com.foo.BarService")); | ||
|
||
invokers.add(redInvoker); | ||
invokers.add(yellowInvoker); | ||
invokers.add(blueInvoker); | ||
invokers.add(defaultInvoker); | ||
|
||
Router tagRouter = new TagRouterFactory().getRouter(tagUrl); | ||
List<Invoker<String>> filteredInvokers = tagRouter.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); | ||
Assert.assertTrue(filteredInvokers.contains(defaultInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(yellowInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(blueInvoker)); | ||
Assert.assertFalse(filteredInvokers.contains(redInvoker)); | ||
} | ||
|
||
@Test | ||
public void testRoute_requestWithoutTag_shouldNotDowngrade() { | ||
|
||
RpcContext.getContext().setAttachment(Constants.REQUEST_TAG_KEY, ""); | ||
|
||
List<Invoker<String>> invokers = new ArrayList<>(); | ||
Invoker<String> redInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.1:20880/com.foo.BarService?tag=red")); | ||
Invoker<String> yellowInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.2:20880/com.foo.BarService?tag=yellow")); | ||
Invoker<String> blueInvoker = new MockInvoker<>(URL.valueOf( | ||
"dubbo://10.20.3.3:20880/com.foo.BarService?tag=blue")); | ||
|
||
invokers.add(redInvoker); | ||
invokers.add(yellowInvoker); | ||
invokers.add(blueInvoker); | ||
|
||
Router tagRouter = new TagRouterFactory().getRouter(tagUrl); | ||
List<Invoker<String>> filteredInvokers = tagRouter.route(invokers, URL.valueOf("consumer://" + NetUtils.getLocalHost() + "/com.foo.BarService"), new RpcInvocation()); | ||
Assert.assertEquals(0, filteredInvokers.size()); | ||
} | ||
|
||
@Test | ||
public void testRoute_createBySpi() { | ||
URL zkProvider = URL.valueOf("zookeeper://10.20.3.1:20880/com.foo.BarService?router=tag"); | ||
String parameter = zkProvider.getParameter(Constants.ROUTER_KEY); | ||
RouterFactory routerFactory = ExtensionLoader.getExtensionLoader(RouterFactory.class).getExtension(parameter); | ||
Router tagRouter = routerFactory.getRouter(zkProvider); | ||
Assert.assertTrue(tagRouter instanceof TagRouter); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -522,5 +522,4 @@ public String getScope() { | |
public void setScope(String scope) { | ||
this.scope = scope; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,5 +197,4 @@ public void setGroup(String group) { | |
checkKey("group", group); | ||
this.group = group; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
three for statement can be combined one and check conditions in it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three for statements have their own logic, depend on different conditions, can you give me more details, thanks a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lexburner @kimmking i think three for statement can be combined too,my impl: