diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java index 827518bda8e..154a161a214 100644 --- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java +++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.rpc.cluster.router.tag.model; +import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.rpc.cluster.router.AbstractRouterRule; import java.util.ArrayList; @@ -50,7 +51,7 @@ public void init() { return; } - tags.forEach(tag -> { + tags.stream().filter(tag -> CollectionUtils.isNotEmpty(tag.getAddresses())).forEach(tag -> { tagnameToAddresses.put(tag.getName(), tag.getAddresses()); tag.getAddresses().forEach(addr -> { List tagNames = addressToTagnames.computeIfAbsent(addr, k -> new ArrayList<>()); @@ -60,7 +61,10 @@ public void init() { } public List getAddresses() { - return tags.stream().flatMap(tag -> tag.getAddresses().stream()).collect(Collectors.toList()); + return tags.stream() + .filter(tag -> CollectionUtils.isNotEmpty(tag.getAddresses())) + .flatMap(tag -> tag.getAddresses().stream()) + .collect(Collectors.toList()); } public List getTagNames() { diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java index 98cdaf4d4df..829208da27f 100644 --- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java +++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java @@ -19,6 +19,8 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule; +import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -63,4 +65,47 @@ public void normalTagRuleTest() { private void setData(String path, String data) throws Exception { client.setData().forPath(path, data.getBytes()); } + + /** + * TagRouterRule parse test when the tags addresses is null + * + *
+     *     ~ -> null
+     *     null -> null
+     * 
+ */ + @Test + public void tagRouterRuleParseTest(){ + String tagRouterRuleConfig = "---\n" + + "force: false\n" + + "runtime: true\n" + + "enabled: false\n" + + "priority: 1\n" + + "key: demo-provider\n" + + "tags:\n" + + " - name: tag1\n" + + " addresses: null\n" + + " - name: tag2\n" + + " addresses: [\"30.5.120.37:20880\"]\n" + + " - name: tag3\n" + + " addresses: []\n" + + " - name: tag4\n" + + " addresses: ~\n" + + "..."; + + TagRouterRule tagRouterRule = TagRuleParser.parse(tagRouterRuleConfig); + + // assert tags + assert tagRouterRule.getTagNames().contains("tag1"); + assert tagRouterRule.getTagNames().contains("tag2"); + assert tagRouterRule.getTagNames().contains("tag3"); + assert tagRouterRule.getTagNames().contains("tag4"); + // assert addresses + assert tagRouterRule.getAddresses().contains("30.5.120.37:20880"); + assert tagRouterRule.getTagnameToAddresses().get("tag1")==null; + assert tagRouterRule.getTagnameToAddresses().get("tag2").size()==1; + assert tagRouterRule.getTagnameToAddresses().get("tag3")==null; + assert tagRouterRule.getTagnameToAddresses().get("tag4")==null; + assert tagRouterRule.getAddresses().size()==1; + } }