• SpringCloudAlibaba【三】SpringBoot整合Nacos Config


    背景

    前面,我们将网店的微服务注册到了Nacos,并实现了服务的注册与集群搭建,下面我们来看看SpringBoot如何整合Nacos Config的

    环境

    Windows10
    JDK1.8
    IDEA2021
    Maven2.6
    
    • 1
    • 2
    • 3
    • 4

    下载安装Nacos

    Windows安装Nacos

    搭建Maven项目

    IDEA中创建一个新的Maven工程,项目目录如下

    在这里插入图片描述

    项目文件配置

    POM

    <?xml version="1.0" encoding="UTF-8"?>
    <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">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>nacos-config</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <!--add-->
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.2.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--nacos-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.5.RELEASE</version>
                    <scope>import</scope>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    </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

    YML

    spring:
      application:
        name: nacos-config
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yml
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    NacosConfigApplication

    package com.nacos;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class NacosConfigApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosConfigApplication.class);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    NacosConfigController

    package com.nacos.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;
    
    @RestController
    @RefreshScope
    public class NacosConfigController {
    
        @Value("${str}")
        private String str;
    
        @GetMapping("/")
        public String getConfig() {
            return str;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    测试Nacos动态配置

    启动服务,查看Nacos

    在这里插入图片描述

    注:spring.application.name要和Data Id一致

    编辑配置文件

    在这里插入图片描述

    浏览器输入接口地址

    在这里插入图片描述

    修改配置文件,不重启服务,即可动态获取配置信息,自测

  • 相关阅读:
    (其他) 剑指 Offer 67. 把字符串转换成整数 ——【Leetcode每日一题】
    Typescript基本类型---上篇
    阅读 | 001《人工智能导论》(二)知识获取篇
    电脑D盘格式化会有什么影响?电脑D盘格式化了怎么恢复数据
    k8s教程(13)-pod定向调度
    【Java】SPI在Java中的实现与应用
    MD5退出历史舞台你知道吗?
    不使用脚手架构建vue项目
    flutter问题汇总
    12 Service:微服务架构的应对之道
  • 原文地址:https://blog.csdn.net/weixin_41405524/article/details/127438497