SpringCloud項目中使用Nacos作為配置中心


參考:https://blog.csdn.net/qq_33619378/article/details/96991237

Nacos-server啟動

這里就不說了

新建配置

在Nacos-Server中新建配置,其中Data ID它的定義規則是:${prefix}-${spring.profile.active}.${file-extension}

prefix 默認為 spring.application.name 的值,也可以通過配置項 spring.cloud.nacos.config.prefix 來配置。
spring.profile.active 即為當前環境對應的 profile,可以通過配置項 spring.profile.active 來配置。
file-exetension 為配置內容的數據格式,可以通過配置項 spring.cloud.nacos.config.file-extension 來配置。目前只支持 properties 和 yaml 類型。
注意:當 spring.profile.active 為空時,對應的連接符 - 也將不存在,dataId 的拼接格式變成 prefix.{prefix}.prefix.{file-extension}

這里我創建Data Id 為nacos-config.yml的配置文件,其中Group為默認的DEFAULT_GROUP,配置文件的格式也相應的選擇yaml,其內添加配置nacos.config=hello_nacos,如圖所示

 創建應用

1,在聚合工程Nacos下新建Module,創建一個名為nacos-config的子工程,其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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>nacos</artifactId>
        <groupId>com.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-config</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

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

</project>

2,創建配置文件名為bootstrap.yml,注意是bootstrap.xxx,而不是application或者其他。原因如

Nacos同springcloud-config一樣,在項目初始化時,要保證先從配置中心進行配置拉取,拉取配置之后,才能保證項目的正常啟動。springboot中配置文件的加載是存在優先級順序的,bootstrap優先級高於application

 PS:項目啟動的時候先加載 bootstrap.yml,然后再加載application.yml,所以在使用配置中心的時候,項目中使用bootstrap.yml,在其中配置一些固定不變的信息,在配置中心配置可變信息

這里的配置文件類型可以根據個人習慣選擇,我這里用的時yml類型,配置內容如下

spring:
  application:
    name: nacos-config
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yml

  可以看到必須可少的配置項spring.application.name
  spring.cloud.nacos.discovery.server-addr指定注冊中心的地址,如果你不需要注冊該服務,也可以去掉該項,並刪除discovery依賴
  spring.cloud.nacos.config.server-addr指定配置中心的地址
  file-extension指定配置中心中配置文件的格式

上面的配置是為了保證服務的正常注冊和配置獲取,以及配置DataID的正確性

3,在啟動類上增加@EnableDiscoveryClient注解

package com.example.nacosconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}

4,創建對外接口

package com.example.nacosconfig.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;

/**
 * @Description
 * @Author clj
 * @Date 2019/12/10 14:18
 */
@RestController
@RefreshScope
public class ConfigController {

    @Value("${nacos.config}")
    private String config;

    @GetMapping("/helloConfig")
    public String helloConfig(){
        return config;
    }

}

  其中通過@Value注解,去讀取key為nacosconfig的配置的值,並通過/getValue接口返回。
  加入@RefreshScope注解,可以使當前類下的配置支持動態更新。

到此代碼部分的工作已經完成

啟動測試

1,保證Nacos-server已經啟動

2,檢查配置文件是否已經添加

3,啟動nacos-config項目,默認是8080端口

啟動成功后在Nacos控制台中可以看到我們注冊的服務

 

 此時調用接口進行測試 http://localhost:8080/helloConfig,可以看到返回結果

 

 此時說明已經成功讀取到配置,下面我將Nacos-Server上的配置修改為hello_lars,看看能否動態更新

 

 此時調用接口進行測試 http://localhost:8080/helloConfig,可以看到返回結果

可以看到我通過Nacos-server的控制台進行配置的修改,客戶端服務nacos-config也相應的進行熱更新。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM