美文网首页GISer在Java开发路上的摸爬滚打
【保姆级】Spring Boot如何使用Nacos配置参数

【保姆级】Spring Boot如何使用Nacos配置参数

作者: OQOooo | 来源:发表于2022-06-10 14:05 被阅读0次

    超级详细的保姆级配置教程>>>多图预警 ( ´ ▽ ` )ノ

    新建SpringBoot项目

    image.png

    生成如下目录结构:


    image.png

    1.添加pom.xml依赖

    <?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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.aqin</groupId>
        <artifactId>my-simulator</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>my-simulator</name>
        <description>my-simulator</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR9</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.6.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--nacos config-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
                <version>2.2.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-context</artifactId>
                <version>2.2.2.RELEASE</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    2. 在application.properties里配置端口号

    image.png

    3. 新建bootstrap.properties配置Nacos信息

    image.png

    4. 新建控制类并添加依赖和代码

    image.png

    连接Nacos(前提是安装过了哈~安装很简单可自行🔍)

    1. 启动Nacos

    打开命令行,进入到你电脑的Nacos的bin目录下,我这里是:/Users/aqin1012/Java/nacos/bin

    image.png

    调用sh startup.sh -m standalone启动单个Nacos

    image.png

    2. 打开Nacos的配置页面(http://127.0.0.1:8848/nacos/

    image.png

    新建配置文件:

    image.png

    填写配置参数:

    image.png

    启动项目并测试

    使用Postman进行简单的本地测试(当然用别的工具也都行~~有比postman好用的工具欢迎评论区戳俺👇~~)

    (1) 直接发送请求(localhost:8085/aqin/testNacosConfig/

    image.png

    (2) 修改Nacos的配置参数并发布

    image.png

    (3) 不重启服务,再次发送请求

    image.png

    搞定,撒花🎉🎉🎉~~

    相关文章

      网友评论

        本文标题:【保姆级】Spring Boot如何使用Nacos配置参数

        本文链接:https://www.haomeiwen.com/subject/xphbmrtx.html