Skip to content
OpenClaw 不踩坑恶意 Skills ,企业需 Skills Registry:Nacos 3.2 发布Know more

Java SDK Properties

1. How the Java SDK Reads Configuration

1.1. Introduction

The Nacos Java SDK uses NacosClientProperties to manage client configuration items in a unified way. Its role is similar to Spring Environment.

1.2. Features

  • Unified management of Properties, JVM parameters, environment variables, and default values.
  • Priority-based lookup. The default lookup order is properties -> JVM parameters -> environment variables -> default values. You can adjust the priority order. By default, properties has the highest priority.
  • Configuration isolation. Except for global configuration sources, each NacosClientProperties object is isolated from the others.

1.3. Usage

1.3.1. Priority

The default priority is properties. You can adjust it in either of the following ways:

- (JVM parameter) -Dnacos.env.first=PROPERTIES|JVM|ENV
- (environment variable) NACOS_ENV_FIRST=PROPERTIES|JVM|ENV

The default behavior is equivalent to -Dnacos.env.first=PROPERTIES.

If both methods are configured, the client first reads the priority setting from the JVM parameter. If the JVM parameter is not found, it reads the setting from the environment variable. If neither method specifies a priority, the default priority is properties.

Default priority: default_order.png

Priority: PROPERTIES default_order.png

Priority: JVM jvm_order.png

Priority: ENV jvm_order.png

1.3.2. Lookup

NacosClientProperties searches configuration by the specified priority. Using the default priority (PROPERTIES) as an example, if you want to obtain the value of a key named key1, the lookup order is as follows:

search_order.png

NacosClientProperties searches in the order shown above until it finds a value.

1.3.3. Configuration Isolation

To support scenarios with multiple registries or multiple config centers, NacosClientProperties introduces configuration isolation. NacosClientProperties has four value sources: user-defined properties, JVM parameters, environment variables, and default values. Among them, JVM parameters, environment variables, and default values are shared globally and cannot be isolated. Therefore, only the user-defined Properties object can be isolated. Each NacosClientProperties object contains a different Properties object, so configurations do not affect each other.

Note: Globally shared configuration, including JVM parameters, environment variables, and default values, cannot be changed after initialization. The setProperty method cannot modify them. setProperty only modifies values in the Properties object contained by the current NacosClientProperties object.

1.3.4. Configuration Derivation

Configuration derivation is introduced on top of configuration isolation so that configuration can be inherited. All NacosClientProperties objects are derived from the NacosClientProperties.PROTOTYPE object. There is no other way to create a NacosClientProperties object.

// global properties
NacosClientProperties.PROTOTYPE.setProperty("global-key1","global-value1");
// properties1
NacosClientProperties properties1 = NacosClientProperties.PROTOTYPE.derive();
properties1.
setProperty("properties1-key1","properties1-value1");
// properties2
NacosClientProperties properties2 = properties1.derive();
properties2.
setProperty("properties2-key1","properties2-value1");

The preceding code is shown in the following diagram: derive.png

How does lookup work in this case? Using the default priority (PROPERTIES) as an example:

// value == global-value1
String value = properties2.getProperty("global-key1");

derive_search.png

1.3.5. API

MethodInputReturnDescription
getPropertykey: StringStringGets the value for the specified key. Returns null if the key does not exist.
getPropertykey: String, default: StringStringGets the value for the specified key. Returns the default value if the key does not exist.
getBooleankey: StringBooleanGets the Boolean value for the specified key. Returns null if the key does not exist.
getBooleankey: String, default: BooleanBooleanGets the Boolean value for the specified key. Returns the default value if the key does not exist.
getIntegerkey: StringIntegerGets the Integer value for the specified key. Returns null if the key does not exist.
getIntegerkey: String, default: IntegerIntegerGets the Integer value for the specified key. Returns the default value if the key does not exist.
getLongkey: StringLongGets the Long value for the specified key. Returns null if the key does not exist.
getLongkey: String, default: LongLongGets the Long value for the specified key. Returns the default value if the key does not exist.
setPropertykey: String, value: StringvoidSets the key-value pair in the NacosClientProperties object. Existing values are overwritten.
addPropertiesproperties: PropertiesvoidAdds Properties to the NacosClientProperties object. Existing values are overwritten.
containsKeykey: StringbooleanChecks whether the specified key exists. Returns true if it exists; otherwise returns false.
asPropertiesvoidPropertiesConverts the NacosClientProperties object to a Properties object.
derivevoidNacosClientPropertiesCreates a NacosClientProperties object that inherits all configuration from its parent and contains empty Properties.
derivePropertiesNacosClientPropertiesCreates a NacosClientProperties object that inherits all configuration from its parent and contains the specified Properties.

