本文共 3782 字,大约阅读时间需要 12 分钟。
Nacos(阿里云开发者工具)是一款强大且灵活的服务发现、配置管理和服务管理平台。其主要功能包括:
Nacos官网地址:https://nacos.io/zh-cn/index.html
Nacos的运行依赖Java环境,并且需要一些特定的软件安装。以下是安装前需要注意的事项:
依赖环境:
操作系统要求:
安装步骤:
git clone https://gitee.com/mirrors/Nacos.git && cd Nacos/ && mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U。git clone https://gitee.com/mirrors/Nacos.git。cd Nacos/ && mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U。distribution/target/目录下生成安装包nacos-server-2.0.3.tar.gz和nacos-server-2.0.3.zip。Linux/Unix/Mac:
cd /Nacos/distribution/target/nacos-server-2.0.3/nacos/bin/。sh startup.sh -m standalone。sh shutdown.sh。Windows系统:
cd /Nacos/distribution/target/nacos-server-2.0.3/nacos/bin/。startup.cmd -m standalone。shutdown.cmd。注意事项:
netstat -ano | findstr 8080查找进程号,taskkill /F /PID命令即可杀死进程。taskkill /F /PID。admin/nacos。命名空间介绍:用于管理不同环境(如测试、预发、生产)的服务和配置,支持定义环境特定的配置值。
创建和修改命名空间:可通过Nacos控制台或API进行操作。
配置参数:
dataId:配置ID,采用package.class格式,确保全局唯一性。group:配置分组,建议使用产品名和模块名结合。timeoutMs:读取配置的超时时间。动态配置更新:支持自动刷新和监听机制,确保配置实时生效。
com.alibaba.nacos nacos-client 2.0.3 com.google.guava guava 31.0.1-jre org.slf4j slf4j-log4j12 1.7.21
@Testpublic void test01() throws NacosException { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); properties.put(PropertyKeyConst.NAMESPACE, "26df001f-7eaa-49eb-95fe-8a28f6acdb7e"); ConfigService configService = NacosFactory.createConfigService(properties); String config = configService.getConfig("tmall", "DEFAULT_GROUP", 3000); System.out.println(config);} 依赖配置:
com.alibaba.nacos nacos-spring-context 1.1.1
Nacos配置注解驱动:
@Configuration@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))public class NacosConfiguration { @NacosPropertySource(dataId = "tmail", groupId = "DEFAULT_GROUP", autoRefreshed = true) // 配置内容}节点数量:至少3个节点。
安装步骤:
nacos-cluster目录,并在其下创建nacos1、nacos2、nacos3目录。2.上传编译好的nacos-server-2.0.4.tar.gz到每个节点目录。启动命令:
sh startup.sh -p embedded
集群验证:登录任一节点,通过控制台操作配置,配置将自动同步到其他节点。
@Testpublic void test01() throws NacosException { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "192.168.80.128:7777,192.168.80.128:8888,192.168.80.128:9999"); ConfigService configService = NacosFactory.createConfigService(properties); String config = configService.getConfig("tmail", "DEFAULT_GROUP", 3000); System.out.println(config);} 在application.properties中配置:
nacos.config.server-addr=192.168.80.128:7777,192.168.80.128:8888,192.168.80.128:9999
CAP定理指出在分布式系统中,无法同时满足一致性(Consistency)、可用性(Availability)、分区容错性(Partition tolerance)。Nacos在不同角色中采取不同的一致性策略:
转载地址:http://iwcfk.baihongyu.com/