• nocos配置中心使用教程(NACOS 1.X版本)


    1.下载和安装

    进入到官网下载就好了
    在这里插入图片描述
    解压
    在这里插入图片描述
    启动
    在这里插入图片描述

    2.新建cloudalibaba-config-nacos-client3377

    2.1 pom.xml

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>cloud2020artifactId>
            <groupId>com.cao.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>cloudalibaba-config-nacos-client3377artifactId>
    
        <dependencies>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    2.2 两个yml文件

    2.2.1 bootstrap.yml

    # nacos配置
    server:
      port: 3377
    
    spring:
      application:
        name: nacos-config-client
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #Nacos服务注册中心地址
          config:
            server-addr: localhost:8848 #Nacos作为配置中心地址
            file-extension: yaml #指定yaml格式的配置
    #官网地址:https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
    #Nacos同springcloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。
    #springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application
    #公式:${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
    # 在这里就是:nacos-config-client-dev.yaml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.2.2 application.yml

    spring:
      profiles:
        active: dev # 表示开发环境
    
    • 1
    • 2
    • 3

    2.3 主启动类

    package com.cao.springcloud.alibaba;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @auther caowenbin
     * @create 2023-10-17 21:07
     */
    @EnableDiscoveryClient
    @SpringBootApplication
    public class NacosConfigClientMain3377 {
    	public static void main(String[] args) {
    		SpringApplication.run(NacosConfigClientMain3377.class, args);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.4 Controller

    package com.cao.springcloud.alibaba.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @auther caowenbin
     * @create 2023-10-17 21:09
     */
    @RestController
    @RefreshScope //在控制器类加入@RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能。
    public class ConfigClientController
    {
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/config/info")
        public String getConfigInfo() {
            return configInfo;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3.配置新增

    3.1 官网文档说明及地址

    根据官网的规则进行设置自己的Data ID
    官网文档地址:https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
    在这里插入图片描述

    3.2 配置自己的Data Id和内容

    Data Id要求一定要根据官网的要求和规则
    在这里插入图片描述

    在这里插入图片描述
    设置自己的配置内容

    config: 
        info: this is nacos config of dev
    
    • 1
    • 2

    在这里插入图片描述

    4.测试

    4.1 测试是否读取到nacos中的配置内容

    启动当前模块,进行测试访问,看是对否能够读取到nacos配置的内容config.info

    在这里插入图片描述

    4.2 测试动态刷新

    修改nacos中的内容,发布,直接刷新网页,访问Controller,
    在这里插入图片描述

    看到内容已经动态获取到了
    在这里插入图片描述

    5.通过spring.profile.active属性就能进行多环境下配置文件的读取

    5.1 新增一个配置文件nacos-config-client-test.yaml

    config: 
        info: this is nacos config of test,Refresh Test
    
    • 1
    • 2

    在这里插入图片描述

    5.2 修改自己的代码

    在这里插入图片描述

    5.3 测试

    在这里插入图片描述

    6.Nacos之Group分组方案

    6.1 新增两个组和对应的data Id

    DEV_GROUP新增dataID:nacos-config-client-info.yaml
    在这里插入图片描述
    TEST_GROUP新增dataID:nacos-config-client-info.yaml
    在这里插入图片描述
    最终的,同名的两个文件,但是在不同的GROUP,怎样获取配置文件中的内容
    在这里插入图片描述

    6.2 代码改写,重启项目

    在config下增加一条group的配置即可。可配置为DEV_GROUP或TEST_GROUP

    6.2.1 TEST_GROUP

    在这里插入图片描述

    6.2.2 DEV_GROUP

    在这里插入图片描述

    6.3 测试

    6.3.1 TEST_GROUP

    访问的是TEST_GROUP组中的nacos-config-client-info.yaml文件
    在这里插入图片描述

    6.3.2 DEV_GROUP

    访问的是DEV_GROUP组中的nacos-config-client-info.yaml文件
    在这里插入图片描述

    7.Nacos之Namespace空间方案

    7.1 新建两个命名空间:dev和sit

    在这里插入图片描述
    回到服务管理-服务列表查看
    在这里插入图片描述

    7.2 在namespace为dev的下面新加三个配置文件

    在这里插入图片描述

    7.2.1 dev文件配置

    在这里插入图片描述

    7.2.2 sit文件配置

    在这里插入图片描述

    7.2.3 default文件配置

    在这里插入图片描述

    7.3 编写配置

    在这里插入图片描述
    根据namespace+group+dataId确定了读取的是哪一个文件中的内容

    7.4 重启测试

    读取的是:namespace为dev,group为DEV_GROUP,dataId为nacos-config-client-dev.yaml中的内容
    在这里插入图片描述

  • 相关阅读:
    FreeRTOS 内存管理策略
    Centos7安装Nginx
    8051开发实例-实现红外寻迹小车
    商品分类代码
    CommandInvokationFailure: Failed to update Android SDK package list. 报错的解决方法
    Scala Iterator(迭代器)
    使用Python编写一个多线程的12306抢票程序
    RK3588 android12删除launcher3无法使用“近期任务“解决办法
    Dnsmasq的使用
    常用黄芪泡水喝,身体能得到什么?学会搭配,养生效果或会翻倍
  • 原文地址:https://blog.csdn.net/weixin_44137201/article/details/133894018