2. Java SDK Configuration Parameter List

2.1. Common Parameters

Common parameters take effect when initializing both the registry NamingService and the config center ConfigServie:

ParameterPropertyKeyConst KeyMeaningOptional ValuesDefault Value
serverAddrSERVER_ADDRThe address list of Nacos Server, which specifies the Nacos Server accessed by this Java SDK.Any domain name or IP address. Separate multiple addresses with commas (,). All addresses must belong to the same Nacos Server cluster.None
contextPathCONTEXT_PATHThe context path of the Nacos Server OpenAPI, corresponding to the Nacos Server server.servlet.context-path parameter.Any URL-supported pathnacos
endpointENDPOINTThe endpoint used to obtain Nacos Server addresses. The Java SDK queries the actual domain names or IP addresses of Nacos Server through this endpoint.Any domain name or IP addressNone
endpointPortENDPOINT_PORTThe port of the endpoint used to obtain Nacos Server addresses. Used together with endpoint, for example, ${endpoint}:${endpointPort}/nacos/serverlist.0~655358080
endpointContextPathENDPOINT_CONTEXT_PATHThe context path of the endpoint used to obtain Nacos Server addresses. Used together with endpoint, for example, ${endpoint}:${endpointPort}/${endpointContextPath}/serverlist.Any URL-supported pathnacos
endPointClusterNameENDPOINT_CLUSTER_NAMEThe cluster name of Nacos Server in the endpoint. Used together with endpoint, for example, ${endpoint}:${endpointPort}/${endpointContextPath}/${endPointClusterName}.Any URL-supported pathserverlist
endpointQueryParamsENDPOINT_QUERY_PARAMSRequest parameters for the endpoint used to obtain Nacos Server addresses. They are used to extend custom logic in the endpoint service and use the key=value format.Any URL parameter in key=value formatNone
endpointRefreshIntervalSecondsENDPOINT_REFRESH_INTERVAL_SECONDSThe interval at which Nacos Server periodically obtains the address list again from the endpoint, in seconds.Any positive integer30
namespaceNAMESPACEThe namespace ID to which this Java SDK belongs. After it is set, the SDK can access only resources (configuration or services) in this namespace.Namespace IDEmpty string “
usernameUSERNAMEThe username used to access Nacos Server after authentication is enabled.Any stringNone
passwordPASSWORDThe password that corresponds to the username used to access Nacos Server after authentication is enabled.Any stringNone
accessKeyACCESS_KEYThe access key required when Alibaba Cloud RAM authentication is used.Any stringNone
secretKeySECRET_KEYThe secret key required when Alibaba Cloud RAM authentication is used.Any stringNone
ramRoleNameRAM_ROLE_NAMEThe RAM role name required when Alibaba Cloud RAM authentication is used.Any stringNone
signatureRegionIdSIGNATURE_REGION_IDThe signature region ID required when Alibaba Cloud RAM authentication is used.Any stringNone
logAllPropertiesLOG_ALL_PROPERTIESWhether to print all parameters when the Java SDK starts, including custom properties, JVM parameters, and environment variables. It is mainly used for debugging and troubleshooting.booleanfalse
clusterNameCLUSTER_NAMEDeprecated because it can be confused with the service instance ClusterName. Use endPointClusterName instead.Any URL-supported pathserverlist
isAdaptClusterNameUsageIS_ADAPT_CLUSTER_NAME_USAGEWhether to support setting endPointClusterName through clusterName for better upgrade compatibility.booleanfalse
serverNameSERVER_NAMEThe name of this Java SDK. It is currently used only when accessing the endpoint. This parameter will be deprecated because it is rarely used and the name is unreasonable.Any stringAutomatically concatenated from serverAddr or endpoint

2.2. Config Center Parameters

The following parameters take effect only when initializing the config center ConfigServie:

