nacos学习笔记(一)

7 篇文章 0 订阅
4 篇文章 0 订阅

nacos学习笔记

1.概览

官网地址:https://nacos.io/zh-cn/docs/what-is-nacos.html

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

2.使用

1. 踩坑纪录(版本)

有时候版本不一致,会导致服务注册不成功

Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2.2.3.RELEASE1.8.01.3.34.4.02.7.81.3.0
2.2.2.RELEASE1.8.01.3.24.4.02.7.81.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.0
2.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE1.7.01.1.44.4.02.7.30.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE1.6.31.1.14.4.02.7.30.7.1
Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot Version
Spring Cloud Hoxton.SR82.2.3.RELEASE2.3.2.RELEASE
Spring Cloud Hoxton.SR82.2.2.RELEASE2.3.2.RELEASE
Spring Cloud Hoxton.SR32.2.1.RELEASE2.2.5.RELEASE
Spring Cloud Hoxton.RELEASE2.2.0.RELEASE2.2.X.RELEASE
Spring Cloud Greenwich2.1.2.RELEASE2.1.X.RELEASE
Spring Cloud Finchley2.0.2.RELEASE2.0.X.RELEASE
Spring Cloud Edgware1.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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值