Alibaba Nacos配置中心初体验

 

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

安装部署

依赖

  1. 64bit OS: Linux/Unix/Mac/Windows supported, Linux/Unix/Mac recommended.
  2. 64bit JDK 1.8+: downloads, JAVA_HOME settings.
  3. 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);

    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值