ParameterPropertyKeyConst KeyMeaningOptional ValuesDefault Value
clientWorkerMaxThreadCountCLIENT_WORKER_MAX_THREAD_COUNTAutomatically calculates the maximum thread pool size used by the config center ConfigService for configuration listeners.int value >= 2Number of CPUs
clientWorkerThreadCountCLIENT_WORKER_THREAD_COUNTSpecifies the thread pool size used by the config center ConfigService for configuration listeners. This has a higher priority than clientWorkerMaxThreadCount.int value >= 2Max(2, Min(clientWorkerMaxThreadCount, number of CPUs))
enableRemoteSyncConfigENABLE_REMOTE_SYNC_CONFIGImmediately synchronizes and notifies listened configurations with the server when the config center ConfigService listens to configurations. Enabling this may slow down listener startup.booleanfalse
configRequestTimeoutCONFIG_REQUEST_TIMEOUTSpecifies the RPC request timeout for the config center ConfigService. It is disabled by default and uses the common timeout configured in RpcClientConfig.long value >= 0-1
configRetryTimeCONFIG_RETRY_TIMEDeprecated. The long-polling retry interval used by the old config center implementation.Any int2000
configLongPollTimeoutCONFIG_LONG_POLL_TIMEOUTDeprecated. The long-polling timeout used by the old config center implementation.Any int30000
maxRetryMAX_RETRYDeprecated. The maximum retry count used by the old config center implementation.Any int3

2.3. Registry Parameters

The following parameters take effect only when initializing the registry NamingServie:

ParameterPropertyKeyConst KeyMeaningOptional ValuesDefault Value
namingLoadCacheAtStartNAMING_LOAD_CACHE_AT_STARTWhether the registry NamingService reads the local disk cache to initialize data during startup.booleanfalse
namingCacheRegistryDirNAMING_CACHE_REGISTRY_DIRThe extension of the local disk cache directory name for the registry NamingService. It is used to distinguish multiple NamingService instances on the same node.Any stringEmpty string
namingAsyncQuerySubscribeServiceNAMING_ASYNC_QUERY_SUBSCRIBE_SERVICEWhether the registry NamingService enables asynchronous query subscription as a fallback when the data push link is abnormal.booleanfalse
namingPollingMaxThreadCountNAMING_POLLING_MAX_THREAD_COUNTAutomatically calculates the maximum number of threads used by the registry NamingService for asynchronous query subscription.int value >= 1Number of CPUs
namingPollingThreadCountNAMING_POLLING_THREAD_COUNTSpecifies the number of threads used by the registry NamingService for asynchronous query subscription. This has a higher priority than namingPollingMaxThreadCount.int value >= 1Max(2, Min(namingPollingMaxThreadCount, number of CPUs))
namingRequestDomainMaxRetryCountNAMING_REQUEST_DOMAIN_RETRY_COUNTThe maximum retry count after a request to Nacos Server fails when the registry NamingService is initialized with only one address in serverAddr.Any int value3
namingPushEmptyProtectionNAMING_PUSH_EMPTY_PROTECTIONWhether the registry NamingService enables empty push protection. When the subscribed service address list is empty, the client ignores the address list.booleanfalse
redoDelayTimeREDO_DELAY_TIMEThe interval, in milliseconds, at which the registry NamingService checks and performs redo operations after the connection to Nacos Server is disconnected.Any long value3000
redoDelayThreadCountREDO_DELAY_THREAD_COUNTThe number of threads used by the registry NamingService to perform redo operations.Any int value1
namingRequestTimeoutNAMING_REQUEST_TIMEOUTSpecifies the RPC request timeout for the registry NamingService. It is disabled by default and uses the common timeout configured in RpcClientConfig.long value >= 0-1
namingClientBeatThreadCountNAMING_CLIENT_BEAT_THREAD_COUNTDeprecated. The number of threads used by the old registry NamingService implementation to send heartbeats for registered service instances.Any int valueNone

2.4. Connection Parameters

When the Nacos Java SDK connects to Nacos Server, you can configure a set of parameters to improve fault tolerance during network jitter.

