Ignore the empty nacos configuration and get it based on dataId[common.yaml] & group[DEFAULT_GROUP]
时间: 2024-02-06 22:04:54 浏览: 104
To ignore the empty Nacos configuration and get it based on dataId[common.yaml] & group[DEFAULT_GROUP], you can use the following code in Java:
```
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import java.util.Properties;
import java.util.concurrent.Executor;
public class NacosConfigExample {
public static void main(String[] args) throws NacosException {
String serverAddr = "localhost:8848";
String dataId = "common.yaml";
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
ConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId, group, 5000);
if (content == null || content.isEmpty()) {
System.out.println("Empty configuration!");
} else {
System.out.println(content);
}
}
}
```
This code uses the Nacos API to create a `ConfigService` instance and retrieve the configuration based on the specified `dataId` and `group`. If the configuration is empty, it prints a message to the console.
阅读全文