This topic explains how to use Nacos Client SDK.
Get the SDK
To get Nacos Client SDK, add the following configuration to the pom.xml file in the Maven project.
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${latest.version}</version>
</dependency>
<! -- Remove the following if logging implementation is available. -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
Example
Run the following code in your project for configuration management.
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.listener.impl.PropertiesListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
/**
* Sample code, for sample test only.
*/
public class NacosExample {
private static final Logger LOGGER = LoggerFactory.getLogger(NacosExample.class);
public static void main(String[] args) throws NacosException {
Properties properties = new Properties();
String dataId = "com.alibaba.nacos.example";
String group = "DEFAULT_GROUP";
String content = "connectTimeoutInMills=5000";
// Copy endpoint and namespace from the namespace details on the namespaces page of the console
properties.put(PropertyKeyConst.ENDPOINT, "${endpoint}");
properties.put(PropertyKeyConst.NAMESPACE, "${namespace}");
// We recommend that you use the accessKey and secretKey of the RAM account.
properties.put(PropertyKeyConst.ACCESS_KEY, "${accessKey}");
properties.put(PropertyKeyConst.SECRET_KEY, "${secretKey}");
ConfigService configService = NacosFactory.createConfigService(properties);
// Publish configuration
boolean publishConfig = configService.publishConfig(dataId, group, content);
LOGGER.info("publishConfig: {}", publishConfig);
wait2Sync();
// Query configuration
String config = configService.getConfig(dataId, group, 5000);
LOGGER.info("getConfig: {}", config);
// Listen for configuration changes
configService.addListener(dataId, group, new PropertiesListener() {
@Override
public void innerReceive(Properties properties) {
LOGGER.info("innerReceive: {}", properties);
}
});
// Update configuration
boolean updateConfig = configService.publishConfig(dataId, group, "connectTimeoutInMills=3000");
LOGGER.info("updateConfig: {}", updateConfig);
wait2Sync();
// Delete configuration
boolean removeConfig = configService.removeConfig(dataId, group);
LOGGER.info("removeConfig: {}", removeConfig);
wait2Sync();
config = configService.getConfig(dataId, group, 5000);
LOGGER.info("getConfig: {}", config);
}
private static void wait2Sync() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// ignore
}
}
}
Options of passing parameters
To help you get onboard, the sample code initializes the parameters with code. However, the real production process may involve different environments (namely different accounts, regions, or namespaces), and the parameters vary with environments, so you must use variables to pass the parameters. To facilitate the input parameter configuration and reduce the configuration cost, ACM provides multiple options of passing parameters.
Note EDAS automatically injects for you during the publishing, and therefore you don’t need to take any action.
Initialization parameters | How to pass parameters |
---|---|
endpoint | From highest priority to lowest priority:
|
namespace | From highest priority to lowest priority:
|
accessKey/secretKey | From highest priority to lowest priority:
|