ParameterMeaningOptional ValuesDefault Value
nacos.server.grpc.port.offsetThe offset of the Nacos Server gRPC port relative to the main port.Any int value1000
nacos.remote.client.grpc.nameThe name of the gRPC connection for this Nacos Java SDK.Any stringnull
nacos.remote.client.grpc.connect.keep.aliveThe keep-alive interval of the gRPC connection for this Nacos Java SDK.Any long value5000
nacos.remote.client.grpc.retry.timesThe maximum retry count when the gRPC connection of this Nacos Java SDK initiates a request.Any int value3
nacos.remote.client.grpc.timeoutThe request timeout when the gRPC connection of this Nacos Java SDK initiates a request.Any long value3000
nacos.remote.client.grpc.pool.aliveThe thread keep-alive time of the thread pool used by the gRPC connection of this Nacos Java SDK, in milliseconds.Any long value10000
nacos.remote.client.grpc.pool.core.sizeThe minimum size of the thread pool used by the gRPC connection of this Nacos Java SDK.Any int valueNumber of CPUs * 2
nacos.remote.client.grpc.pool.max.sizeThe maximum size of the thread pool used by the gRPC connection of this Nacos Java SDK.Any int valueNumber of CPUs * 8
nacos.remote.client.grpc.server.check.timeoutThe timeout for connection registration when the gRPC connection of this Nacos Java SDK has just connected to the server.Any long value3000
nacos.remote.client.grpc.queue.sizeThe request queue length of the gRPC connection for this Nacos Java SDK.Any int value10000
nacos.remote.client.grpc.health.retryThe retry count for gRPC connection health checks. If health checks fail this many times, the client forcibly closes the connection and reconnects.Any int value3
nacos.remote.client.grpc.health.timeoutThe timeout for gRPC connection health checks.Any long value3000
nacos.remote.client.grpc.maxinbound.message.sizeThe maximum size, in bytes, of a single request for the gRPC connection of this Nacos Java SDK.Any int value10M
nacos.remote.client.grpc.channel.keep.aliveThe keep-alive interval of the TCP channel corresponding to the gRPC connection of this Nacos Java SDK. This value should be greater than connect.keep.alive, in milliseconds.Any int value6 * 60 * 1000
nacos.remote.client.grpc.channel.keep.alive.timeoutThe keep-alive timeout of the TCP channel corresponding to the gRPC connection of this Nacos Java SDK, in milliseconds.Any long value20 * 1000
nacos.remote.client.grpc.channel.capability.negotiation.timeoutThe TLS handshake timeout of the TCP channel corresponding to the gRPC connection of this Nacos Java SDK.Any long value5000

2.5. Other Parameters

Some parameters in the Nacos Java SDK have little impact at runtime and must remain globally consistent. Therefore, these parameters currently need to be set through JVM parameters (-D) or environment variables. In most cases, use the default values and configure them only for special scenarios.

ParameterMeaningOptional ValuesDefault Value
PER_TASK_CONFIG_SIZEThe maximum number of configurations that each ConfigService can listen to.Any int3000
JM.SNAPSHOT.PATHThe local snapshot root directory of the Nacos Java SDK. The naming and config directories are created under the root directory to store cache information for subscribed services and configurations.Any directory${user.home}
JM.LOG.PATHThe log output directory of the Nacos Java SDK. Normally, Nacos Java SDK logs are written to this directory. In some special scenarios and versions, logs may be written to business logs, such as when log4j 1.0 is used or Spring Cloud reloads the logging configuration.Any directory${user.home}/logs
nacos.server.portThe default port for the Nacos Server config center and authentication login. When the serverAddr parameter does not contain a port, the SDK uses this port to connect to Nacos Server. We recommend setting the port uniformly through the serverAddr parameter.0~655358848
nacos.naming.exposed.portThe default port for the Nacos Server registry. When the serverAddr parameter does not contain a port, the SDK uses this port to connect to Nacos Server. We recommend setting the port uniformly through the serverAddr parameter.0~655358848
nacos.client.contextPathThe default Nacos Server contentPath. It is used when contextPath and endpointContextPath are not provided.Any URL-supported pathnacos
nacos.env.firstThe configuration lookup order of NacosClientProperties in the Nacos Java SDK. For details, see 1.3.1. Priority.PROPERTIES/JVM/ENVPROPERTIES
project.nameThe application name to which this SDK belongs. It can be used in the service subscriber list and configuration subscriber list as a reference field only.Any stringunknown
NACOS.CONNECT.TIMEOUTDeprecated. The connection timeout used by the old HTTP implementation when connecting to a service.Any int1000
NACOS.READ.TIMEOUTThe read timeout used by the old HTTP implementation when connecting to a service.Any int3000