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

Nacos supports fuzzy config listen. #11856

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
17 changes: 17 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public class Constants {

public static final Integer CLUSTER_GRPC_PORT_DEFAULT_OFFSET = 1001;

public static final String NAMESPACE_ID_SPLITTER = ">>";

public static final String DATA_ID_SPLITTER = "@@";

/**
* second.
*/
Expand Down Expand Up @@ -200,6 +204,8 @@ public class Constants {

public static final String ALL_PATTERN = "*";

public static final String FUZZY_LISTEN_PATTERN_WILDCARD = "*";

public static final String COLON = ":";

public static final String LINE_BREAK = "\n";
Expand All @@ -223,6 +229,17 @@ public class Constants {

public static final int DEFAULT_REDO_THREAD_COUNT = 1;

public static class ConfigChangeType {

public static final String ADD_CONFIG = "ADD_CONFIG";

public static final String DELETE_CONFIG = "DELETE_CONFIG";

public static final String FINISH_LISTEN_INIT = "FINISH_LISTEN_INIT";

public static final String LISTEN_INIT = "LISTEN_INIT";
}

/**
* The constants in config directory.
*/
Expand Down
56 changes: 51 additions & 5 deletions api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.api.config;

import com.alibaba.nacos.api.config.filter.IConfigFilter;
import com.alibaba.nacos.api.config.listener.AbstractFuzzyListenListener;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

Expand Down Expand Up @@ -59,8 +60,8 @@ String getConfigAndSignListener(String dataId, String group, long timeoutMs, Lis
/**
* Add a listener to the configuration, after the server modified the configuration, the client will use the
* incoming listener callback. Recommended asynchronous processing, the application can implement the getExecutor
* method in the ManagerListener, provide a thread pool of execution. If not provided, use the main thread callback, May
* block other configurations or be blocked by other configurations.
* method in the ManagerListener, provide a thread pool of execution. If not provided, use the main thread callback,
* May block other configurations or be blocked by other configurations.
*
* @param dataId dataId
* @param group group
Expand All @@ -69,6 +70,51 @@ String getConfigAndSignListener(String dataId, String group, long timeoutMs, Lis
*/
void addListener(String dataId, String group, Listener listener) throws NacosException;

/**
* Add a fuzzy listener to the configuration. After the server modifies the configuration matching the specified
* fixed group name, the client will utilize the incoming fuzzy listener callback. Fuzzy listeners allow for
* pattern-based subscription to configurations, where the fixed group name represents the group and dataId patterns
* specified for subscription.
*
* @param fixedGroupName The fixed group name representing the group and dataId patterns to subscribe to.
* @param listener The fuzzy listener to be added.
* @throws NacosException NacosException
*/
void addFuzzyListener(String fixedGroupName, AbstractFuzzyListenListener listener) throws NacosException;

/**
* Add a fuzzy listener to the configuration. After the server modifies the configuration matching the specified
* dataId pattern and fixed group name, the client will utilize the incoming fuzzy listener callback. Fuzzy
* listeners allow for pattern-based subscription to configurations.
*
* @param dataIdPattern The pattern to match dataIds for subscription.
* @param fixedGroupName The fixed group name representing the group and dataId patterns to subscribe to.
* @param listener The fuzzy listener to be added.
* @throws NacosException NacosException
*/
void addFuzzyListener(String dataIdPattern, String fixedGroupName, AbstractFuzzyListenListener listener)
throws NacosException;

/**
* Cancel fuzzy listen and remove the event listener for a specified fixed group name.
*
* @param fixedGroupName The fixed group name for fuzzy watch.
* @param listener The event listener to be removed.
* @throws NacosException If an error occurs during the cancellation process.
*/
void cancelFuzzyListen(String fixedGroupName, AbstractFuzzyListenListener listener) throws NacosException;

/**
* Cancel fuzzy listen and remove the event listener for a specified service name pattern and fixed group name.
*
* @param dataIdPatter The pattern to match dataId for fuzzy watch.
* @param fixedGroupName The fixed group name for fuzzy watch.
* @param listener The event listener to be removed.
* @throws NacosException If an error occurs during the cancellation process.
*/
void cancelFuzzyListen(String dataIdPatter, String fixedGroupName, AbstractFuzzyListenListener listener)
throws NacosException;

/**
* Publish config.
*
Expand Down Expand Up @@ -144,10 +190,10 @@ boolean publishConfigCas(String dataId, String group, String content, String cas
* @return whether health
*/
String getServerStatus();

/**
* add config filter.
* It is recommended to use {@link com.alibaba.nacos.api.config.filter.AbstractConfigFilter} to expand the filter.
* add config filter. It is recommended to use {@link com.alibaba.nacos.api.config.filter.AbstractConfigFilter} to
* expand the filter.
*
* @param configFilter filter
* @since 2.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.config.listener;

import java.util.Objects;

/**
* AbstractFuzzyListenListener is an abstract class that provides basic functionality for listening to fuzzy
* configuration changes in Nacos.
*
* @author stone-98
* @date 2024/3/4
*/
public abstract class AbstractFuzzyListenListener extends AbstractListener {

/**
* Unique identifier for the listener.
*/
private String uuid;

/**
* Get the UUID (Unique Identifier) of the listener.
*
* @return The UUID of the listener
*/
public String getUuid() {
return uuid;
}

/**
* Set the UUID (Unique Identifier) of the listener.
*
* @param uuid The UUID to be set
*/
public void setUuid(String uuid) {
this.uuid = uuid;
}

/**
* Callback method invoked when a fuzzy configuration change event occurs.
*
* @param event The fuzzy configuration change event
*/
public abstract void onEvent(FuzzyListenConfigChangeEvent event);

/**
* Receive the configuration information. This method is overridden but does nothing in this abstract class.
*
* @param configInfo The configuration information
*/
@Override
public void receiveConfigInfo(String configInfo) {
// Do nothing by default
}

/**
* Compute the hash code for this listener based on its UUID.
*
* @return The hash code value for this listener
*/
@Override
public int hashCode() {
return Objects.hashCode(uuid);
}

/**
* Compare this listener to the specified object for equality. Two listeners are considered equal if they have the
* same UUID.
*
* @param o The object to compare to
* @return true if the specified object is equal to this listener, false otherwise
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractFuzzyListenListener that = (AbstractFuzzyListenListener) o;
return Objects.equals(uuid, that.uuid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.config.listener;

/**
* Represents a fuzzy listening configuration change event.
*
* <p>This event indicates that a change has occurred in a configuration that matches a fuzzy listening pattern.
*
* @author stone-98
* @date 2024/3/12
*/
public class FuzzyListenConfigChangeEvent {

/**
* The group of the configuration that has changed.
*/
private String group;

/**
* The data ID of the configuration that has changed.
*/
private String dataId;

/**
* The type of change that has occurred (e.g., "add", "delete", "update").
*/
private String type;

/**
* Constructs an empty FuzzyListenConfigChangeEvent.
*/
public FuzzyListenConfigChangeEvent() {
}

/**
* Constructs a FuzzyListenConfigChangeEvent with the specified parameters.
*
* @param group The group of the configuration that has changed
* @param dataId The data ID of the configuration that has changed
* @param type The type of change that has occurred
*/
public FuzzyListenConfigChangeEvent(String group, String dataId, String type) {
this.group = group;
this.dataId = dataId;
this.type = type;
}

/**
* Constructs and returns a new FuzzyListenConfigChangeEvent with the specified parameters.
*
* @param group The group of the configuration that has changed
* @param dataId The data ID of the configuration that has changed
* @param type The type of change that has occurred
* @return A new FuzzyListenConfigChangeEvent instance
*/
public static FuzzyListenConfigChangeEvent build(String group, String dataId, String type) {
return new FuzzyListenConfigChangeEvent(group, dataId, type);
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getDataId() {
return dataId;
}

public void setDataId(String dataId) {
this.dataId = dataId;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

/**
* Returns a string representation of the FuzzyListenConfigChangeEvent.
*
* @return A string representation of the event
*/
@Override
public String toString() {
return "FuzzyListenConfigChangeEvent{" + "group='" + group + '\'' + ", dataId='" + dataId + '\'' + ", type='"
+ type + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.config.remote.request;

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

import static com.alibaba.nacos.api.common.Constants.Config.CONFIG_MODULE;

/**
* AbstractFuzzyListenNotifyRequest.
*
* @author stone-98
* @date 2024/3/14
*/
public abstract class AbstractFuzzyListenNotifyRequest extends ServerRequest {

private String serviceChangedType;

public AbstractFuzzyListenNotifyRequest() {
}

public AbstractFuzzyListenNotifyRequest(String serviceChangedType) {
this.serviceChangedType = serviceChangedType;
}

public String getServiceChangedType() {
return serviceChangedType;
}

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

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