Java SDK Usage
Nacos Java SDK, also known as Nacos Java Client, provides Java access to Nacos configuration management, service discovery, AI Registry, and distributed lock capabilities.
The Java SDK is intended for application runtime access. It provides APIs to publish, delete, read, and listen to configurations; register, deregister, query, and subscribe to service instances; access AI resources; and acquire or release distributed locks.
It does not provide broad management operations such as listing all configurations or all services in a namespace. For range-based queries, higher-priority updates, or operational management, use the Nacos Maintainer SDK or Admin API.
0. Before You Start
0.1. Choose the Right Access Method
| What You Need | Recommended Entry |
|---|---|
| Read and listen to configurations, register instances, and subscribe to services in a Java application. | Use the Java SDK described in this page. |
| Query or subscribe to AI resources such as MCP, Agent, Skill, Prompt, and AgentSpec in a Java application. | Use the AI sections in this page, and read AI Registry Overview first. |
| Access Nacos from Go, Python, or another language. | Read SDK Overview and choose the SDK for that language. |
| Understand connections, reconnects, local cache, failover, and lifecycle behavior. | Read SDK Runtime. |
| Query a small number of known configurations or services through HTTP. | Use Client API. |
| Publish configurations, query lists, manage namespaces, manage services, or perform operations tasks. | Use Admin API or Maintainer SDK. |
The Java SDK maintains connections, listeners, subscriptions, local cache, and reconnect recovery. Business applications should prefer SDK capabilities instead of manually composing internal requests or depending on Console APIs.
0.2. Usage Principles
- One SDK instance belongs to one namespace. Create separate instances when accessing multiple namespaces.
- Reuse SDK instances inside an application. Frequent client creation wastes connections and thread resources.
- Call
shutdown()when the application exits, reloads, or replaces a client instance. - A configuration listener receives configuration content through
Listener.receiveConfigInfo; the application refreshes its own state from that content. - A service subscription returns a runtime discovery view. It may be affected by health state, weight, protection threshold, cluster, selector, and other factors.
- AI resources are affected by namespace, version, label, publish state, and visibility. In production, when using MCP, Agent, Skill, Prompt, or other resources that support versions or labels, specify the expected version or label when possible, and handle missing, invisible, or changed resources.
- AI listeners return resource content or discovery snapshots according to their contracts. Replace the previous view and handle unavailable events; not every callback requires another query.
- Distributed lock is experimental. Before production use, read Distributed Lock and validate the behavior carefully.
1. Dependency Overview
1.1. Java Version
Nacos Java SDK requires JDK 1.8 or later.
1.2. Maven Coordinates
<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${version}</version></dependency>1.2.1. Pure Java SDK
Note: Since Nacos Java SDK 2.0 introduced gRPC, some dependencies are shaded into
nacos-clientto avoid conflicts with gRPC versions introduced by user applications. This makesnacos-clientlarger. If your application does not introduce gRPC by itself, or you have confirmed that there is no version conflict, you can use the pure client classifier to reduce dependency size.
<properties> <!-- The pure client is supported since 2.1.2. --> <nacos.version>3.3.0-RC</nacos.version> </properties>
<dependencies> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos.version}</version> <!-- Specify the pure SDK. --> <classifier>pure</classifier> </dependency> <!-- When using the pure client, introduce nacos-api and nacos-common of the same version. Otherwise, runtime class-not-found errors may occur. --> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-common</artifactId> <version>${nacos.version}</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-api</artifactId> <version>${nacos.version}</version> </dependency> </dependencies>1.3. Upgrade Compatibility
In 3.x the default namespace ID was changed from empty string to public (see issue #9846). The 3.0 client uses public by default, so it is incompatible with older servers when using the default namespace.
Before upgrading, ensure that the server is already 3.0 or above, or you are not using the config center in the default namespace.
When upgrading to the 3.3 Java SDK, also check the following:
- Use the resource services in section 2 for new AI integrations. Previously released flat MCP, A2A, Skill, Prompt and AgentSpec methods remain compatible and are deprecated; generic Agent operations use
aiService.agent(). - Client metrics now use Micrometer. Register a
MeterRegistryand update metric names, units and alerts as described in Client Metrics Migration. Keeping only the oldsimpleclientdependency does not automatically export metrics. - Use the same version of
nacos-client,nacos-apiandnacos-commonwith the pure classifier, and verify dependencies against the application’s runtime.
1.4. JSON Dependencies and Adapters
3.3 supports Jackson 2 and Jackson 3. The default auto mode selects an available runtime adapter, preferring Jackson 3 when both are available. Jackson 2 is a regular dependency; applications using Jackson 3 must provide its dependencies and meet that Jackson version’s Java requirements. The SDK’s Java 8 minimum does not mean Jackson 3 also runs on Java 8.
To select Jackson 2 explicitly, set this JVM startup option:
-Dnacos.client.json.adapter=jackson2Supported values are auto, jackson2 and jackson3. Initialization fails if the selected adapter is unavailable or no adapter is available. This is a JVM system property, not a per-client Properties option.
Serialization examples below use com.alibaba.nacos.api.utils.json.JsonUtils. Prefer this neutral entry point for new code. Legacy JacksonUtils signatures that expose Jackson 2 types remain compatibility APIs; changing the adapter cannot turn those signatures into Jackson 3 types.
2. Initialize the SDK
Nacos 3.3 enables client authentication by default. With the default plugin, create a user with the required resource permissions, then configure the address, username, and password through Properties. The SDK logs in, attaches the token, and refreshes it automatically. An address-only initializer does not supply the full identity for default authentication.
This and subsequent examples explicitly read NACOS_USERNAME and NACOS_PASSWORD environment variables. Set them to an existing account before running the examples. See Access Credentials for other auth plugins.
Properties properties = new Properties();properties.setProperty(PropertyKeyConst.SERVER_ADDR, "localhost:8848");properties.setProperty(PropertyKeyConst.NAMESPACE, "public");properties.setProperty(PropertyKeyConst.USERNAME, System.getenv("NACOS_USERNAME"));properties.setProperty(PropertyKeyConst.PASSWORD, System.getenv("NACOS_PASSWORD"));
ConfigService configService = NacosFactory.createConfigService(properties);NamingService namingService = NacosFactory.createNamingService(properties);LockService lockService = NacosLockFactory.createLockService(properties);AiService aiService = AiFactory.createAiService(properties);In 3.3, obtain AI services by resource:
| Entry point | Return type | Section |
|---|---|---|
aiService.mcp() | McpService | Chapter 6 |
aiService.agent() | AgentService | Legacy A2A in chapter 7; generic Agent in chapter 11 |
aiService.skill() | SkillService | Chapter 8 |
aiService.prompt() | PromptService | Chapter 9 |
aiService.agentSpec() | AgentSpecService | Chapter 10 |
These interfaces are in com.alibaba.nacos.api.ai and share the parent AiService namespace, connections and authentication. Release them through aiService.shutdown(). The following examples use resource services; existing flat compatibility calls can migrate gradually. Generic Agent search, discovery, subscriptions, endpoint registration and definition publication cannot be called directly on AiService.
For more parameters involved in initialization, see Java SDK Properties.
Note: A Nacos Java SDK instance can access only configurations and services in the same namespace. To access configurations or services in different namespaces, create different Nacos Java SDK instances.
3. Configuration Management API
3.1. Get Config
Description
Gets configuration from Nacos when a service starts.
public String getConfig(String dataId, String group, long timeoutMs) throws NacosException;
ConfigQueryResult getConfig(GetConfigRequest request) throws NacosException;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID. Use a naming rule similar to package.class, such as com.taobao.tc.refund.log.level, to ensure global uniqueness. The class part should describe the business meaning of the configuration. Use lowercase characters only. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 256 bytes. |
| group | string | Configuration group. We recommend using product:module, such as Nacos:Test, to ensure uniqueness. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 128 bytes. |
| timeout | long | Timeout for reading the configuration, in milliseconds. The recommended value is 3000. |
3.3 adds getConfig(GetConfigRequest). Request and result models are in com.alibaba.nacos.api.config; the namespace comes from SDK initialization.
| Request field | Type | Description |
|---|---|---|
| dataId | String | Required configuration ID. |
| group | String | Configuration group; an empty value uses DEFAULT_GROUP. |
| timeoutMs | long | Query timeout in milliseconds. Set it explicitly, for example to 5000. |
| localMd5 | String | Optional MD5 of content already held locally. Usually omit it and let the SDK manage conditional queries and matching content. |
Return Value
| Type | Description |
|---|---|
| string | Configuration value |
The request-object overload returns ConfigQueryResult, with content, md5, configType and encryptedDataKey. content can be null when the configuration does not exist. Use the returned md5 for CAS updates; do not compute it from decrypted content.
The SDK handles gRPC Not-Modified responses and restores content. Applications still consume the result object rather than handling HTTP 304. This does not imply that all HTTP Config APIs support conditional queries.
Request Example
try { String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); System.out.println(content);} catch (NacosException e) { e.printStackTrace();}The request-object example reuses configService from section 2:
GetConfigRequest request = GetConfigRequest.builder() .dataId("{dataId}").group("{group}").timeoutMs(5000).build();ConfigQueryResult result = configService.getConfig(request);System.out.println(result.getContent());System.out.println(result.getMd5());Exceptions
Invalid parameters, query timeouts or network failures can raise NacosException.
3.2. Listen to Config
Description
If you want Nacos to push configuration changes, use the Nacos dynamic configuration listener API.
public void addListener(String dataId, String group, Listener listener)Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID. Use a naming rule similar to package.class, such as com.taobao.tc.refund.log.level, to ensure global uniqueness. The class part should describe the business meaning of the configuration. Use lowercase characters only. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 256 bytes. |
| group | string | Configuration group. We recommend using product, such as Nacos:Test, to ensure uniqueness. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 128 bytes. |
| listener | Listener | Listener callback invoked when the configuration changes. |
Return Value
| Type | Description |
|---|---|
| string | Configuration value, returned through the callback during initialization or configuration changes. |
Request Example
String serverAddr = "{serverAddr}";String dataId = "{dataId}";String group = "{group}";Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.put("serverAddr", serverAddr);ConfigService configService = NacosFactory.createConfigService(properties);String content = configService.getConfig(dataId, group, 5000);System.out.println(content);configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { System.out.println("recieve1:" + configInfo); } @Override public Executor getExecutor() { return null; }});
// Keep the main thread alive for testing because configuration subscription uses daemon threads.// This code is not required in production.while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}3.3. Remove Listener
Description
Cancels the configuration listener. After the listener is removed, configuration changes are no longer pushed.
public void removeListener(String dataId, String group, Listener listener)Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID. Use a naming rule similar to package.class, such as com.taobao.tc.refund.log.level, to ensure global uniqueness. The class part should describe the business meaning of the configuration. Use lowercase characters only. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 256 bytes. |
| group | string | Configuration group |
| listener | ConfigChangeListenerAdapter | Listener callback invoked when the configuration changes. |
Usage Example
String serverAddr = "{serverAddr}";String dataId = "{dataId}";String group = "{group}";Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.put("serverAddr", serverAddr);ConfigService configService = NacosFactory.createConfigService(properties);configService.removeListener(dataId, group, yourListener);3.4. Publish Config
Description
Publishes Nacos configurations automatically from a program to reduce operations costs through automation.
Note: The same publish API is used to create and update configurations. If the configuration does not exist, it is created. If the configuration already exists, it is updated.
public boolean publishConfig(String dataId, String group, String content) throws NacosException;
public boolean publishConfig(String dataId, String group, String content, String type) throws NacosException;
PublishConfigResult publishConfig(PublishConfigRequest request) throws NacosException;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID. Use a naming rule similar to package.class, such as com.taobao.tc.refund.log.level, to ensure global uniqueness. We recommend defining the class part according to the business meaning of the configuration. Use lowercase characters only. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 256 bytes. |
| group | string | Configuration group. We recommend using product:module, such as Nacos:Test, to ensure uniqueness. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 128 bytes. |
| content | string | Configuration content. The maximum size is 100 KB. |
| type | string | @Since 1.4.1. Configuration type. See com.alibaba.nacos.api.config.ConfigType. The default value is TEXT. |
3.3 adds publishConfig(PublishConfigRequest). This model is in com.alibaba.nacos.api.config and contains dataId, group, content, type and optional casMd5. Setting casMd5 enables CAS; a mismatch fails the publish without overwriting existing content. The SDK supplies the namespace.
Return Parameters
| Type | Description |
|---|---|
| boolean | Whether the publish operation succeeded |
The request-object overload returns PublishConfigResult. Check isSuccess() first; on failure, inspect getErrorCode() and getErrorMessage(). On success, getMd5() is available. Remote request failures may be returned as failed results, while local validation and filters can still throw exceptions. A catch block alone is insufficient to detect publication failure.
Request Example
try { // Initialize the configuration service. The console automatically obtains the following parameters through the sample code. String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); boolean isPublishOk = configService.publishConfig(dataId, group, "content"); System.out.println(isPublishOk);} catch (NacosException e) { e.printStackTrace();}This example reuses configService from section 2, reads an existing configuration and publishes with CAS:
ConfigQueryResult current = configService.getConfig(GetConfigRequest.builder() .dataId("{dataId}").group("{group}").timeoutMs(5000).build());if (current.getContent() == null || current.getMd5() == null) { throw new IllegalStateException("Read an existing configuration before CAS publishing");}PublishConfigResult result = configService.publishConfig(PublishConfigRequest.builder() .dataId("{dataId}").group("{group}").content("new-content") .type("text").casMd5(current.getMd5()).build());if (!result.isSuccess()) { System.err.println(result.getErrorCode() + ": " + result.getErrorMessage());}Exceptions
Check the boolean or result object for a failed publication, and handle NacosException from validation, filters or other processing.
3.5. Delete Config
Description
Deletes Nacos configurations automatically from a program to reduce operations costs through automation.
Note: If the configuration exists, it is deleted. If the configuration does not exist, a success message is returned directly.
public boolean removeConfig(String dataId, String group) throws NacosException;
RemoveConfigResult removeConfig(RemoveConfigRequest request) throws NacosException;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID |
| group | string | Configuration group |
Return Parameters
| Type | Description |
|---|---|
| boolean | Whether the deletion succeeded |
3.3 adds RemoveConfigRequest in com.alibaba.nacos.api.config, with dataId and group; the SDK supplies the namespace. It returns RemoveConfigResult: check isSuccess(), then getErrorCode() and getErrorMessage() on failure. Parameter or remote invocation errors can still throw NacosException.
Request Example
try { // Initialize the configuration service. The console automatically obtains the following parameters through the sample code. String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties); boolean isRemoveOk = configService.removeConfig(dataId, group); System.out.println(isRemoveOk);} catch (NacosException e) { e.printStackTrace();}The request-object example reuses configService from section 2:
RemoveConfigResult result = configService.removeConfig(RemoveConfigRequest.builder() .dataId("{dataId}").group("{group}").build());if (!result.isSuccess()) { System.err.println(result.getErrorCode() + ": " + result.getErrorMessage());}Exceptions
Check the boolean or result object for a failed deletion, and handle NacosException from validation or remote calls.
3.6. Get Config with Listener
Description
If you want to register a listener when the program obtains the configuration for the first time so that the listener can be used for subsequent configuration updates, use this API directly.
This API is equivalent to calling
getConfigfirst and then callingaddListener.
String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener) throws NacosException;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID. Use a naming rule similar to package.class, such as com.taobao.tc.refund.log.level, to ensure global uniqueness. The class part should describe the business meaning of the configuration. Use lowercase characters only. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 256 bytes. |
| group | string | Configuration group. We recommend using product:module, such as Nacos:Test, to ensure uniqueness. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 128 bytes. |
| timeout | long | Timeout for reading the configuration, in milliseconds. The recommended value is 3000. |
| listener | Listener | Listener callback invoked when the configuration changes. |
Return Value
| Type | Description |
|---|---|
| string | Configuration value |
Request Example
try { String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfigAndSignListener(dataId, group, 5000, new Listener() { @Override public void receiveConfigInfo(String configInfo) { System.out.println("recieve1:" + configInfo); } @Override public Executor getExecutor() { return null; } }); System.out.println(content);} catch (NacosException e) { e.printStackTrace();}Exceptions
If reading the configuration times out or a network error occurs, a NacosException is thrown.
3.7. Publish Config with Compare-And-Swap (CAS)
Description
When publishConfig is used directly to publish configurations, concurrent updates from different processes may overwrite each other. In this case, use the configuration publish API with Compare-And-Swap (CAS) to prevent this situation.
Note: The same publish API is used to create and update configurations. If the configuration does not exist, it is created. If the configuration already exists, it is updated.
boolean publishConfigCas(String dataId, String group, String content, String casMd5) throws NacosException;
boolean publishConfigCas(String dataId, String group, String content, String casMd5, String type) throws NacosException;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID. Use a naming rule similar to package.class, such as com.taobao.tc.refund.log.level, to ensure global uniqueness. We recommend defining the class part according to the business meaning of the configuration. Use lowercase characters only. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 256 bytes. |
| group | string | Configuration group. We recommend using product:module, such as Nacos:Test, to ensure uniqueness. Only letters and four special characters (., :, -, _) are allowed. The maximum length is 128 bytes. |
| content | string | Configuration content. The maximum size is 100 KB. |
| casMd5 | string | MD5 of the previous configuration content |
| type | string | Configuration type. See com.alibaba.nacos.api.config.ConfigType. The default value is TEXT. |
Return Parameters
| Type | Description |
|---|---|
| boolean | Whether the publish operation succeeded |
Request Example
try { // Initialize the configuration service. The console automatically obtains the following parameters through the sample code. String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; String oldContent = "oldContent"; String oldContentMd5 = "63fb636909f1ebad67110e49117e6de4"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); // For the first publish, pass null as casMd5. boolean isPublishOk = configService.publishConfigCas(dataId, group, oldContent, null); System.out.println(isPublishOk); // The old MD5 is correct, so the update succeeds. isPublishOk = configService.publishConfigCas(dataId, group, "newContent", oldContentMd5); System.out.println(isPublishOk); // The old MD5 is incorrect, so the update fails. isPublishOk = configService.publishConfigCas(dataId, group, "newContent2", oldContentMd5); System.out.println(isPublishOk);} catch (NacosException e) { e.printStackTrace();}Exceptions
If reading the configuration times out or a network error occurs, a NacosException is thrown.
3.8. Fuzzy Watch Config
Description
The fuzzyWatch API can batch-subscribe to configurations that match the specified group rule and configuration dataId rule. Use * for prefix fuzzy matching, suffix fuzzy matching, or bilateral fuzzy matching.
Fuzzy watch pushes only configuration add and delete events. It does not directly push the content of configuration changes. In a fuzzy watch listener, you can use the addListener API together with fuzzy watch to listen for configuration content changes.
For stability, Nacos applies upper-limit protection to the number of fuzzy watch rules and the number of configurations matched by a single rule. For details, see Fuzzy Watch Capacity Protection.
/** * Subscribes to change events for all configurations in the current namespace that match the specified group rule and dataId rule. * The fuzzy watch list is asynchronously returned through the watcher callback. * @param dataIdPattern dataId matching rule * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener */void fuzzyWatch(String dataIdPattern, String groupNamePattern, FuzzyWatchEventWatcher watcher);
/** * Subscribes to change events for all configurations in the current namespace that match the specified group rule and dataId rule, * and obtains the current matched configuration list in Future mode. * The fuzzy watch list is asynchronously returned through the watcher callback. * @param dataIdPattern dataId matching rule * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener * @return Future that can be used to wait for the asynchronous configuration push result */Future<Set<String>> fuzzyWatchWithGroupKeys(String dataIdPattern, String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Cancels the subscription to change events for all configurations in the current namespace that match the specified group rule and dataId rule. * @param dataIdPattern dataId matching rule * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener to remove */void cancelFuzzyWatch(String dataIdPattern, String groupNamePattern, FuzzyWatchEventWatcher watcher);
/** * Subscribes to change events for all configurations in the current namespace that match the specified group rule. * * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener */void fuzzyWatch(String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Subscribes to change events for all configurations in the current namespace that match the specified group rule. * A Future can be used to obtain the current matched configuration list. * * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener * @return Future that can be used to wait for the asynchronous configuration push result */Future<Set<String>> fuzzyWatchWithGroupKeys(String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Cancels the subscription to change events for all configurations in the current namespace that match the specified group rule. * * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener to remove */void cancelFuzzyWatch(String groupNamePattern, FuzzyWatchEventWatcher watcher) ;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| dataIdPattern | string | Configuration ID matching rule. Supports prefix fuzzy matching, such as mydataId*; suffix fuzzy matching, such as *mydatdId; and bilateral fuzzy matching, such as *mydatadId*. |
| groupNamePattern | string | Configuration group matching rule. Supports prefix fuzzy matching, such as mygroup*; suffix fuzzy matching, such as *mygroup; and bilateral fuzzy matching, such as *mygroup*. |
| watcher | FuzzyWatchEventWatcher | Fuzzy watch listener |
FuzzyWatchEventWatcher Fuzzy Watch Listener
| Method | Parameter Type | Description |
|---|---|---|
| onEvent | ConfigFuzzyWatchChangeEvent | Fuzzy watch callback event object |
| getExecutor | void | Specifies the thread pool used to execute callback events. If it is empty, callbacks are executed in the Nacos push thread. |
ConfigFuzzyWatchChangeEvent Fuzzy Watch Event
| Parameter | Type | Description |
|---|---|---|
| dataId | string | dataId of the changed configuration |
| group | string | Group of the changed configuration |
| namespace | string | Namespace of the changed configuration |
| changedType | string | Change type received by the client, including ADD_CONFIG for added configurations and DELETE_CONFIG for removed configurations |
| syncType | string | Type that triggered the change, including FUZZY_WATCH_INIT_NOTIFY for initial push, FUZZY_WATCH_DIFF_SYNC_NOTIFY for change reconciliation, and FUZZY_WATCH_RESOURCE_CHANGED for configuration change push |
Return Parameters
| Type | Description |
|---|---|
| Future<Set | A Future object that can obtain the current matched configuration list. After the list of configurations matched by the rule is pushed to the client, you can obtain the list through the Future object. *Note: When capacity protection is triggered, the returned configuration list may be incomplete. |
// The returned parameter is a groupKey list. You can use the GroupKey utility class to obtain dataId, group, and namespace.String[] groupKeyItems = GroupKey.parseKey(String groupKey);String dataId=groupKeyItems[0];String group=groupKeyItems[1];String namespace=groupKeyItems[2];Fuzzy Watch Capacity Protection
For stability, to avoid server memory pressure and client push storms caused by too many rules or too many configurations matched by rules, Nacos provides capacity protection for fuzzy watch at two levels. When an upper limit is exceeded, fuzzy watch pushes are suppressed.
- Upper-limit protection for the number of fuzzy watch rules. The default upper limit is 20 and can be adjusted by the
nacos.config.fuzzy.watch.max.pattern.countparameter. - Upper-limit protection for the number of configurations matched by a single fuzzy watch rule. The default upper limit is 500 and can be adjusted by the
nacos.config.fuzzy.watch.max.pattern.match.config.countparameter.
When registering a fuzzy watch listener in the fuzzyWatch API, the listener can also implement FuzzyWatchLoadWatcher to perceive capacity protection events.
FuzzyWatchLoadWatcher Fuzzy Watch Load Listener
| Method | Description |
|---|---|
| onPatternOverLimit | Triggered when pushes for the current fuzzy watch rule are suppressed because the rule count exceeds the upper limit |
| onConfigReachUpLimit | Triggered when pushes for the current fuzzy watch rule are suppressed because the number of matched configurations reaches the upper limit |
*Note:
- When capacity protection is triggered, the configuration list returned by
fuzzyWatchWithGroupKeysmay be incomplete. - When configuration-count upper-limit protection is triggered, configuration deletion events may also fail to be pushed because of the protection mechanism.
Request Example
try { // Initialize the configuration service. The console automatically obtains the following parameters through the sample code. String serverAddr = "{serverAddr}"; String dataIdPattern = "testDataId*"; String groupPattern = "group*"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); properties.put("namespace", "mynamespaceId");
ConfigService configService = NacosFactory.createConfigService(properties); Future<Set<String>> fuzzyWatchFuture = configService.fuzzyWatchWithGroupKeys(dataIdPattern, groupPattern, new AbstractFuzzyWatchEventWatcher() { @Override public void onEvent(ConfigFuzzyWatchChangeEvent event) { String changedType= event.getChangedType(); String syncType= event.getSyncType(); String groupChanged = event.getGroup(); String dataIdChanged = event.getDataId(); String namespace = event.getNamespace(); // Do something. }
@Override public void onPatternOverLimit() { System.out.println("pattern count over limit"); // Do something. }
@Override public void onConfigReachUpLimit() { System.out.println("pattern match config count reach to up limit"); // Do something. } });
Set<String> matchGroupKeys = fuzzyWatchFuture.get(); System.out.println("get group keys count:"+matchGroupKeys.size());
} catch (NacosException e) { e.printStackTrace();}3.9. Get config with result object
Description
Get configuration from Nacos and receive a result object containing content and metadata (e.g. MD5) for use in CAS publish and similar operations. This API is available since 3.0.
ConfigQueryResult getConfigWithResult(String dataId, String group, long timeoutMs) throws NacosException;Request parameters
| Parameter | Type | Description |
|---|---|---|
| dataId | string | Configuration ID; use a package.class-style name; only letters and 4 special chars (”.”, ”:”, ”-”, ”_”); max 256 bytes. |
| group | string | Configuration group; recommend product; only letters and 4 special chars; max 128 bytes. |
| timeoutMs | long | Read timeout in ms; recommend 3000. |
Return value
| Type | Description |
|---|---|
| ConfigQueryResult | Contains content, md5, etc.; can be used with CAS publish config. |
Request example
try { String serverAddr = "{serverAddr}"; String dataId = "{dataId}"; String group = "{group}"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); ConfigQueryResult result = configService.getConfigWithResult(dataId, group, 5000); String content = result.getContent(); String md5 = result.getMd5(); System.out.println(content);} catch (NacosException e) { e.printStackTrace();}Exception
NacosException on read timeout or network error.
3.10. Get server status
Description
Get the current config server status.
String getServerStatus();Request parameters
None.
Return value
| Type | Description |
|---|---|
| String | Server status. |
Request example
ConfigService configService = NacosFactory.createConfigService("{serverAddr}");String status = configService.getServerStatus();System.out.println(status);3.11. Add config filter
Description
Add a config filter for request/response filtering when getting or publishing config. Prefer extending com.alibaba.nacos.api.config.filter.AbstractConfigFilter. This API is available since 2.3.0.
void addConfigFilter(IConfigFilter configFilter);Request parameters
| Parameter | Type | Description |
|---|---|---|
| configFilter | IConfigFilter | Filter instance; recommend using an AbstractConfigFilter subclass. |
Return value
None.
Request example
ConfigService configService = NacosFactory.createConfigService("{serverAddr}");configService.addConfigFilter(new AbstractConfigFilter() { @Override public void init(Properties properties) {} @Override public void doFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain chain) throws NacosException { // filter logic chain.doFilter(request, response); } @Override public int getOrder() { return 0; } @Override public String getFilterName() { return "myFilter"; }});4. Service Discovery API
Tip for learning: When you register an instance using the Service Discovery API, the instance will be removed from Nacos once the client process exits, so you won’t see it in the Nacos console. For learning or debugging, you can keep the process running after registration (e.g. with
Thread.sleep()) so you can verify in the Nacos console that the instance was registered successfully.
4.1. Register Instance
Description
Registers an instance to a service.
Because the same Nacos Client instance can register only one instance to a service, if the same Nacos Client instance registers instances to the same service multiple times, the later instance overwrites the earlier one. For proxy registration scenarios, use Batch Register Service Instances.
void registerInstance(String serviceName, String ip, int port) throws NacosException;
void registerInstance(String serviceName, String groupName, String ip, int port) throws NacosException;
void registerInstance(String serviceName, String ip, int port, String clusterName) throws NacosException;
void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException;
void registerInstance(String serviceName, Instance instance) throws NacosException;
void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| ip | string | Service instance IP | None, required |
| port | int | Service instance port | None, required |
| clusterName | string | Cluster name | DEFAULT |
| instance | See code comments | Instance properties | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));
// The following registration requests have the same result: they register an instance in group `DEFAULT_GROUP`// for service `nacos.test.service` with IP `127.0.0.1`, port `8848`, and clusterName `DEFAULT`.naming.registerInstance("nacos.test.service", "127.0.0.1", 8848);naming.registerInstance("nacos.test.service", "DEFAULT_GROUP", "127.0.0.1", 8848);naming.registerInstance("nacos.test.service", "127.0.0.1", 8848, "DEFAULT");naming.registerInstance("nacos.test.service", "DEFAULT_GROUP", "127.0.0.1", 8848, "DEFAULT");Instance instance = new Instance();instance.setIp("127.0.0.1");instance.setPort(8848);instance.setClusterName("DEFAULT");naming.registerInstance("nacos.test.service", instance);naming.registerInstance("nacos.test.service", "DEFAULT_GROUP", instance);
// Keep the process running so you can see the registered instance in the Nacos console (for learning/debugging)Thread.sleep(300000); // e.g. keep for 5 minutes4.2. Deregister Instance
Description
Deletes an instance from a service.
If the service is registered through Batch Register Service Instances, using deregister instance will deregister all batch-registered instances. To deregister only some batch-registered instances, use Batch Deregister Service Instances.
void deregisterInstance(String serviceName, String ip, int port) throws NacosException;
void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException;
void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException;
void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException;
void deregisterInstance(String serviceName, Instance instance) throws NacosException;
void deregisterInstance(String serviceName, String groupName, Instance instance);Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| ip | string | Service instance IP | None, required |
| port | int | Service instance port | None, required |
| clusterName | string | Cluster name | DEFAULT |
| instance | See code comments | Instance properties | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));// The following deregistration requests have the same result: they deregister an instance in group `DEFAULT_GROUP`// for service `nacos.test.service` with IP `127.0.0.1`, port `8848`, and clusterName `DEFAULT`.naming.deregisterInstance("nacos.test.service", "127.0.0.1", 8848);naming.deregisterInstance("nacos.test.service", "DEFAULT_GROUP", "127.0.0.1", 8848);naming.deregisterInstance("nacos.test.service", "127.0.0.1", 8848, "DEFAULT");naming.deregisterInstance("nacos.test.service", "DEFAULT_GROUP", "127.0.0.1", 8848, "DEFAULT");Instance instance = new Instance();instance.setIp("127.0.0.1");instance.setPort(8848);instance.setClusterName("DEFAULT");naming.deregisterInstance("nacos.test.service", instance);naming.deregisterInstance("nacos.test.service", "DEFAULT_GROUP", instance);4.3. Get All Instances
Description
Gets all instances under a service.
List<Instance> getAllInstances(String serviceName) throws NacosException;
List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException;
List<Instance> getAllInstances(String serviceName, boolean subscribe) throws NacosException;
List<Instance> getAllInstances(String serviceName, String groupName, boolean subscribe) throws NacosException;
List<Instance> getAllInstances(String serviceName, List<String> clusters) throws NacosException;
List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters) throws NacosException;
List<Instance> getAllInstances(String serviceName, List<String> clusters, boolean subscribe) throws NacosException;
List<Instance> getAllInstances(String serviceName, String groupName, List<String> clusters, boolean subscribe) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| subscribe | Boolean | Whether to subscribe to the service. If true, the service is subscribed to and queries prefer the in-memory cache. If false, queries are sent directly to Nacos Server. | true |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
Return Parameters
List<Instance> instance list.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));// The following query requests have the same result.System.out.println(naming.getAllInstances("nacos.test.service"));System.out.println(naming.getAllInstances("nacos.test.service", "DEFAULT_GROUP"));System.out.println(naming.getAllInstances("nacos.test.service", true));System.out.println(naming.getAllInstances("nacos.test.service", "DEFAULT_GROUP", true));System.out.println(naming.getAllInstances("nacos.test.service", new ArrayList<>()));System.out.println(naming.getAllInstances("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>()));System.out.println(naming.getAllInstances("nacos.test.service", new ArrayList<>(), true));System.out.println(naming.getAllInstances("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>(), true));4.4. Get Healthy or Unhealthy Instances
Description
Gets a filtered instance list based on conditions.
List<Instance> selectInstances(String serviceName, boolean healthy) throws NacosException;
List<Instance> selectInstances(String serviceName, String groupName, boolean healthy) throws NacosException;
List<Instance> selectInstances(String serviceName, boolean healthy, boolean subscribe) throws NacosException;
List<Instance> selectInstances(String serviceName, String groupName, boolean healthy, boolean subscribe) throws NacosException;
List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy) throws NacosException;
List<Instance> selectInstances(String serviceName, String groupName, List<String> clusters, boolean healthy) throws NacosException;
List<Instance> selectInstances(String serviceName, List<String> clusters, boolean healthy, boolean subscribe) throws NacosException;
List<Instance> selectInstances(String serviceName, String groupName, List<String> clusters, boolean healthy, boolean subscribe) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| subscribe | Boolean | Whether to subscribe to the service. If true, the service is subscribed to and queries prefer the in-memory cache. If false, queries are sent directly to Nacos Server. | true |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
| healthy | boolean | Whether the instances are healthy. If true, only healthy instances are returned. Otherwise, unhealthy instances are returned. | true |
Return Parameters
List<Instance> instance list.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));// The following query requests have the same result.System.out.println(naming.selectInstances("nacos.test.service", true));System.out.println(naming.selectInstances("nacos.test.service", "DEFAULT_GROUP", true));System.out.println(naming.selectInstances("nacos.test.service", true, true));System.out.println(naming.selectInstances("nacos.test.service", "DEFAULT_GROUP", true, true));System.out.println(naming.selectInstances("nacos.test.service", new ArrayList<>(), true));System.out.println(naming.selectInstances("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>(), true));System.out.println(naming.selectInstances("nacos.test.service", new ArrayList<>(), true, true));System.out.println(naming.selectInstances("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>(), true, true));4.5. Select One Healthy Instance
Description
Randomly selects one healthy instance based on the load-balancing algorithm.
Instance selectOneHealthyInstance(String serviceName) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, String groupName) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, boolean subscribe) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, String groupName, boolean subscribe) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, List<String> clusters) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, String groupName, List<String> clusters) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, List<String> clusters, boolean subscribe) throws NacosException;
Instance selectOneHealthyInstance(String serviceName, String groupName, List<String> clusters, boolean subscribe) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| subscribe | Boolean | Whether to subscribe to the service. If true, the service is subscribed to and queries prefer the in-memory cache. If false, queries are sent directly to Nacos Server. | true |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
Return Parameters
Instance instance.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));// The following query requests have the same result.System.out.println(naming.selectOneHealthyInstance("nacos.test.service"));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", "DEFAULT_GROUP"));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", true));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", "DEFAULT_GROUP", true));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", new ArrayList<>()));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>()));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", new ArrayList<>(), true));System.out.println(naming.selectOneHealthyInstance("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>(), true));4.6. Subscribe to Service
Description
Listens for changes in the instance list under a service.
void subscribe(String serviceName, EventListener listener) throws NacosException;
void subscribe(String serviceName, String groupName, EventListener listener) throws NacosException;
void subscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException;
void subscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
| listener | EventListener | Callback listener | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));EventListener serviceListener = event -> { if (event instanceof NamingEvent) { System.out.println(((NamingEvent) event).getServiceName()); System.out.println(((NamingEvent) event).getInstances()); }};naming.subscribe("nacos.test.service", serviceListener);naming.subscribe("nacos.test.service", "DEFAULT_GROUP", serviceListener);naming.subscribe("nacos.test.service", new ArrayList<>(), serviceListener);naming.subscribe("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>(), serviceListener);Use a Custom Thread Pool for Asynchronous Listening
Nacos supports asynchronous listener callbacks with a custom thread pool. Replace EventListener with AbstractEventListener and implement the Executor getExecutor() method to return the custom thread pool. Nacos Client uses this thread pool for asynchronous callbacks when the service changes.
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));ExecutorService executorService = Executors.newFixedThreadPool(1);EventListener serviceListener = new AbstractEventListener() { @Override public void onEvent(Event event) { if (event instanceof NamingEvent) { System.out.println(((NamingEvent) event).getServiceName()); System.out.println(((NamingEvent) event).getInstances()); } }
@Override public Executor getExecutor() { return executorService; }};naming.subscribe("nacos.test.service", serviceListener);Listen to Service Change Diffs
Starting from Nacos 2.4.0, Nacos supports listening to service change diffs, including which instances are added, removed, or modified compared with the previous notification. Replace EventListener with AbstractNamingChangeListener and implement the onChange method. A NamingChangeEvent is passed to onChange, and InstancesDiff records the instance changes in this notification compared with the previous one.
To avoid issues caused by incorrect or abnormal diffs, NamingChangeEvent can still obtain the final service instance list through the getInstances method.
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));ExecutorService executorService = Executors.newFixedThreadPool(1);EventListener serviceListener = new AbstractNamingChangeListener() { @Override public void onChange(NamingChangeEvent event) { if (event.isAdded()) { System.out.println(event.getAddedInstances()); } if (event.isRemoved()) { System.out.println(event.getRemovedInstances()); } if (event.isModified()) { System.out.println(event.getModifiedInstances()); } }
@Override public Executor getExecutor() { return executorService; }};naming.subscribe("nacos.test.service", serviceListener);4.7. Unsubscribe from Service
Description
Cancels listening for changes in the instance list under a service.
void unsubscribe(String serviceName, EventListener listener) throws NacosException;
void unsubscribe(String serviceName, String groupName, EventListener listener) throws NacosException;
void unsubscribe(String serviceName, List<String> clusters, EventListener listener) throws NacosException;
void unsubscribe(String serviceName, String groupName, List<String> clusters, EventListener listener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
| listener | EventListener | Callback listener | None, required |
Note: When unsubscribing from a service, use the same
listenerthat was used for subscription. Otherwise, unsubscription may fail.
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));EventListener serviceListener = event -> {};naming.subscribe("nacos.test.service", serviceListener);naming.unsubscribe("nacos.test.service", serviceListener);naming.unsubscribe("nacos.test.service", "DEFAULT_GROUP", serviceListener);naming.unsubscribe("nacos.test.service", new ArrayList<>(), serviceListener);naming.unsubscribe("nacos.test.service", "DEFAULT_GROUP", new ArrayList<>(), serviceListener);4.8. Batch Register Service Instances
Description
Registers a series of instances to a specified service.
Because the same Nacos Client instance can register only one instance to a service, if the same Nacos Client instance registers instances to the same service multiple times, the later instance overwrites the earlier one. To support proxy registration scenarios in the community, such as Nacos-Sync and Proxy-Registry, where different instances of the same service need to be registered from one client, the community added the batch registration capability.
void batchRegisterInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| instances | List of Instance | Service instance list | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));
Instance instance1 = new Instance();instance1.setIp("127.0.0.1");instance1.setPort(8848);instance1.setClusterName("DEFAULT");
Instance instance2 = new Instance();instance2.setIp("127.0.0.1");instance2.setPort(9848);instance2.setClusterName("DEFAULT");
List<Instance> instances = new ArrayList<>(2);instances.add(instance1);instances.add(instance2);
naming.batchRegisterInstance("nacos.test.service", "DEFAULT_GROUP", instances);4.9. Batch Deregister Service Instances
Description
Deregisters a series of instances from a specified service.
This API is designed for users who use batch registration. It allows users to deregister some or all batch-registered instances.
void batchDeregisterInstance(String serviceName, String groupName, List<Instance> instances) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| instances | List of Instance | Service instance list | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));
Instance instance1 = new Instance();instance1.setIp("127.0.0.1");instance1.setPort(8848);instance1.setClusterName("DEFAULT");
Instance instance2 = new Instance();instance2.setIp("127.0.0.1");instance2.setPort(9848);instance2.setClusterName("DEFAULT");
List<Instance> instances = new ArrayList<>(2);instances.add(instance1);instances.add(instance2);
naming.batchRegisterInstance("nacos.test.service", "DEFAULT_GROUP", instances);naming.batchDeregisterInstance("nacos.test.service", "DEFAULT_GROUP", instances);4.10. Subscribe to Service with Selector
Description
Uses a selector with custom logic to listen for changes in the instance list under a service. When the service list changes, the custom selector filters the data. A callback notification is sent only when the filtered data still changes.
void subscribe(String serviceName, NamingSelector selector, EventListener listener) throws NacosException;
void subscribe(String serviceName, String groupName, NamingSelector selector, EventListener listener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
| selector | NamingSelector | Custom data selector | None, required |
| listener | EventListener | Callback listener | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));EventListener serviceListener = event -> { if (event instanceof NamingEvent) { System.out.println(((NamingEvent) event).getServiceName()); System.out.println(((NamingEvent) event).getInstances()); }};// Selects only subscribed instances whose IP starts with `127.0`.NamingSelector selector = NamingSelectorFactory.newIpSelector("127.0.*");naming.subscribe("nacos.test.service", "DEFAULT_GROUP", selector, serviceListener);Built-in Data Selectors
Nacos Client provides multiple built-in data selectors for default scenarios:
- Cluster selector:
NamingSelectorFactory.newClusterSelector(Collection<String> clusters). When theclustersparameter is passed during service subscription, Nacos Client automatically uses this data selector. - IP selector:
NamingSelectorFactory.newClusterSelector(String ipRegex). A callback is notified only when an instance IP matches the passedipRegex. - Metadata selector:
NamingSelectorFactory.newMetadataSelector(Map<String, String> metadata). A callback is notified only when the instance metadata contains all metadata entries passed to the selector. - Any metadata selector:
NamingSelectorFactory.newMetadataSelector(Map<String, String> metadata, false). A callback is notified when the instance metadata contains any one pair of metadata entries passed to the selector.
Develop a Custom Data Selector
In most cases, creating a custom data selector only requires creating DefaultNamingSelector. During construction, pass a Predicate<Instance> filter as the result that determines whether a single instance meets your filtering conditions. This is similar to the filter method in Java streams, so you only need to consider the filtering condition for a single instance.
If DefaultNamingSelector cannot meet your requirements, implement the NamingSelector interface, perform complex logic validation based on the passed NamingContext, and finally output NamingResult to Nacos Client.
4.11. Unsubscribe from Service with Selector
Description
If you use a selector with custom logic to listen for changes in the instance list under a service, use the unsubscribe from service with selector API to correctly cancel the listener.
Note: When unsubscribing, pass the same selector and listener used for subscription. Otherwise, unsubscription may fail.
void unsubscribe(String serviceName, NamingSelector selector, EventListener listener) throws NacosException;
void unsubscribe(String serviceName, String groupName, NamingSelector selector, EventListener listener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serviceName | string | Service name | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
| clusters | List of strings | Instance clusterName. If the list is empty, all instances are queried. | Empty list |
| selector | NamingSelector | Custom data selector | None, required |
| listener | EventListener | Callback listener | None, required |
Return Parameters
None.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));EventListener serviceListener = event -> { if (event instanceof NamingEvent) { System.out.println(((NamingEvent) event).getServiceName()); System.out.println(((NamingEvent) event).getInstances()); }};// Selects only subscribed instances whose IP starts with `127.0`.NamingSelector selector = NamingSelectorFactory.newIpSelector("127.0.*");naming.subscribe("nacos.test.service", "DEFAULT_GROUP", selector, serviceListener);naming.unsubscribe("nacos.test.service", "DEFAULT_GROUP", selector, serviceListener);4.12. Get Services with Pagination
Description
Get the list of services in the current client namespace with pagination.
ListView<String> getServicesOfServer(int pageNo, int pageSize) throws NacosException;
ListView<String> getServicesOfServer(int pageNo, int pageSize, String groupName) throws NacosException;Note: The
getServicesOfServeroverloads that takeAbstractSelectorare deprecated and should not be used; they are not listed in this document. They will be marked with@Deprecatedin the API in a future release. Use only the two-parameter or three-parameter overloads above.
Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| pageNo | int | Page number | None, required |
| pageSize | int | Number of services per page | None, required |
| groupName | string | Group name | DEFAULT_GROUP |
Return Parameters
Service name list: ListView<String>.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));// Equivalent to `naming.getServicesOfServer(1, 10, "DEFAULT_GROUP");`ListView<String> result = naming.getServicesOfServer(1, 10);System.out.println(result.getCount());System.out.println(result.getData());4.13. Get Services Subscribed by the Current Client
Description
Gets all services subscribed to by the current client.
List<ServiceInfo> getSubscribeServices() throws NacosException;Request Parameters
None.
Return Parameters
Service list: List<ServiceInfo>.
Request Example
NamingService naming = NamingFactory.createNamingService(System.getProperty("serveAddr"));System.out.println(naming.getSubscribeServices());4.14. Fuzzy Watch Service
Description
The fuzzyWatch API can batch-subscribe to services that match the specified group rule and serviceName rule. Use * for prefix fuzzy matching, suffix fuzzy matching, or bilateral fuzzy matching.
Fuzzy watch pushes only service add and delete events. It does not directly push the instance list under a service. In a service fuzzy watch listener, you can use the subscribe API together with fuzzy watch to listen for changes in the instance list under a service.
For stability, Nacos applies upper-limit protection to the number of fuzzy watch rules and the number of services matched by a single rule. For details, see Fuzzy Watch Service Capacity Protection.
/** * Subscribes to change events for all services in the current namespace that match the specified group rule and service name rule. * * @param serviceNamePattern service name matching rule * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener */void fuzzyWatch(String serviceNamePattern, String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Subscribes to change events for all services in the current namespace that match the specified group rule and serviceName rule, * and obtains the current matched service list in Future mode. * The fuzzy watch list is asynchronously returned through the watcher callback. * @param serviceNamePattern serviceName matching rule * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener * @return Future that can be used to wait for the asynchronous service push result */Future<ListView<String>> fuzzyWatchWithServiceKeys(String serviceNamePattern, String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Cancels the subscription to change events for all services in the current namespace that match the specified group rule. * @param serviceNamePattern service matching rule * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener to remove */void cancelFuzzyWatch(String serviceNamePattern, String groupNamePattern, FuzzyWatchEventWatcher watcher);
/** * Subscribes to change events for all services in the current namespace that match the specified group rule. * * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener */void fuzzyWatch(String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Subscribes to change events for all services in the current namespace that match the specified group rule. * A Future can be used to obtain the current matched service list. * * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener * @return Future that can be used to wait for the asynchronous service push result */Future<ListView<String>> fuzzyWatchWithServiceKeys(String groupNamePattern, FuzzyWatchEventWatcher watcher) throws NacosException;
/** * Cancels the subscription to service events for all services in the current namespace that match the specified group rule and service rule. * * @param groupNamePattern group matching rule * @param watcher fuzzy watch listener to remove */void cancelFuzzyWatch(String groupNamePattern, FuzzyWatchEventWatcher watcher) ;Request Parameters
| Parameter | Type | Description |
|---|---|---|
| serviceNamePattern | string | Service name matching rule. Supports prefix fuzzy matching, such as myservice*; suffix fuzzy matching, such as *service; and bilateral fuzzy matching, such as *service*. |
| groupNamePattern | string | Configuration group matching rule. Supports prefix fuzzy matching, such as mygroup*; suffix fuzzy matching, such as *mygroup; and bilateral fuzzy matching, such as *mygroup*. |
| watcher | FuzzyWatchEventWatcher | Fuzzy watch listener |
FuzzyWatchEventWatcher Fuzzy Watch Listener
| Method | Parameter Type | Description |
|---|---|---|
| onEvent | FuzzyWatchChangeEvent | Fuzzy watch callback event object |
| getExecutor | void | Specifies the thread pool used to execute callback events. If it is empty, callbacks are executed in the Nacos push thread. |
FuzzyWatchChangeEvent Fuzzy Watch Event
| Parameter | Type | Description |
|---|---|---|
| serviceName | string | Changed service name |
| groupName | string | Group of the changed service |
| namespace | string | Namespace of the changed service |
| changedType | string | Change type received by the client, including ADD_SERVICE for added services and DELETE_SERVICE for removed services |
| syncType | string | Type that triggered the change, including FUZZY_WATCH_INIT_NOTIFY for initial push of the existing service list, FUZZY_WATCH_DIFF_SYNC_NOTIFY for change reconciliation, and FUZZY_WATCH_RESOURCE_CHANGED for service change push |
Return Parameters
| Type | Description |
|---|---|
| Future<ListView | A Future object for the current matched service list. After the list of services matched by the rule is pushed to the client, you can obtain the list through the Future object. *Note: When capacity protection is triggered, the returned service list may be incomplete. |
// The returned parameter is a serviceKey list. You can use the NamingUtils utility class to obtain serviceName, groupName, and namespace. String[] serviceKeyItems = NamingUtils.parseServiceKey(serviceKey); String namespace = serviceKeyItems[0]; String groupName = serviceKeyItems[1]; String serviceName = serviceKeyItems[2];Fuzzy Watch Service Capacity Protection
For stability, to avoid server memory pressure and client push storms caused by too many rules or too many services matched by rules, Nacos provides capacity protection for fuzzy watch at two levels. When an upper limit is exceeded, fuzzy watch pushes are suppressed.
- Upper-limit protection for the number of fuzzy watch rules. The default upper limit is 20 and can be adjusted by the
nacos.naming.fuzzy.watch.max.pattern.countparameter. - Upper-limit protection for the number of services matched by a single fuzzy watch rule. The default upper limit is 500 and can be adjusted by the
nacos.naming.fuzzy.watch.max.pattern.match.service.countparameter.
When registering a fuzzy watch listener in the fuzzyWatch API, the listener can also implement FuzzyWatchLoadWatcher to perceive capacity protection events.
FuzzyWatchLoadWatcher Fuzzy Watch Load Listener
| Method | Description |
|---|---|
| onPatternOverLimit | Triggered when pushes for the current fuzzy watch rule are suppressed because the rule count exceeds the upper limit |
| onServiceReachUpLimit | Triggered when pushes for the current fuzzy watch rule are suppressed because the number of matched services reaches the upper limit |
*Note:
- When capacity protection is triggered, the service list returned by
fuzzyWatchWithServiceKeysmay be incomplete. - When service-count upper-limit protection is triggered, service offline events may also fail to be pushed because of the protection mechanism.
Request Example
try { // Initialize the configuration service. The console automatically obtains the following parameters through the sample code. String serverAddr = "{serverAddr}"; String serviceNamePattern = "service*"; String groupPattern = "group*"; Properties properties = new Properties(); properties.setProperty("username", System.getenv("NACOS_USERNAME")); properties.setProperty("password", System.getenv("NACOS_PASSWORD")); properties.put("serverAddr", serverAddr); properties.put("namespace", "mynamespaceId");
Future<ListView<String>> future = namingService.fuzzyWatchWithServiceKeys(serviceNamePattern, groupPattern, new AbstractFuzzyWatchEventWatcher() {@Overridepublic void onEvent(FuzzyWatchChangeEvent event) { System.out.println(event.toString()); }
@Overridepublic void onPatternOverLimit() { System.out.println("pattern service over limit ");
}
@Overridepublic void onServiceReachUpLimit() { System.out.println("pattern service over limit "); } }); } catch (NacosException e) { e.printStackTrace(); }4.15. Get server status
Description
Get the current naming server status.
String getServerStatus();Request parameters
None.
Return value
| Type | Description |
|---|---|
| String | Server status. |
Request example
NamingService naming = NacosFactory.createNamingService("{serverAddr}");String status = naming.getServerStatus();System.out.println(status);5. Distributed Lock API
The current version of distributed lock does not yet provide the corresponding operations API or lock listener API. Support will be added in later versions.
5.1. Acquire Distributed Lock
Description
This API attempts to acquire a distributed lock. It returns false if acquisition fails and true if acquisition succeeds.
Boolean lock(LockInstance instance) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| instance | LockInstance | Lock object instance for the distributed lock | None, required |
The LockInstance object contains the following parameters:
| Name | Type | Description | Default Value |
|---|---|---|---|
| key | String | Unique key of the distributed lock. For locks of the same type, the same key means the same lock is expected. | None, required |
| expiredTime | long | Expiration time of the distributed lock, in milliseconds. 0 means the lock is released immediately after it is acquired. If the configured value is less than 0, the default value 30000 is used. | 0 |
| params | Map<String,String> | Custom parameters used to extend custom lock attributes | None |
| lockType | String | Distributed lock type. Currently, only "NACOS_LOCK" is supported. | None |
Nacos currently provides one default lock type implementation,
"NACOS_LOCK", which can be quickly created throughnew NLock(). More lock types will be supported later.
Return Parameters
Lock acquisition result Boolean. Returns true if the lock is acquired; otherwise returns false.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");LockService lockService = NacosLockFactory.createLockService(properties);NLock nLock = new NLock("testLock", 5000L);try { if (lockService.lock(nLock)) { System.out.printf("try to lock `testLock` successfully."); } else { System.out.printf("try to lock `testLock` failed, please retry later."); }} catch (NacosException e) { e.printStackTrace();} finally { System.out.printf("try to unlock `testLock`, result: " + lockService.unLock(nLock));}5.2. Release Distributed Lock
Description
This API releases an acquired distributed lock. It returns true if release succeeds; otherwise returns false.
Boolean unLock(LockInstance instance) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| instance | LockInstance | Lock object instance for the distributed lock | None, required |
The LockInstance object contains the following parameters:
| Name | Type | Description | Default Value |
|---|---|---|---|
| key | String | Unique key of the distributed lock. For locks of the same type, the same key means the same lock is expected. | None, required |
| expiredTime | long | Expiration time of the distributed lock, in milliseconds. 0 means the lock is released immediately after it is acquired. If the configured value is less than 0, the default value 30000 is used. | 0 |
| params | Map<String,String> | Custom parameters used to extend custom lock attributes | None |
| lockType | String | Distributed lock type. Currently, only "NACOS_LOCK" is supported. | None |
Nacos currently provides one default lock type implementation,
"NACOS_LOCK", which can be quickly created throughnew NLock(). More lock types will be supported later.
Return Parameters
Lock release result Boolean. Returns true if the lock is released; otherwise returns false.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");LockService lockService = NacosLockFactory.createLockService(properties);NLock nLock = new NLock("testLock", 5000L);try { if (lockService.lock(nLock)) { System.out.printf("try to lock `testLock` successfully."); } else { System.out.printf("try to lock `testLock` failed, please retry later."); }} catch (NacosException e) { e.printStackTrace();} finally { System.out.printf("try to unlock `testLock`, result: " + lockService.unLock(nLock));}5.3. Remote Try Lock
Description
Use this API to directly send a remote lock request. The semantics are the same as lock: returns true on success and false on failure.
Boolean remoteTryLock(LockInstance instance) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| instance | LockInstance | Lock object instance for the distributed lock | None, required |
Return Parameters
Remote lock result Boolean: returns true if lock is acquired, otherwise false.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");LockService lockService = NacosLockFactory.createLockService(properties);NLock nLock = new NLock("testLock", 5000L);try { Boolean lockResult = lockService.remoteTryLock(nLock); System.out.printf("remote try lock result: " + lockResult);} catch (NacosException e) { e.printStackTrace();}5.4. Remote Release Lock
Description
Use this API to directly send a remote unlock request to release the distributed lock. Returns true on success and false on failure.
Boolean remoteReleaseLock(LockInstance instance) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| instance | LockInstance | Lock object instance for the distributed lock | None, required |
Return Parameters
Remote unlock result Boolean: returns true if lock is released, otherwise false.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");LockService lockService = NacosLockFactory.createLockService(properties);NLock nLock = new NLock("testLock", 5000L);try { Boolean lockResult = lockService.remoteTryLock(nLock); if (lockResult) { System.out.printf("remote try lock success, begin release."); Boolean releaseResult = lockService.remoteReleaseLock(nLock); System.out.printf("remote release result: " + releaseResult); }} catch (NacosException e) { e.printStackTrace();}6. MCP Service
6.1. Query MCP Service
Description
This API queries the details of a specified MCP service, including its metadata and callable Endpoint information. When no version is specified, it queries the latest published version.
McpServerDetailInfo getMcpServer(String mcpName) throws NacosException;
McpServerDetailInfo getMcpServer(String mcpName, String version) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| mcpName | String | MCP service name | None, required |
| version | String | MCP service version | Empty. When empty, the latest published version is queried. |
Return Parameters
MCP service details: McpServerDetailInfo.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { McpServerDetailInfo detailInfo = aiService.mcp().getMcpServer(mcpName, null); System.out.println(JsonUtils.toJson(detailInfo));} catch (Exception e) { e.printStackTrace();}6.2. Release New MCP Service Version
Description
This API releases a new MCP service version. If the MCP service is released for the first time, a new MCP service is created.
In direct-online mode, a request for an existing MCP service and version is idempotent and does not recreate or modify it. Draft mode requires a version that does not yet exist and no other pending version for the resource. If a conflict occurs, inspect and handle the existing draft through management entry points first.
3.3 adds createDraft overloads. Omitting it or setting it to false preserves direct-online publication. Setting it to true only creates a draft, without submitting, reviewing or publishing it. Complete the server upgrade to 3.3 and MCP lifecycle migration first. If the server does not support draft release, the call fails instead of publishing directly. See AI Resource Lifecycle for subsequent draft operations.
String releaseMcpServer(McpServerBasicInfo serverSpecification, McpToolSpecification toolSpecification) throws NacosException;
String releaseMcpServer(McpServerBasicInfo serverSpecification, McpToolSpecification toolSpecification, McpResourceSpecification resourceSpecification) throws NacosException;
String releaseMcpServer(McpServerBasicInfo serverSpecification, McpToolSpecification toolSpecification, McpEndpointSpec endpointSpecification) throws NacosException;
String releaseMcpServer(McpServerBasicInfo serverSpecification, McpToolSpecification toolSpecification, McpResourceSpecification resourceSpecification, McpEndpointSpec endpointSpecification) throws NacosException;
String releaseMcpServer(McpServerBasicInfo serverSpecification, McpToolSpecification toolSpecification, boolean createDraft) throws NacosException;
String releaseMcpServer(McpServerBasicInfo serverSpecification, McpToolSpecification toolSpecification, McpResourceSpecification resourceSpecification, McpEndpointSpec endpointSpecification, boolean createDraft) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| serverSpecification | McpServerBasicInfo | MCP service basic information | None, required |
| toolSpecification | McpToolSpecification | MCP service tool information | None, optional |
| resourceSpecification | McpResourceSpecification | MCP service resource and resource template information | None, optional |
| endpointSpecification | McpEndpointSpec | MCP service endpoint information | None, optional |
| createDraft | boolean | Whether to create only a lifecycle draft | false |
Return Parameters
MCP service ID: String. Receiving an ID does not mean that a draft is online.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { McpServerBasicInfo serverSpecification = new McpServerBasicInfo(); serverSpecification.setName("example-mcp"); serverSpecification.setProtocol("stdio"); serverSpecification.setLocalServerConfig( Collections.<String, Object>singletonMap("command", "my-mcp-server")); com.alibaba.nacos.api.ai.model.mcp.registry.ServerVersionDetail versionDetail = new com.alibaba.nacos.api.ai.model.mcp.registry.ServerVersionDetail(); versionDetail.setVersion("1.0.0"); serverSpecification.setVersionDetail(versionDetail); String mcpId = aiService.mcp().releaseMcpServer(serverSpecification, null, true); System.out.println(mcpId);} catch (Exception e) { e.printStackTrace();}The example creates a draft for a stdio service. Replace my-mcp-server with its actual command. Registering the definition does not start an MCP service; a consuming client starts the process according to its own configuration.
6.3. Register MCP Service Endpoint
Description
Registers an MCP service endpoint to the specified MCP service version.
If the registered MCP service does not exist, a service-not-found exception is thrown.
void registerMcpServerEndpoint(String mcpName, String address, int port) throws NacosException;
void registerMcpServerEndpoint(String mcpName, String address, int port, String version) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| mcpName | String | MCP service name | None, required |
| address | String | MCP service endpoint address | None, required |
| port | int | MCP service endpoint port | None, required |
| version | String | MCP service version | Empty. When empty, the endpoint is registered to the latest version. |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { aiService.mcp().registerMcpServerEndpoint(mcpName, "127.0.0.1", 8848, version);} catch (Exception e) { e.printStackTrace();}6.4. Deregister MCP Service Endpoint
Description
Deregisters an MCP service endpoint from the specified MCP service version.
void deregisterMcpServerEndpoint(String mcpName, String address, int port) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| mcpName | String | MCP service name | None, required |
| address | String | MCP service endpoint address | None, required |
| port | int | MCP service endpoint port | None, required |
Return Parameters
None.
Request Example
Reuse the aiService that registered endpoints in section 6.3:
try { aiService.mcp().deregisterMcpServerEndpoint(mcpName, "127.0.0.1", 8848);} catch (Exception e) { e.printStackTrace();}6.5. Subscribe to MCP Service
Description
Subscribes to an MCP service. When the MCP service releases a new version, a notification is received.
In the current version, subscription is implemented through polling queries, so notifications may have some delay. You can configure the
nacosAiMcpServerCacheUpdateIntervalparameter to adjust the query interval. The default value is 10000 ms.
McpServerDetailInfo subscribeMcpServer(String mcpName, AbstractNacosMcpServerListener mcpServerListener) throws NacosException;
McpServerDetailInfo subscribeMcpServer(String mcpName, String version, AbstractNacosMcpServerListener mcpServerListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| mcpName | String | MCP service name | None, required |
| version | String | MCP service version | Empty. When empty, the latest version is subscribed to. |
| listener | AbstractNacosMcpServerListener | MCP service listener | None, required |
Return Parameters
When subscription succeeds, the current MCP service details McpServerDetailInfo are returned.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { ExampleListener listener = new ExampleListener(i); aiService.mcp().subscribeMcpServer(mcpName, listener);} catch (Exception e) { e.printStackTrace();}private static class ExampleListener extends AbstractNacosMcpServerListener {
private final int id;
private ExampleListener(int id) { this.id = id; }
@Override public void onEvent(NacosMcpServerEvent event) { System.out.printf("---------------mcp server listener %s called start---------------%n", id); System.out.printf("mcp server namespaceId: %s, mcpId: %s, mcpName: %s%n", event.getNamespaceId(), event.getMcpId(), event.getMcpName()); System.out.println("mcp server endpoint: " + JsonUtils.toJson( event.getMcpServerDetailInfo().getBackendEndpoints())); System.out.println( "mcp server tools size: " + event.getMcpServerDetailInfo().getToolSpec().getTools().size()); System.out.println("mcp server version: " + event.getMcpServerDetailInfo().getVersionDetail().getVersion()); System.out.printf("---------------mcp server listener %s called end---------------%n", id); }}6.6. Unsubscribe from MCP Service
Description
Unsubscribes from an MCP service. The mcpServerListener passed during unsubscription must be the same as the one used during subscription. Otherwise, unsubscription may fail.
void unsubscribeMcpServer(String mcpName, AbstractNacosMcpServerListener mcpServerListener) throws NacosException;
void unsubscribeMcpServer(String mcpName, String version, AbstractNacosMcpServerListener mcpServerListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| mcpName | String | MCP service name | None, required |
| version | String | MCP service version | Empty. When empty, the latest version is unsubscribed from. |
| listener | AbstractNacosMcpServerListener | MCP service listener | None, required |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { ExampleListener listener = new ExampleListener(i); aiService.mcp().subscribeMcpServer(mcpName, listener); aiService.mcp().unsubscribeMcpServer(mcpName, listener);} catch (Exception e) { e.printStackTrace();}7. A2A Registry
3.3 preserves published A2A APIs through aiService.agent(). New applications can use the generic Agent APIs in section 11 and the RAD Integration Guide.
After the first successful server capability negotiation, the SDK fixes either the legacy A2A or RAD compatibility route for that instance. Recreate the SDK instance after upgrading an older server to enable RAD. RAD business failures do not fall back to the legacy route. See the SDK Runtime Guide for connection requirements.
7.1. Query AgentCard
Description
Queries an AgentCard.
AgentCardDetailInfo getAgentCard(String agentName) throws NacosException;
AgentCardDetailInfo getAgentCard(String agentName, String version) throws NacosException;
AgentCardDetailInfo getAgentCard(String agentName, String version, String registrationType) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| version | String | Agent version | Empty. When empty, the latest version is queried. |
| registrationType | String | Registration type | Empty. When empty, url is automatically filled based on the registrationType used during registration. |
Return Parameters
When the query succeeds, the current AgentCard details AgentCardDetailInfo are returned.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { AgentCardDetailInfo result = aiService.agent().getAgentCard(agentName); result = aiService.agent().getAgentCard(agentName, "1.0.0"); result = aiService.agent().getAgentCard(agentName, "1.0.0", "url");} catch (Exception e) { e.printStackTrace();}7.2. Release New AgentCard Version
Description
Releases a new AgentCard version. If release fails, an exception is thrown.
On the 3.3 RAD compatibility route, this method follows the publication rules in section 11.7: setAsLatest maps to autoSubmit. A new resource’s first version is always submitted; later versions or existing drafts follow this flag. The method does not force a version online or bypass review, and existing non-draft versions remain unchanged. The legacy A2A route retains its direct-publication behavior.
void releaseAgentCard(AgentCard agentCard) throws NacosException;
void releaseAgentCard(AgentCard agentCard, String registrationType) throws NacosException;
void releaseAgentCard(AgentCard agentCard, String registrationType, boolean setAsLatest) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentCard | AgentCard | AgentCard information | None, required |
| registrationType | String | URL or SERVICE. URL prefers declared addresses from the definition; SERVICE prefers runtime endpoints. | SERVICE |
| setAsLatest | boolean | Set the latest version on the legacy A2A route; automatically submit the draft on the RAD route. | false |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { AgentCard agentCard = new AgentCard(); agentCard.setName("test"); agentCard.setDescription("test for agent card"); agentCard.setUrl("http://localhost:8848"); agentCard.setVersion("1.0.0"); agentCard.setProtocolVersion("0.3.0"); agentCard.setPreferredTransport("JSONRPC"); aiService.agent().releaseAgentCard(agentCard); aiService.agent().releaseAgentCard(agentCard, "SERVICE"); aiService.agent().releaseAgentCard(agentCard, "SERVICE", false);} catch (NacosException e) { e.printStackTrace();}7.3. Register Agent Endpoint
Description
Registers an endpoint under an AgentCard.
void registerAgentEndpoint(String agentName, String version, String address, int port) throws NacosException;
void registerAgentEndpoint(String agentName, String version, String address, int port, String transport) throws NacosException;
void registerAgentEndpoint(String agentName, String version, String address, int port, String transport, String path) throws NacosException;
void registerAgentEndpoint(String agentName, String version, String address, int port, String transport, String path, boolean supportTls) throws NacosException;
void registerAgentEndpoint(String agentName, AgentEndpoint endpoint) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| version | String | Agent version | None, required |
| address | String | Agent IP address | None, required |
| port | int | Agent port | None, required |
| transport | String | Transport method of the endpoint: JSONRPC, GRPC, or HTTP+JSON | JSONRPC |
| path | String | Access path of the endpoint | Empty string |
| supportTls | boolean | Whether TLS is supported | false |
| endpoint | AgentEndpoint | AgentEndpoint | None, required |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { aiService.agent().registerAgentEndpoint("test", "1.0.0", "127.0.0.1", 8848); aiService.agent().registerAgentEndpoint("test", "1.0.0", "127.0.0.1", 8848, "JSONRPC"); aiService.agent().registerAgentEndpoint("test", "1.0.0", "127.0.0.1", 8848, "JSONRPC", ""); aiService.agent().registerAgentEndpoint("test", "1.0.0", "127.0.0.1", 8848, "JSONRPC", "", false); AgentEndpoint endpoint = new AgentEndpoint(); endpoint.setAddress("127.0.0.1"); endpoint.setPort(8848); endpoint.setTransport("JSONRPC"); endpoint.setPath(""); endpoint.setSupportTls(false); endpoint.setVersion("1.0.0"); aiService.agent().registerAgentEndpoint("test", endpoint);} catch (NacosException e) { e.printStackTrace();}7.4. Deregister Agent Endpoint
Description
Deregisters an endpoint from an AgentCard.
On the RAD compatibility route, passing even one address removes this SDK instance’s entire registration intent for the exact version, while preserving registrations for other versions. For partial endpoint removal, use the generic Agent API in section 11.6 and use the same generic API family from registration onward.
void deregisterAgentEndpoint(String agentName, String version, String address, int port) throws NacosException;
void deregisterAgentEndpoint(String agentName, AgentEndpoint endpoint) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| version | String | Agent version | None, required |
| address | String | Agent IP address | None, required |
| port | int | Agent port | None, required |
| endpoint | AgentEndpoint | AgentEndpoint | None, required |
Return Parameters
None.
Request Example
Reuse the aiService that registered endpoints in section 7.3:
try { aiService.agent().deregisterAgentEndpoint("test", "1.0.0", "127.0.0.1", 8848); AgentEndpoint endpoint = new AgentEndpoint(); endpoint.setAddress("127.0.0.1"); endpoint.setPort(8848); endpoint.setVersion("1.0.0"); aiService.agent().deregisterAgentEndpoint("test", endpoint);} catch (NacosException e) { e.printStackTrace();}7.5. Subscribe to AgentCard
Description
Subscribes to an AgentCard. When the AgentCard releases a new version, a notification is received.
The legacy A2A route polls at the interval set by
nacosAiAgentCardCacheUpdateInterval, which defaults to 10000 ms. The 3.3 RAD compatibility route uses Watch, with bounded polling when the capability is unavailable. The legacy parameter does not control every Agent subscription’s notification interval.
AgentCardDetailInfo subscribeAgentCard(String agentName, AbstractNacosAgentCardListener agentCardListener) throws NacosException;
AgentCardDetailInfo subscribeAgentCard(String agentName, String version, AbstractNacosAgentCardListener agentCardListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| version | String | Agent version | When empty, the latest version is subscribed to. |
| listener | AbstractNacosAgentCardListener | Listener | None, required |
Return Parameters
When subscription succeeds, the current AgentCard details AgentCardDetailInfo are returned.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { aiService.agent().subscribeAgentCard("test", new AbstractNacosAgentCardListener() { @Override public void onEvent(NacosAgentCardEvent event) { System.out.println("---------------agent card listener called start---------------"); System.out.println(JsonUtils.toJson(event.getAgentCard())); System.out.println("---------------agent card listener called end---------------"); } }); aiService.agent().subscribeAgentCard("test", "", new AbstractNacosAgentCardListener() { @Override public void onEvent(NacosAgentCardEvent event) { System.out.println("---------------agent card listener called start---------------"); System.out.println(JsonUtils.toJson(event.getAgentCard())); System.out.println("---------------agent card listener called end---------------"); } });} catch (NacosException e) { e.printStackTrace();}7.6. Unsubscribe from AgentCard
Unsubscribes from an AgentCard. The agentCardListener passed during unsubscription must be the same as the one used during subscription. Otherwise, unsubscription may fail.
Description
void unsubscribeAgentCard(String agentName, AbstractNacosAgentCardListener agentCardListener) throws NacosException;
void unsubscribeAgentCard(String agentName, String version, AbstractNacosAgentCardListener agentCardListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| version | String | Agent version | When empty, the latest version is unsubscribed from. |
| listener | AbstractNacosAgentCardListener | Listener | None, required |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);AbstractNacosAgentCardListener listener = new AbstractNacosAgentCardListener() {};try { aiService.agent().subscribeAgentCard("test", listener); aiService.agent().unsubscribeAgentCard("test", listener); aiService.agent().unsubscribeAgentCard("test", "", listener);} catch (NacosException e) { e.printStackTrace();}7.7. Batch Register Agent Endpoints
Batch-registers multiple endpoints under an AgentCard.
The same client can register only one set of endpoints to one AgentCard. Therefore, this API conflicts with Register Agent Endpoint: endpoints batch-registered by this API overwrite the previously registered endpoints for the same Agent. If the same client registers endpoints for multiple different AgentCards, they do not overwrite each other.
This API is available since version 3.1.1.
Description
void registerAgentEndpoint(String agentName, Collection<AgentEndpoint> endpoints) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| endpoints | Collection | Agent endpoint collection | None, required |
Note: The
versionof allAgentEndpointobjects inendpointsshould be non-empty and the same.
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { AgentEndpoint endpoint1 = new AgentEndpoint(); endpoint1.setAddress("127.0.0.1"); endpoint1.setPort(8848); endpoint1.setTransport("JSONRPC"); endpoint1.setPath(""); endpoint1.setSupportTls(false); endpoint1.setVersion("1.0.0");
AgentEndpoint endpoint2 = new AgentEndpoint(); endpoint2.setAddress("127.0.0.1"); endpoint2.setPort(8848); endpoint2.setTransport("JSONRPC"); endpoint2.setPath(""); endpoint2.setSupportTls(false); endpoint2.setVersion("1.0.0");
aiService.agent().registerAgentEndpoint("test", Arrays.asList(endpoint1, endpoint2));} catch (NacosException e) { e.printStackTrace();}8. Skill Capabilities
Skills are distributed via the Java SDK as ZIP packages containing SKILL.md and all resource files (binary resources are decoded from Base64 back to raw bytes). The SDK currently exposes three download variants: by name, by version, and by label, and also supports subscribing to Skill changes.
8.1. Download Skill ZIP
Description
Download the latest Skill ZIP package (byte array) by skill name.
byte[] downloadSkillZip(String skillName) throws NacosException;Request Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| skillName | String | Skill name (unique identifier) | — required |
Response
Skill ZIP bytes byte[] containing SKILL.md and all resource files.
Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { byte[] skillZip = aiService.skill().downloadSkillZip("{skillName}"); System.out.println("skill zip bytes: " + skillZip.length);} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when the skill is not found or on query error.
8.2. Download Skill ZIP by Version
Description
Download a specific version of the Skill ZIP package (byte array) by skill name and version.
byte[] downloadSkillZipByVersion(String skillName, String version) throws NacosException;Request Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| skillName | String | Skill name (unique identifier) | — required |
| version | String | Target skill version; latest when null | — |
Response
Skill ZIP bytes byte[].
Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { byte[] skillZip = aiService.skill().downloadSkillZipByVersion("{skillName}", "1.0.0"); System.out.println("skill zip bytes: " + skillZip.length);} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when the skill or specified version is not found, or on query error.
8.3. Download Skill ZIP by Label
Description
Download a labeled Skill ZIP package (byte array) by skill name and label.
byte[] downloadSkillZipByLabel(String skillName, String label) throws NacosException;Request Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| skillName | String | Skill name (unique identifier) | — required |
| label | String | Target label (for example: latest, stable) | — required |
Response
Skill ZIP bytes byte[].
Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { byte[] skillZip = aiService.skill().downloadSkillZipByLabel("{skillName}", "{label}"); System.out.println("skill zip bytes: " + skillZip.length);} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when the skill or specified label is not found, or on query error.
8.4. Subscribe Skill
Description
Subscribe to Skill changes; the listener is invoked when the Skill ZIP content changes. version and label are optional to narrow the subscription.
byte[] subscribeSkill(String skillName, String version, String label, AbstractNacosSkillListener skillListener) throws NacosException;Request Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| skillName | String | Skill name | — required |
| version | String | Target skill version, optional | — |
| label | String | Target label, optional | — |
| skillListener | AbstractNacosSkillListener | Callback listener for Skill changes | — required |
Response
Returns current Skill ZIP bytes byte[] when subscribed successfully; may be null if the skill is not found.
Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { byte[] skillZip = aiService.skill().subscribeSkill("{skillName}", null, "{label}", new AbstractNacosSkillListener() { @Override public void onEvent(NacosSkillEvent event) { System.out.println("skill changed: " + event.getSkillName()); System.out.println("resolved version: " + event.getResolvedVersion()); System.out.println("md5: " + event.getMd5()); } }); System.out.println("skill zip bytes: " + (skillZip != null ? skillZip.length : 0));} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when request parameters are invalid or subscription processing fails.
8.5. Unsubscribe Skill
Description
Unsubscribe from Skill changes. skillName, version, label, and listener must match those used when subscribing.
void unsubscribeSkill(String skillName, String version, String label, AbstractNacosSkillListener skillListener) throws NacosException;Request Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| skillName | String | Skill name | — required |
| version | String | Target version, optional | — |
| label | String | Target label, optional | — |
| skillListener | AbstractNacosSkillListener | Listener used when subscribing | — required |
Response
None.
Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);AbstractNacosSkillListener listener = new AbstractNacosSkillListener() {};try { aiService.skill().subscribeSkill("{skillName}", null, "{label}", listener); aiService.skill().unsubscribeSkill("{skillName}", null, "{label}", listener);} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when request parameters are invalid or unsubscribe processing fails.
9. Prompt Capabilities
9.1. Get Prompt
Description
Get the Prompt object for the current version by prompt key.
Prompt getPrompt(String promptKey) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| promptKey | String | Prompt key (unique identifier) | None, required |
Return Parameters
| Type | Description |
|---|---|
| Prompt | Prompt object for current version |
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { Prompt prompt = aiService.prompt().getPrompt("{promptKey}"); System.out.println(JsonUtils.toJson(prompt));} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when prompt not found or query error.
9.2. Get Prompt by Version
Description
Get the Prompt object by prompt key and version. Returns latest version when version is null.
Prompt getPromptByVersion(String promptKey, String version) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| promptKey | String | Prompt key (unique identifier) | None, required |
| version | String | Target version. null means latest. | None |
Return Parameters
| Type | Description |
|---|---|
| Prompt | Prompt object for the specified version |
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { Prompt prompt = aiService.prompt().getPromptByVersion("{promptKey}", "1.0.0"); System.out.println(JsonUtils.toJson(prompt));} catch (NacosException e) { e.printStackTrace();}9.3. Get Prompt by Label
Description
Get the Prompt object by prompt key and label.
Prompt getPromptByLabel(String promptKey, String label) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| promptKey | String | Prompt key (unique identifier) | None, required |
| label | String | Target label | None, required |
Return Parameters
| Type | Description |
|---|---|
| Prompt | Prompt object for that label |
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { Prompt prompt = aiService.prompt().getPromptByLabel("{promptKey}", "{label}"); System.out.println(JsonUtils.toJson(prompt));} catch (NacosException e) { e.printStackTrace();}9.4. Subscribe Prompt
Description
Subscribe to prompt changes; the listener is invoked when prompt configuration changes. version and label are optional to narrow the subscription.
Prompt subscribePrompt(String promptKey, String version, String label, AbstractNacosPromptListener promptListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| promptKey | String | Prompt key | None, required |
| version | String | Target version, optional | None |
| label | String | Target label, optional | None |
| promptListener | AbstractNacosPromptListener | Callback listener for prompt changes | None, required |
Return Parameters
| Type | Description |
|---|---|
| Prompt | Current Prompt on subscribe success, or null if not found |
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { Prompt prompt = aiService.prompt().subscribePrompt("{promptKey}", null, null, new AbstractNacosPromptListener() { @Override public void onEvent(NacosPromptEvent event) { System.out.println("prompt changed: " + event.getPromptKey()); } }); System.out.println(JsonUtils.toJson(prompt));} catch (NacosException e) { e.printStackTrace();}9.5. Unsubscribe Prompt
Description
Unsubscribe from prompt changes. version, label, and listener must match those used when subscribing.
void unsubscribePrompt(String promptKey, String version, String label, AbstractNacosPromptListener promptListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| promptKey | String | Prompt key | None, required |
| version | String | Target version, optional | None |
| label | String | Target label, optional | None |
| promptListener | AbstractNacosPromptListener | Listener used when subscribing | None, required |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);AbstractNacosPromptListener listener = new AbstractNacosPromptListener() {};try { aiService.prompt().subscribePrompt("{promptKey}", null, null, listener); aiService.prompt().unsubscribePrompt("{promptKey}", null, null, listener);} catch (NacosException e) { e.printStackTrace();}10. AgentSpec Capabilities
10.1. Load AgentSpec
Description
Load the complete AgentSpec object by AgentSpec name, including main configuration and all resource configurations.
AgentSpec loadAgentSpec(String agentSpecName) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentSpecName | String | AgentSpec name (unique identifier) | None, required |
Return Parameters
Returns AgentSpec object AgentSpec on success.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { AgentSpec agentSpec = aiService.agentSpec().loadAgentSpec("{agentSpecName}"); System.out.println(JsonUtils.toJson(agentSpec));} catch (NacosException e) { e.printStackTrace();}10.2. Subscribe AgentSpec
Description
Subscribe to a specific AgentSpec. The listener is invoked when the AgentSpec configuration changes.
AgentSpec subscribeAgentSpec(String agentSpecName, AbstractNacosAgentSpecListener agentSpecListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentSpecName | String | AgentSpec name | None, required |
| agentSpecListener | AbstractNacosAgentSpecListener | Callback listener for AgentSpec changes | None, required |
Return Parameters
Returns current AgentSpec AgentSpec when subscribed successfully; may be null if not found.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);try { AgentSpec agentSpec = aiService.agentSpec().subscribeAgentSpec("{agentSpecName}", new AbstractNacosAgentSpecListener() { @Override public void onEvent(NacosAgentSpecEvent event) { System.out.println("agent spec changed: " + event.getAgentSpecName()); } }); System.out.println(JsonUtils.toJson(agentSpec));} catch (NacosException e) { e.printStackTrace();}10.3. Unsubscribe AgentSpec
Description
Unsubscribe from a specific AgentSpec. The listener must be the same instance used during subscription.
void unsubscribeAgentSpec(String agentSpecName, AbstractNacosAgentSpecListener agentSpecListener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentSpecName | String | AgentSpec name | None, required |
| agentSpecListener | AbstractNacosAgentSpecListener | Listener used when subscribing | None, required |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");AiService aiService = AiFactory.createAiService(properties);AbstractNacosAgentSpecListener listener = new AbstractNacosAgentSpecListener() {};try { aiService.agentSpec().subscribeAgentSpec("{agentSpecName}", listener); aiService.agentSpec().unsubscribeAgentSpec("{agentSpecName}", listener);} catch (NacosException e) { e.printStackTrace();}11. Agent Management and Discovery
Obtain Agent management and discovery APIs through aiService.agent(). They cover catalog search, definition discovery, subscriptions, definition publication and Runtime Endpoint registration. The legacy A2A APIs in chapter 7 remain compatible; prefer the generic APIs for new integrations. See RAD Integration for a complete example.
Agent APIs use the namespace configured when AiService is created. Search, RegistrationBatch, Reference and publication requests do not have a namespace field; the SDK defensively copies caller-owned data. Shared models are in com.alibaba.nacos.api.ai.model.agent, and the publication request is in its client subpackage. These APIs are available since 3.3.0.
11.1. Search Agents
Description
Search for visible and enabled Agents that have at least one online version in the current namespace. agentNameContains performs a case-sensitive literal substring match. Values in tagsAll are ANDed, while values in protocolsAny are ORed.
Page<AgentSummary> searchAgents(AgentSearchRequest request) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| request | AgentSearchRequest | Agent search conditions | None, required |
AgentSearchRequest contains the following fields:
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentNameContains | String | Case-sensitive literal substring of the Agent name | None |
| tagsAll | List<String> | Tags that the Agent must contain | None |
| protocolsAny | List<String> | Calling protocols that any online version may expose | None |
| pageNo | Integer | One-based page number | 1 |
| pageSize | Integer | Number of entries per page, from 1 through 100 | 20 |
Return Parameters
Returns Page<AgentSummary> with total count, current page, available pages and catalog entries. Online versions are in versionInfo.onlineVersions; the default version is versionInfo.labels.latest. Entries do not contain full protocol definitions or Endpoints. Search can briefly lag behind publication, and a matching entry does not guarantee healthy endpoints.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");properties.setProperty(PropertyKeyConst.NAMESPACE, "{namespaceId}");AiService aiService = AiFactory.createAiService(properties);
AgentSearchRequest request = new AgentSearchRequest();request.setAgentNameContains("order");request.setTagsAll(Arrays.asList("production", "public"));request.setProtocolsAny(Collections.singletonList("a2a"));request.setPageNo(1);request.setPageSize(20);
Page<AgentSummary> page = aiService.agent().searchAgents(request);System.out.println(JsonUtils.toJson(page));Exceptions
Throws NacosException when the request is null, page parameters are out of range, or the remote request fails.
11.2. Discover an Agent
Description
Resolve a complete Agent discovery snapshot by Agent name and an optional exact version or label. An optional filter can prune calling protocols and Endpoint sets. The filter only prunes the current result: it neither selects another Agent version nor performs load balancing.
AgentDiscoveryResult discoverAgent(AgentReference reference) throws NacosException;
AgentDiscoveryResult discoverAgent(AgentReference reference, AgentDiscoveryFilter filter) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| reference | AgentReference | Agent reference | None, required |
| filter | AgentDiscoveryFilter | Optional protocol, transport, Endpoint-source, and metadata filter | None |
AgentReference contains the following fields:
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| version | String | Exact online version; mutually exclusive with label | None |
| label | String | Label resolved to an online version at request time; mutually exclusive with version | None |
Every AgentDiscoveryFilter field is optional:
| Name | Type | Description |
|---|---|---|
| protocols | List<String> | Allowed calling protocols; values are ORed |
| protocolVersion | String | Exact protocol-version match against candidate interfaces |
| transports | List<String> | Allowed transports; values are ORed |
| endpointSources | List<EndpointSource> | Allowed RUNTIME or DECLARED sources |
| metadataSelector | Map<String, String> | Exact key/value pairs that Endpoint metadata must contain |
Return Parameters
Returns AgentDiscoveryResult with Agent name, resolved version, contentDigest and callInterfaces. Addresses are in each interface’s endpointSets[].endpoints[]. Interfaces or address sets may be empty, and Runtime Endpoints may have healthy=false. Select addresses by protocol, source, health, priority and weight before invoking the Agent service.
When both version and label are absent, definition metadata uses the current latest version while Runtime Endpoints may be compatible with any current online version. Explicit label=latest is stricter: both definition metadata and Runtime Endpoints are restricted to the current latest version.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");properties.setProperty(PropertyKeyConst.NAMESPACE, "{namespaceId}");AiService aiService = AiFactory.createAiService(properties);
AgentReference reference = new AgentReference();reference.setAgentName("{agentName}");
AgentDiscoveryFilter filter = new AgentDiscoveryFilter();filter.setProtocols(Collections.singletonList("a2a"));filter.setEndpointSources(Arrays.asList(EndpointSource.RUNTIME, EndpointSource.DECLARED));
AgentDiscoveryResult result = aiService.agent().discoverAgent(reference, filter);System.out.println(JsonUtils.toJson(result));Exceptions
Throws NacosException when the Agent reference is invalid, version and label are both set, the filter is invalid, the Agent or target version does not exist, or the remote request fails.
11.3. Subscribe to an Agent
Description
Subscribe using the same Agent reference and optional filter as Discover. 3.3 supports HTTP long-poll Watch or gRPC notifications; without the corresponding server capability, the SDK falls back to bounded Discover polling. Listeners receive complete replacement snapshots, not field deltas. Delivery of every intermediate change is not guaranteed.
Replace the previous result on SNAPSHOT; stop using it on UNAVAILABLE. A temporarily missing target can remain subscribed until it returns. After terminal errors such as authentication or capacity failures, resolve the cause and subscribe again. The SDK handles transient connection failures through reconnection.
AgentDiscoveryResult subscribeAgent(AgentReference reference, AbstractNacosAgentDiscoveryListener listener) throws NacosException;
AgentDiscoveryResult subscribeAgent(AgentReference reference, AgentDiscoveryFilter filter, AbstractNacosAgentDiscoveryListener listener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| reference | AgentReference | Same Agent reference used by Discover | None, required |
| filter | AgentDiscoveryFilter | Same optional filter used by Discover | None |
| listener | AbstractNacosAgentDiscoveryListener | Listener for complete replacement snapshots | None, required |
Return Parameters
Returns the current AgentDiscoveryResult, or possibly null while the target is absent, retaining the subscription.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");properties.setProperty(PropertyKeyConst.NAMESPACE, "{namespaceId}");AiService aiService = AiFactory.createAiService(properties);
AgentReference reference = new AgentReference();reference.setAgentName("{agentName}");
AbstractNacosAgentDiscoveryListener listener = new AbstractNacosAgentDiscoveryListener() { @Override public void onEvent(NacosAgentDiscoveryEvent event) { if (event.getType() == NacosAgentDiscoveryEventType.SNAPSHOT) { AgentDiscoveryResult snapshot = event.getAgentDiscoveryResult(); System.out.println("agent changed: " + JsonUtils.toJson(snapshot)); } else { System.err.println(event.getErrorCode() + ": " + event.getErrorMessage()); } }};
AgentDiscoveryResult current = aiService.agent().subscribeAgent(reference, listener);System.out.println(JsonUtils.toJson(current));Exceptions
Throws NacosException when the reference, filter, or listener is invalid, or the initial Discover request fails.
11.4. Unsubscribe from an Agent
Description
Cancel this SDK’s Agent subscription. Pass the same AgentReference, equivalent filter and listener instance used to subscribe. Unsubscribing does not deregister endpoints or stop the Agent service.
void unsubscribeAgent(AgentReference reference, AbstractNacosAgentDiscoveryListener listener) throws NacosException;
void unsubscribeAgent(AgentReference reference, AgentDiscoveryFilter filter, AbstractNacosAgentDiscoveryListener listener) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| reference | AgentReference | Agent reference used to subscribe | None, required |
| filter | AgentDiscoveryFilter | Filter used to subscribe; use null for an unfiltered subscription | None |
| listener | AbstractNacosAgentDiscoveryListener | Same listener instance used to subscribe | None, required |
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");properties.setProperty(PropertyKeyConst.NAMESPACE, "{namespaceId}");AiService aiService = AiFactory.createAiService(properties);
AgentReference reference = new AgentReference();reference.setAgentName("{agentName}");AbstractNacosAgentDiscoveryListener listener = new AbstractNacosAgentDiscoveryListener() { @Override public void onEvent(NacosAgentDiscoveryEvent event) { if (event.getType() == NacosAgentDiscoveryEventType.SNAPSHOT) { System.out.println(JsonUtils.toJson(event.getAgentDiscoveryResult())); } else { System.err.println(event.getErrorCode() + ": " + event.getErrorMessage()); } }};try { aiService.agent().subscribeAgent(reference, listener); aiService.agent().unsubscribeAgent(reference, listener);} catch (NacosException e) { e.printStackTrace();}Exceptions
Throws NacosException when the reference, filter, or listener is invalid.
11.5. Register Agent Runtime Endpoints
Description
Register this SDK publisher’s complete Runtime Endpoint batch for one Agent protocol. Registering the same (agentName, protocol) again for the same publisher completely replaces the previous batch, so omitted Endpoints are removed. The SDK retains the complete batch as redo intent after reconnecting. Endpoint registration does not implicitly create an Agent definition.
void registerAgentEndpoints(AgentEndpointRegistrationBatch batch) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| batch | AgentEndpointRegistrationBatch | Complete Endpoint registration batch | None, required |
AgentEndpointRegistrationBatch contains the following fields:
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| runtimeVersion | String | Default runtime version unless overridden by an Endpoint; every Endpoint must resolve a version | None |
| versionRange | String | Default compatible range unless overridden; must contain the effective runtime version | Exact runtime version if omitted at both levels |
| protocol | String | Canonical protocol token for the Endpoints | None, required |
| endpoints | List<Endpoint> | This publisher’s complete Endpoint set, containing 1 through 1000 entries | None, required |
Endpoint contains the following fields:
| Name | Type | Description | Default Value |
|---|---|---|---|
| uri | String | Complete absolute calling URI | None, required |
| transport | String | Canonical transport, such as HTTP+JSON | None, required |
| priority | Integer | Priority; lower values are preferred | 0 |
| weight | Double | Weight among Endpoints with the same priority | 1 |
| metadata | Map<String, String> | Flat Endpoint metadata | None |
Endpoints also accept healthy and enabled (both default to true and cannot be null), plus bindings. When omitted, bindings inherits the batch values; when supplied, it must contain exactly one RuntimeVersionBinding. Its runtimeVersion and versionRange override the batch defaults independently. The effective range must contain the runtime version. Discovery excludes disabled contributions but can still return unhealthy endpoints.
Endpoints are identified by URI host, effective port and transport. Different paths or queries alone cannot identify two endpoints from the same publisher. Deregister endpoints and close AiService on normal exit; see RAD Integration for a complete lifecycle.
Return Parameters
None.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");properties.setProperty(PropertyKeyConst.NAMESPACE, "{namespaceId}");AiService aiService = AiFactory.createAiService(properties);
Endpoint endpoint = new Endpoint();endpoint.setUri("https://agent.example.com:443/a2a");endpoint.setTransport("HTTP+JSON");endpoint.setPriority(0);endpoint.setWeight(1.0D);
AgentEndpointRegistrationBatch batch = new AgentEndpointRegistrationBatch();batch.setAgentName("{agentName}");batch.setRuntimeVersion("1.1.0");batch.setVersionRange("[1.0.0,2.0.0)");batch.setProtocol("a2a");batch.setEndpoints(Collections.singletonList(endpoint));
aiService.agent().registerAgentEndpoints(batch);Exceptions
Throws NacosException when the batch or an Endpoint is invalid, the effective range excludes the runtime version, or publication fails.
11.6. Deregister Agent Runtime Endpoints
Description
Remove endpoints identified by URI host, effective port and transport from this SDK publisher’s set. The SDK re-registers the complete remaining set; when empty, it deregisters the publisher’s entire set under (agentName, protocol).
void deregisterAgentEndpoints(String agentName, String protocol, List<Endpoint> endpoints) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | Required |
| protocol | String | Calling protocol | Required |
| endpoints | List<Endpoint> | Endpoints to remove | Non-empty list |
The SDK supplies the namespace. Each Endpoint only needs uri and transport; use addresses that were actually registered, and reuse the same aiService instance that registered them.
Return Parameters
None.
Request Example
Reuse the aiService that registered endpoints in section 11.5:
Endpoint endpoint = new Endpoint();endpoint.setUri("https://agent.example.com:443/a2a");endpoint.setTransport("HTTP+JSON");
aiService.agent().deregisterAgentEndpoints("{agentName}", "a2a", Collections.singletonList(endpoint));Exceptions
Throws NacosException when the Agent name, protocol or endpoint identity is invalid, or deregistration fails.
11.7. Publish an Agent Definition from Code
Description
Publish one exact Agent version from application code, using the AiService namespace.
| Target version state | Behavior |
|---|---|
| Newly created first version of a resource | Automatically submits through the ordinary pipeline, even with autoSubmit=false. |
| A later new version or an existing editable draft | Creates or fully replaces the draft definition, then follows autoSubmit. |
| Already reviewing, reviewed, online or offline | Returns the existing version successfully without overwriting, resubmitting or bringing it online. |
Submission can require review, so the returned version is not necessarily online. This operation never force-publishes. Updating a draft replaces its entire calling-interface list, removing omitted protocols. Failure does not guarantee that no draft was saved: inspect the current version before deciding to publish again. The SDK does not replay an unknown publication outcome across transports.
The
AgentProviderandAgentVersionDetailtypes in this section are fromcom.alibaba.nacos.api.ai.model.agent, not the same-named legacy A2A types undercom.alibaba.nacos.api.ai.model.a2a.
AgentVersionDetail publishAgent(AgentPublishRequest request) throws NacosException;Request Parameters
| Name | Type | Description | Default Value |
|---|---|---|---|
| request | AgentPublishRequest | Agent definition publication request | None, required |
com.alibaba.nacos.api.ai.model.agent.client.AgentPublishRequest shares definition fields with management draft requests and adds autoSubmit:
| Name | Type | Description | Default Value |
|---|---|---|---|
| agentName | String | Agent name | None, required |
| displayName | String | Optional presentation name when the Agent is first created | None |
| description | String | Optional catalog description when the Agent is first created | None |
| iconUrl | String | Optional icon URI when the Agent is first created | None |
| provider | AgentProvider | Optional provider name and url when the Agent is first created | None |
| tags | List<String> | Optional public catalog tags when the Agent is first created | None |
| extensions | Map<String, Object> | Optional public extensions when the Agent is first created | None |
| version | String | Exact SemVer version to create | None, required |
| callInterfaces | List<AgentCallInterface> | Non-empty ordered protocol definitions; exactly one of this and basedOnVersion must be set | Conditionally required |
| basedOnVersion | String | Exact source version whose content is reused; mutually exclusive with callInterfaces | Conditionally required |
| author | String | Version author | None |
| changeDescription | String | Version change description | None |
| autoSubmit | boolean | Whether to submit a later version or editable draft; a newly created first version always submits | false |
AgentCallInterface contains the protocol, protocol version, descriptorMediaType, native nativeDescriptor, endpointSourceOrder and endpointSets. Source order must contain RUNTIME and DECLARED exactly once each. Definition addresses belong in an EndpointSet with source=DECLARED; do not include runtime endpoints. Protocols cannot repeat within a version.
The first version of an Agent must provide callInterfaces directly because no source version exists yet; basedOnVersion is invalid for that initial creation. A subsequent version must still choose exactly one of direct content and one exact source version.
Return Parameters
Returns the exact AgentVersionDetail, including the namespace, Agent name, version, current status, calling interfaces, author, change description, contentDigest, and audit timestamps.
Request Example
Properties properties = new Properties();properties.setProperty("username", System.getenv("NACOS_USERNAME"));properties.setProperty("password", System.getenv("NACOS_PASSWORD"));properties.setProperty(PropertyKeyConst.SERVER_ADDR, "{serverAddr}");properties.setProperty(PropertyKeyConst.NAMESPACE, "{namespaceId}");AiService aiService = AiFactory.createAiService(properties);
AgentInterface binding = new AgentInterface();binding.setUrl("https://agent.example.com:443/a2a");binding.setProtocolBinding("HTTP+JSON");binding.setProtocolVersion("1.0");
AgentCapabilities capabilities = new AgentCapabilities();capabilities.setStreaming(Boolean.TRUE);AgentCard card = new AgentCard();card.setName("{agentName}");card.setDescription("Order Agent");card.setVersion("1.0.0");card.setSupportedInterfaces(Collections.singletonList(binding));card.setCapabilities(capabilities);
Endpoint declaredEndpoint = new Endpoint();declaredEndpoint.setUri(binding.getUrl());declaredEndpoint.setTransport(binding.getProtocolBinding());
AgentCallInterface callInterface = new AgentCallInterface();callInterface.setProtocol("a2a");callInterface.setProtocolVersion("1.0");callInterface.setDescriptorMediaType("application/json");callInterface.setNativeDescriptor( JsonUtils.toObj(JsonUtils.toJson(card), Map.class));callInterface.setEndpointSourceOrder( Arrays.asList(EndpointSource.DECLARED, EndpointSource.RUNTIME));EndpointSet declaredSet = new EndpointSet();declaredSet.setSource(EndpointSource.DECLARED);declaredSet.setEndpoints(Collections.singletonList(declaredEndpoint));callInterface.setEndpointSets(Collections.singletonList(declaredSet));
AgentPublishRequest request = new AgentPublishRequest();request.setAgentName("{agentName}");request.setDisplayName("Order Agent");request.setVersion("1.0.0");request.setCallInterfaces(Collections.singletonList(callInterface));request.setAuthor("{author}");request.setChangeDescription("Initial version");request.setAutoSubmit(true);
AgentVersionDetail detail = aiService.agent().publishAgent(request);System.out.println(JsonUtils.toJson(detail));Exceptions
Throws NacosException when the request is null, the version or definition is invalid, the exclusive-or rule for callInterfaces and basedOnVersion is violated, concurrent updates conflict, submission fails, or the server does not support the capability.
12. Java SDK Lifecycle
A Nacos Java SDK instance owns thread pools, connections, subscriptions and other resources from construction until shutdown(). Reuse instances while the application runs and close them when replacing an instance or exiting to avoid resource leaks.
aiService.mcp(), agent(), skill(), agentSpec() and prompt() share the parent AiService namespace and lifecycle; close them together with aiService.shutdown(). Deregister Agent/MCP runtime endpoints through the instance that registered them. Unsubscribing does not deregister endpoints. Automatic reconnection and heartbeat recovery do not replace handling authentication, permission and terminal subscription errors. See the SDK Runtime Guide.