diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index 017bbb7dccd..c670d9b00fa 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -204,6 +204,8 @@ public class Constants { public static final String ALL_PATTERN = "*"; + public static final String FUZZY_WATCH_PATTERN_WILDCARD = "*"; + public static final String COLON = ":"; public static final String LINE_BREAK = "\n"; @@ -240,17 +242,6 @@ public static class Naming { public static final String CMDB_CONTEXT_TYPE = "CMDB"; } - /** - * The constants in fuzzy watch pattern match rule directory. - */ - public static class FuzzyWatchMatchRule { - - public static final String MATCH_ALL = "MATCH_ALL"; - - public static final String MATCH_PREFIX = "MATCH_PREFIX"; - - } - /** * The constants in fuzzy watch event type directory. */ diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java index fba95bb6e16..75d7d9264f3 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java @@ -534,7 +534,9 @@ void unsubscribe(String serviceName, String groupName, List clusters, Ev throws NacosException; /** - * Watch a range of services by rule to receive notify events of matched services alteration. + * According to matching rules, watch services within a specific scope, and receive notifications when + * changes occur in the services within the scope. + * When given a fixed group name, watch changes in all services under this group. * * @param fixedGroupName fixed group name for fuzzy watch * @param listener event listener @@ -543,7 +545,10 @@ void unsubscribe(String serviceName, String groupName, List clusters, Ev void fuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException; /** - * Watch a range of services by rule to receive notify events of matched services alteration. + * According to matching rules, watch services within a specific scope, and receive notifications when + * changes occur in the services within the scope. + * When provided with a fixed group name and pattern of service name, watch changes in services under + * this group that match the specified pattern. * * @param serviceNamePattern service name pattern for fuzzy watch * @param fixedGroupName fixed group name for fuzzy watch diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java index b29b9639918..2207e97d3bc 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java @@ -30,6 +30,7 @@ import java.util.regex.Pattern; import static com.alibaba.nacos.api.common.Constants.CLUSTER_NAME_PATTERN_STRING; +import static com.alibaba.nacos.api.common.Constants.FUZZY_WATCH_PATTERN_WILDCARD; import static com.alibaba.nacos.api.common.Constants.NUMBER_PATTERN_STRING; /** @@ -209,68 +210,20 @@ public static String getPatternRemovedNamespace(String completedPattern) { /** * Get the pattern watched under given namespace id. + * * @param namespaceId name space id - * @param completedPattern a set of all watch pattern(with namespace id) + * @param completedPatterns a set of all watched pattern(with namespace id) * @return filtered pattern set */ - public static Set filterPatternWithNamespace(String namespaceId, Set completedPattern) { - Set patterns = new HashSet<>(); - for (String each : completedPattern) { - String eachId = getNamespaceFromPattern(each); - if (namespaceId.equals(eachId)) { - patterns.add(getPatternRemovedNamespace(each)); + public static Set filterPatternWithNamespace(String namespaceId, Set completedPatterns) { + Set patternsOfGivenNamespace = new HashSet<>(); + for (String each : completedPatterns) { + String nameSpaceOfPattern = getNamespaceFromPattern(each); + if (namespaceId.equals(nameSpaceOfPattern)) { + patternsOfGivenNamespace.add(getPatternRemovedNamespace(each)); } } - return patterns; - } - - /** - * Returns a combined string with matchPattern and matchType. - * @param matchPattern a match pattern. Such as a 'serviceNamePrefix' - * @param matchType The match type want to use - * @return grouped pattern like 'matchPattern##matchType' - */ - public static String getGroupedPattern(final String matchPattern, final String matchType) { - if (StringUtils.isBlank(matchPattern) && !matchType.equals(Constants.FuzzyWatchMatchRule.MATCH_ALL)) { - throw new IllegalArgumentException("Param 'matchPattern' is illegal, matchPattern is blank"); - } - if (StringUtils.isBlank(matchType)) { - throw new IllegalArgumentException("Param 'matchType' is illegal, matchType is blank"); - } else if (matchType.equals(Constants.FuzzyWatchMatchRule.MATCH_ALL)) { - return Constants.FuzzyWatchMatchRule.MATCH_ALL; - } - final String resultGroupedName = matchPattern + Constants.MATCH_PATTERN_SPLITER + matchType; - return resultGroupedName.intern(); - } - - /** - * Given a Pattern, return the string to be used for the match. - * @param groupedPattern a grouped pattern, format: [match string ## match type] - * @return the string to be used for the match. - */ - public static String getMatchName(String groupedPattern) { - if (StringUtils.isBlank(groupedPattern)) { - return StringUtils.EMPTY; - } - if (!groupedPattern.contains(Constants.MATCH_PATTERN_SPLITER)) { - return groupedPattern; - } - return groupedPattern.split(Constants.MATCH_PATTERN_SPLITER)[0]; - } - - /** - * Given a Pattern, return the matching rule type. - * @param groupedPattern a grouped pattern (match string ## match type) - * @return the matching rule type. - */ - public static String getMatchRule(String groupedPattern) { - if (StringUtils.isBlank(groupedPattern)) { - return StringUtils.EMPTY; - } - if (!groupedPattern.contains(Constants.MATCH_PATTERN_SPLITER)) { - return Constants.FuzzyWatchMatchRule.MATCH_ALL; - } - return groupedPattern.split(Constants.MATCH_PATTERN_SPLITER)[1]; + return patternsOfGivenNamespace; } /** @@ -318,6 +271,7 @@ public static Set getPatternMatchedServices(Collection servicesL /** * Given a service name and a pattern to match, determine whether it can match. + * TODOļ¼šIf want to add a matching method, can implement in here. * * @param serviceName service name to judge * @param groupName group name to judge @@ -326,14 +280,13 @@ public static Set getPatternMatchedServices(Collection servicesL * @return matching result */ public static boolean isMatchPattern(String serviceName, String groupName, String serviceNamePattern, String groupNamePattern) { - String serviceMatchName = getMatchName(serviceNamePattern); - String serviceMatchType = getMatchRule(serviceNamePattern); - // Only support prefix match or all match right now + // Only support prefix match or all match service name right now // Only support fixed group name right now - if (serviceMatchType.equals(Constants.FuzzyWatchMatchRule.MATCH_ALL)) { + if (serviceNamePattern.equals(FUZZY_WATCH_PATTERN_WILDCARD)) { return groupName.equals(groupNamePattern); - } else if (serviceMatchType.equals(Constants.FuzzyWatchMatchRule.MATCH_PREFIX)) { - return prefixMatchWithFixedGroupName(serviceName, serviceMatchName, groupName, getMatchName(groupNamePattern)); + } else if (serviceNamePattern.endsWith(FUZZY_WATCH_PATTERN_WILDCARD)) { + String serviceMatchName = serviceNamePattern.substring(0, serviceNamePattern.length() - 1); + return prefixMatchWithFixedGroupName(serviceName, serviceMatchName, groupName, groupNamePattern); } return false; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index eb7e90beb42..d7afbd0c99b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -50,6 +50,8 @@ import java.util.Properties; import java.util.UUID; +import static com.alibaba.nacos.api.common.Constants.FUZZY_WATCH_PATTERN_WILDCARD; + /** * Nacos Naming Service. * @@ -446,17 +448,22 @@ public void unsubscribe(String serviceName, String groupName, List clust @Override public void fuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException { - // pattern e.g. DEFAULT_GROUP@@MATCH_ALL - doFuzzyWatch(Constants.FuzzyWatchMatchRule.MATCH_ALL, fixedGroupName, listener); + doFuzzyWatch(FUZZY_WATCH_PATTERN_WILDCARD, fixedGroupName, listener); } @Override public void fuzzyWatch(String serviceNamePattern, String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException { // only support prefix match right now - // pattern e.g. DEFAULT_GROUP@@nacos.test##MATCH_PREFIX - String serviceNamePrefixPattern = NamingUtils.getGroupedPattern(serviceNamePattern, Constants.FuzzyWatchMatchRule.MATCH_PREFIX); - doFuzzyWatch(serviceNamePrefixPattern, fixedGroupName, listener); + if (!serviceNamePattern.endsWith(FUZZY_WATCH_PATTERN_WILDCARD)) { + if (serviceNamePattern.startsWith(FUZZY_WATCH_PATTERN_WILDCARD)) { + throw new UnsupportedOperationException("Suffix matching for service names is not supported yet." + + " It will be supported in future updates if needed."); + } else { + throw new UnsupportedOperationException("Illegal service name pattern, please read the documentation and pass a valid pattern."); + } + } + doFuzzyWatch(serviceNamePattern, fixedGroupName, listener); } private void doFuzzyWatch(String serviceNamePattern, String groupNamePattern, @@ -472,13 +479,12 @@ private void doFuzzyWatch(String serviceNamePattern, String groupNamePattern, @Override public void cancelFuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException { - doCancelFuzzyWatch(Constants.FuzzyWatchMatchRule.MATCH_ALL, fixedGroupName, listener); + doCancelFuzzyWatch(FUZZY_WATCH_PATTERN_WILDCARD, fixedGroupName, listener); } @Override public void cancelFuzzyWatch(String serviceNamePattern, String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException { - String serviceNamePrefixPattern = NamingUtils.getGroupedPattern(serviceNamePattern, Constants.FuzzyWatchMatchRule.MATCH_PREFIX); - doCancelFuzzyWatch(serviceNamePrefixPattern, fixedGroupName, listener); + doCancelFuzzyWatch(serviceNamePattern, fixedGroupName, listener); } private void doCancelFuzzyWatch(String serviceNamePattern, String groupNamePattern, diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java index e29be80f042..588cc3e08db 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java @@ -456,4 +456,8 @@ private void shutDownAndRemove(String uuid) { public boolean isEnable() { return rpcClient.isRunning(); } + + public String getNamespaceId() { + return namespaceId; + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/RedoScheduledTask.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/RedoScheduledTask.java index f9166cc4529..64908b1a0eb 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/RedoScheduledTask.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/RedoScheduledTask.java @@ -146,8 +146,8 @@ private void redoForFuzzyWatchers() { try { redoForFuzzyWatcher(each); } catch (NacosException e) { - LogUtils.NAMING_LOGGER.error("Redo fuzzy watcher operation {} for pattern {}@@{} failed. ", each.getRedoType(), - each.getGroupName(), each.getServiceName(), e); + LogUtils.NAMING_LOGGER.error("Redo fuzzy watcher operation {} for pattern {}@@{} in namespace {} failed. ", + each.getRedoType(), each.getGroupName(), each.getServiceName(), clientProxy.getNamespaceId(), e); } } } @@ -156,7 +156,8 @@ private void redoForFuzzyWatcher(FuzzyWatcherRedoData redoData) throws NacosExce RedoData.RedoType redoType = redoData.getRedoType(); String serviceNamePattern = redoData.getServiceName(); String groupNamePattern = redoData.getGroupName(); - LogUtils.NAMING_LOGGER.info("Redo fuzzy watcher operation {} for pattern {}@@{}", redoType, groupNamePattern, serviceNamePattern); + LogUtils.NAMING_LOGGER.info("Redo fuzzy watcher operation {} for pattern {}@@{} in namespace {}", redoType, + groupNamePattern, serviceNamePattern, clientProxy.getNamespaceId()); switch (redoType) { case REGISTER: if (isClientDisabled()) { diff --git a/example/src/main/java/com/alibaba/nacos/example/FuzzyWatchExample.java b/example/src/main/java/com/alibaba/nacos/example/FuzzyWatchExample.java index b4a8e5b207d..20f0829eb94 100644 --- a/example/src/main/java/com/alibaba/nacos/example/FuzzyWatchExample.java +++ b/example/src/main/java/com/alibaba/nacos/example/FuzzyWatchExample.java @@ -78,7 +78,7 @@ public void onEvent(FuzzyWatchNotifyEvent event) { } }); - naming.fuzzyWatch("nacos.test", DEFAULT_GROUP, new AbstractFuzzyWatchEventListener() { + naming.fuzzyWatch("nacos.test.*", DEFAULT_GROUP, new AbstractFuzzyWatchEventListener() { @Override public Executor getExecutor() {