import java.util.Properties;
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 lombok.extern.slf4j.Slf4j;
@Slf4j
public class Test {
private static final String SERVER_ADDR = "ip:8848";
private static final String DATA_ID = "yr.properties";
private static final String GROUP = "你的group";
private static final String NAMESPACE = "dev(你的namespace)";
public static void main(String[] args) {
// 创建 Nacos 客户端
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, SERVER_ADDR);
properties.setProperty(PropertyKeyConst.NAMESPACE, NAMESPACE);
ConfigService configService = null;
try {
configService = NacosFactory.createConfigService(properties);
} catch (NacosException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 查询配置
try {
String config = configService.getConfig(DATA_ID, GROUP, 5000);
log.info(config);
} catch (NacosException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Java 获取nacos配置
最新推荐文章于 2024-10-09 16:02:01 发布