nacos学习笔记
1.概览
官网地址:https://nacos.io/zh-cn/docs/what-is-nacos.html
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。
2.使用
1. 踩坑纪录(版本)
有时候版本不一致,会导致服务注册不成功
Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
---|---|---|---|---|---|
2.2.3.RELEASE | 1.8.0 | 1.3.3 | 4.4.0 | 2.7.8 | 1.3.0 |
2.2.2.RELEASE | 1.8.0 | 1.3.2 | 4.4.0 | 2.7.8 | 1.3.0 |
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE | 1.7.1 | 1.2.1 | 4.4.0 | 2.7.6 | 1.2.0 |
2.2.0.RELEASE | 1.7.1 | 1.1.4 | 4.4.0 | 2.7.4.1 | 1.0.0 |
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE | 1.7.0 | 1.1.4 | 4.4.0 | 2.7.3 | 0.9.0 |
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE | 1.6.3 | 1.1.1 | 4.4.0 | 2.7.3 | 0.7.1 |
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
---|---|---|
Spring Cloud Hoxton.SR8 | 2.2.3.RELEASE | 2.3.2.RELEASE |
Spring Cloud Hoxton.SR8 | 2.2.2.RELEASE | 2.3.2.RELEASE |
Spring Cloud Hoxton.SR3 | 2.2.1.RELEASE | 2.2.5.RELEASE |
Spring Cloud Hoxton.RELEASE | 2.2.0.RELEASE | 2.2.X.RELEASE |
Spring Cloud Greenwich | 2.1.2.RELEASE | 2.1.X.RELEASE |
Spring Cloud Finchley | 2.0.2.RELEASE | 2.0.X.RELEASE |
Spring Cloud Edgware | 1.5.1.RELEASE(停止维护,建议升级) | 1.5.X.RELEASE |
###2. 安装
git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
ls -al distribution/target/
// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin
//start
sh startup.sh -m standalone
###3.服务注册
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
###4.服务发现
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'
###5.发布配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
###6.获取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
3.SpringCloud框架集成
###1.添加依赖
版本一定要注意!!!
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${latest.version}</version>
</dependency>
###2.其他详细的简单使用
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
###3.企业配置文件集成
配置文件:bootstrap.yml
server:
port: 8083
spring:
profiles:
#获取pom中的环境配置(一般放在最上层的pom中)
active: @spring.profiles.active@
#获取pom中的项目名称(子pom)
application:
name: @project.name@
cloud:
nacos:
#配置的nacos的地址
addr: @nacos.url@
discovery:
server-addr: ${spring.cloud.nacos.addr}
#pom中的namespace
namespace: ${nacos.group.id}
group: DEFAULT_GROUP
config:
prefix: ${project.name}
namespace: ${nacos.group.id}
group: DEFAULT_GROUP
server-addr: ${spring.cloud.nacos.addr}
file-extension: yaml
enabled: true
#公用的配置 比如说公用的一些参数可以放在一个文件里面
#这里定义的是application-dev.yaml或者application-test.yaml
#根据工作的那个环境来配置
shared-configs[0]:
data-id: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 默认为DEFAULT_GROUP
group: DEFAULT_GROUP
## 是否动态刷新,默认为false
refresh: true
子pom插件
<finalName>${project.name}</finalName>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<!--springBoot支持默认的$符填充配置文件的value-->
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
父pom
<profile>
<id>开发环境</id>
<properties>
<!--nacos namespace ID-->
<nacos.group.id>fc8373a3-a3b6-4172-ac9b-2d1d17e09413</nacos.group.id>
<nacos.url>nacos-headless.jn.svc.cluster.local:8848</nacos.url>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>开发环境-本地调试</id>
<properties>
<!--nacos namespace ID-->
<nacos.group.id>fc8373a3-a3b6-4172-ac9b-2d1d17e09413</nacos.group.id>
<nacos.url>localhost:8848</nacos.url>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>