Alibaba Nacos配置中心初体验

it2023-02-01  51

 

官方文档:https://nacos.io/en-us/docs/use-nacos-with-kubernetes.html

安装部署

依赖

64bit OS: Linux/Unix/Mac/Windows supported, Linux/Unix/Mac recommended.64bit JDK 1.8+: downloads, JAVA_HOME settings.Maven 3.2.x+: downloads, settings.

 

下载软件包

下载地址 https://github.com/alibaba/nacos/releases

tar -xvf nacos-server-$version.tar.gz cd nacos/bin # 启动服务 sh startup.sh -m standalone # 关闭服务 sh shutdown.sh

默认启动8848端口,访问 http://localhost:8848/nacos/index.html

初始用户名密码均为 nacos

 

Nacos初体验

新建命名空间

 

新建配置

 

python代码示例

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import nacos SERVER_ADDRESSES = "localhost" # 这里是命名空间的ID,不是命名空间名称 NAMESPACE = "001" client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE) # get config data_id = "aa" group = "bb" print(client.get_config(data_id=data_id, group=group))

 

Java代码示例

import java.util.Properties; import java.util.concurrent.Executor; import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; /** * Config service example * * @author Nacos * */ public class GetNacos { public static void main(String[] args) throws NacosException, InterruptedException { String serverAddr = "10.210.100.144"; String dataId = "aa"; String group = "bb"; Properties properties = new Properties(); properties.put(PropertyKeyConst.NAMESPACE, "001"); properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); System.out.println(content); configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { System.out.println("recieve:" + configInfo); } @Override public Executor getExecutor() { return null; } }); //将配置内容修改为content boolean isPublishOk = configService.publishConfig(dataId, group, "content"); System.out.println(isPublishOk); Thread.sleep(3000); content = configService.getConfig(dataId, group, 5000); System.out.println(content); boolean isRemoveOk = configService.removeConfig(dataId, group); System.out.println(isRemoveOk); Thread.sleep(3000); content = configService.getConfig(dataId, group, 5000); System.out.println(content); Thread.sleep(300000); } }

 

 

 

最新回复(0)