1、创建一个空项目

2、创建一个模块

3、创建nacos的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>com.lsh</groupId>
    <artifactId>nacos</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>nacos-config</module>
    </modules>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.boot.starter.web.version>3.3.5</spring.boot.starter.web.version>
        <nacos.config.version>2023.0.1.0</nacos.config.version>
        <nacos.discovery.version>2023.0.1.0</nacos.discovery.version>
        <bootstrap.version>3.1.0</bootstrap.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot Web Starter: 提供构建Web应用所需的基本功能。 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.starter.web.version}</version>
            </dependency>
            <!-- Nacos Config Starter: 用于从Nacos获取配置,支持动态配置管理和配置的热更新。 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>${nacos.config.version}</version>
            </dependency>
            <!-- Nacos Discovery Starter: 提供服务发现功能,允许应用通过Nacos注册和发现服务。 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>${nacos.discovery.version}</version>
            </dependency>
            <!-- bootstrap.yml配置 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bootstrap</artifactId>
                <version>${bootstrap.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>3.3.3</version>
    </parent>
</project>

创建nacos_config的配置文件:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lsh</groupId>
        <artifactId>nacos</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>nacos-config</artifactId>

    <properties>
        <java.version>21</java.version>
    </properties>

    <dependencies>
        <!-- web服务 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 配置中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- bootstrap.yml配置 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4、增加配置文件:application.yml

server:
  port: 5000
  servlet:
    context-path: /grey
bootstrap.yml
spring:
  application:
    name: nacos-test
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        group: DEFAULT_GROUP
        file-extension: yaml
  config:
    import:
      - optional:nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

5、删除test并创建一个UserController

package 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
    @Value("${user.username}")
    private String username;
    @Value("${user.age}")
    private Integer age;
    @Value("${user.id}")
    private String id;
    @Value("${user.sex}")
    private Integer sex;
    @GetMapping("/hsm")
    public String test(){
        return "id:"+id+",name:"+username+",age:"+age+",sex:"+sex;
    }
}

6、项目结构图

7、运行和测试

更多推荐