Nacos is a dynamic service discovery, configuration and service management platform. ElasticJob supports using Nacos 3.x as a registry center.
Each ElasticJob key (for example /my-job/config) is stored as one Nacos config. The ElasticJob namespace is mapped to the Nacos config group.
Because a Nacos dataId only accepts letters, digits and _-.:, every path segment is encoded with Base64URL and joined with ..
Requires
com.alibaba.nacos:nacos-client:3.2.4(pulled in automatically by the artifact below).
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-registry-center-nacos</artifactId>
<version>${elasticjob.version}</version>
</dependency>
| Name | Data Type | Default Value | Description |
|---|---|---|---|
| serverLists | String | Nacos server address list | |
| namespace | String | ElasticJob namespace, mapped to the Nacos config group | |
| tenant | String | Nacos tenant (namespace id), empty means public |
|
| username | String | Authentication username | |
| password | String | Authentication password | |
| timeoutMs | long | 3000 | Timeout of config operations in milliseconds |
serverLists:
Include IP and port, multiple addresses are separated by commas, such as: 127.0.0.1:8848,127.0.0.2:8848.
The nacos:// prefix is optional and will be stripped automatically, so both nacos://127.0.0.1:8848 and 127.0.0.1:8848 work.
When using RegistryCenterFactory (SPI), the nacos:// prefix is required so the Nacos creator can be selected:
RegistryCenterFactory.createCoordinatorRegistryCenter("nacos://127.0.0.1:8848", "elasticjob", null).
namespace:
ElasticJob namespace, mapped to the Nacos config group for isolating different job clusters. It is recommended to use meaningful names, such as: elasticjob.
tenant:
The Nacos tenant (namespace id), used to isolate different environments such as development, test and production. Empty means public.
Note that this is distinct from the ElasticJob namespace, which is mapped to the Nacos config group.
username and password:
Nacos authentication information. They can also be passed as the digest argument (username:password) of RegistryCenterFactory.
Notes and limitations:
import org.apache.shardingsphere.elasticjob.reg.nacos.NacosConfiguration;
import org.apache.shardingsphere.elasticjob.reg.nacos.NacosRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
public class NacosRegistryCenterExample {
public static void main(String[] args) {
NacosConfiguration nacosConfig = new NacosConfiguration("nacos://127.0.0.1:8848", "elasticjob");
nacosConfig.setUsername("nacos");
nacosConfig.setPassword("nacos");
nacosConfig.setTimeoutMs(3000L);
CoordinatorRegistryCenter registryCenter = new NacosRegistryCenter(nacosConfig);
registryCenter.init();
// Use registry center...
registryCenter.close();
}
}
elasticjob:
reg-center:
type: nacos
server-lists: nacos://127.0.0.1:8848,127.0.0.2:8848
namespace: elasticjob
tenant: ""
username: nacos
password: nacos
timeout-ms: 3000
<elasticjob:nacos id="regCenter"
server-lists="nacos://127.0.0.1:8848,127.0.0.2:8848"
namespace="elasticjob"
tenant=""
username="nacos"
password="nacos"
timeout-ms="3000" />
import org.apache.shardingsphere.elasticjob.lifecycle.internal.reg.RegistryCenterFactory;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
public class NacosFactoryExample {
public static void main(String[] args) {
CoordinatorRegistryCenter registryCenter = RegistryCenterFactory.createCoordinatorRegistryCenter("nacos://127.0.0.1:8848", "elasticjob", null);
// Use registry center...
}
}