Skip to content
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

Naming fuzzy watch support #12997

Merged
merged 5 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ public class Constants {

public static final int WRITE_REDIRECT_CODE = 307;

public static final String NAMESPACE_ID_SPLITER = ">>";

public static final String SERVICE_INFO_SPLITER = "@@";

public static final String MATCH_PATTERN_SPLITER = "##";

public static final int SERVICE_INFO_SPLIT_COUNT = 2;

public static final String NULL_STRING = "null";
Expand All @@ -212,7 +216,7 @@ public class Constants {

public static final String ALL_PATTERN = "*";

public static final String FUZZY_LISTEN_PATTERN_WILDCARD = "*";
public static final String FUZZY_WATCH_PATTERN_WILDCARD = "*";

public static final String COLON = ":";

Expand Down Expand Up @@ -292,6 +296,26 @@ public static class Naming {
public static final String CMDB_CONTEXT_TYPE = "CMDB";
}

/**
* The constants in fuzzy watch event type directory.
*/
public static class ServiceChangedType {

public static final String ADD_SERVICE = "ADD_SERVICE";

public static final String DELETE_SERVICE = "DELETE_SERVICE";

public static final String INSTANCE_CHANGED = "INSTANCE_CHANGED";

public static final String HEART_BEAT = "HEART_BEAT";

public static final String SERVICE_SUBSCRIBED = "SERVICE_SUBSCRIBED";

public static final String WATCH_INITIAL_MATCH = "WATCH_INITIAL_MATCH";

public static final String FINISH_WATCH_INIT = "FINISH_WATCH_INIT";
}

/**
* The constants in remote directory.
*/
Expand Down
45 changes: 45 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.api.naming;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.listener.AbstractFuzzyWatchEventListener;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
Expand Down Expand Up @@ -555,6 +556,50 @@ void subscribe(String serviceName, String groupName, NamingSelector selector, Ev
void unsubscribe(String serviceName, String groupName, List<String> clusters, EventListener listener)
throws NacosException;

/**
* 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
* @throws NacosException nacos exception
*/
void fuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* 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
* @param listener event listener
* @throws NacosException nacos exception
*/
void fuzzyWatch(String serviceNamePattern, String fixedGroupName,
AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Cancel fuzzy watch, and remove event listener of a pattern.
*
* @param fixedGroupName fixed group name for fuzzy watch
* @param listener event listener
* @throws NacosException nacos exception
*/
void cancelFuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Cancel fuzzy watch, and remove event listener of a pattern.
*
* @param serviceNamePattern service name pattern for fuzzy watch
* @param fixedGroupName fixed group name for fuzzy watch
* @param listener event listener
* @throws NacosException nacos exception
*/
void cancelFuzzyWatch(String serviceNamePattern, String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Unsubscribe event listener of service.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.listener;

import java.util.concurrent.Executor;

/**
* Abstract fuzzy watch event listener, to support handle event by user custom executor.
*
* @author tanyongquan
*/
public abstract class AbstractFuzzyWatchEventListener implements FuzzyWatchListener {

String uuid;

public Executor getExecutor() {
return null;
}

public final void setUuid(String uuid) {
this.uuid = uuid;
}

public final String getUuid() {
return uuid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.listener;

/**
* Fuzzy Watch Listener.
*
* @author tanyongquan
*/
public interface FuzzyWatchListener {

/**
* callback event.
*
* @param event event
*/
void onEvent(FuzzyWatchNotifyEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.listener;

import com.alibaba.nacos.api.naming.pojo.Service;

/**
* Fuzzy Watch Notify Event.
*
* @author tanyongquan
*/
public class FuzzyWatchNotifyEvent implements Event {

private Service service;

private String changeType;

public FuzzyWatchNotifyEvent() {
}

public FuzzyWatchNotifyEvent(Service service, String changeType) {
this.service = service;
this.changeType = changeType;
}

public Service getService() {
return service;
}

public void setService(Service service) {
this.service = service;
}

public String getChangeType() {
return changeType;
}

public void setChangeType(String changeType) {
this.changeType = changeType;
}
}
29 changes: 29 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.alibaba.nacos.api.naming.pojo;

import com.alibaba.nacos.api.naming.utils.NamingUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Service of Nacos.
Expand Down Expand Up @@ -63,6 +66,11 @@ public Service(String name) {
this.name = name;
}

public Service(String name, String groupName) {
this.name = name;
this.groupName = groupName;
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -95,6 +103,10 @@ public void setGroupName(String groupName) {
this.groupName = groupName;
}

public String getGroupedServiceName() {
return NamingUtils.getGroupedName(name, groupName);
}

public Map<String, String> getMetadata() {
return metadata;
}
Expand All @@ -112,4 +124,21 @@ public String toString() {
return "Service{" + "name='" + name + '\'' + ", protectThreshold=" + protectThreshold + ", appName='" + appName
+ '\'' + ", groupName='" + groupName + '\'' + ", metadata=" + metadata + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Service service = (Service) o;
return name.equals(service.name) && groupName.equals(service.groupName);
}

@Override
public int hashCode() {
return Objects.hash(name, groupName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class NamingRemoteConstants {

public static final String DE_REGISTER_INSTANCE = "deregisterInstance";

public static final String FUZZY_WATCH_SERVICE = "fuzzyWatchService";

public static final String CANCEL_FUZZY_WATCH_SERVICE = "cancelFuzzyWatchService";

public static final String QUERY_SERVICE = "queryService";

public static final String SUBSCRIBE_SERVICE = "subscribeService";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.remote.request;

import com.alibaba.nacos.api.remote.request.ServerRequest;

import static com.alibaba.nacos.api.common.Constants.Naming.NAMING_MODULE;

/**
* Abstract fuzzy watch notify request, including basic fuzzy watch notify information.
*
* @author tanyongquan
*/
public abstract class AbstractFuzzyWatchNotifyRequest extends ServerRequest {
private String namespace;

private String serviceChangedType;

public AbstractFuzzyWatchNotifyRequest(){
}

public AbstractFuzzyWatchNotifyRequest(String namespace, String serviceChangedType) {
this.namespace = namespace;
this.serviceChangedType = serviceChangedType;
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

public String getServiceChangedType() {
return serviceChangedType;
}

public void setServiceChangedType(String serviceChangedType) {
this.serviceChangedType = serviceChangedType;
}

@Override
public String getModule() {
return NAMING_MODULE;
}
}
Loading