spring cloud 集成nacos config discovery最新

廢話不多說直接上代碼

  1. 首先創(chuàng)建一個(gè)spring boot maven項(xiàng)目

  2. 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 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.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>nacosDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>nacosDemo</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <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>
                <version>2.2.3.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.2.3.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    image.gif
  3. 在application.properties文件中添加配置

    spring.application.name=agent
    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    //定義配置文件后綴為.properties
    spring.cloud.nacos.config.file-extension=properties
    //此處為自定義命名空間,下面會(huì)有說明
    spring.cloud.nacos.config.namespace=29de6443-9f76-4c4b-9ff2-f754961365d4
    //此處為自定義分組,下面有說明
    spring.cloud.nacos.config.group=OLD_GROUP
    
    image.gif
  4. 在啟動(dòng)類里面加上

    @EnableDiscoveryClient
    
  5. 在配置類上面添加實(shí)現(xiàn)當(dāng)前類屬性動(dòng)態(tài)更新

    @RefreshScope
    
    package com.example.nacosdemo;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RefreshScope
    @RestController
    @EnableDiscoveryClient
    public class NacosDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosDemoApplication.class, args);
        }
    
        @Value("${redis.host}")
        private String config;
    
        @RequestMapping("/getValue")
        public String getValue() {
            return config;
        }
    }
    
    
    image.gif
  6. 官網(wǎng)下載nacos-server,啟動(dòng)nacos

  7. 創(chuàng)建命名空間
    image
    image.gif
  8. 創(chuàng)建dataid,配填寫配置
    image
    image.gif
  9. 完成以上操作即可順利啟動(dòng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容