Spring Cloud Alibaba | Nacos配置管理

極客挖掘機發表於2019-07-16

Spring Cloud Alibaba | Nacos配置管理

Springboot: 2.1.6.RELEASE

SpringCloud: Greenwich.SR1

如無特殊說明,本系列文章全採用以上版本

上一篇《Spring Cloud Alibaba | Nacos服務註冊與發現》我們聊了Nacos服務註冊與發現,這一篇我們接著聊Nacos配置管理。

Nacos具有配置管理的功能,在Spring Cloud中可以用作配置中心,代替Spring Cloud Config元件,下面我們聊一下Nacos如何和Spring Cloud整合配置中心。

建立一個專案:nacos-config

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 http://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.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>nacos-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-config</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>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>0.9.0.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>

主要引入spring-cloud-starter-alibaba-nacos-config,為開啟nacos配置中心

更多版本對應關係請參考:《Spring Cloud Alibaba | Nacos服務中心初探》

2. 在 bootstrap.properties 中配置 Nacos server 的地址和應用名

spring.cloud.nacos.config.server-addr=127.0.0.1:8848

spring.application.name=spring-cloud-nacos-config

說明:之所以需要配置 spring.application.name ,是因為它是構成 Nacos 配置管理 dataId欄位的一部分。

在 Nacos Spring Cloud 中,dataId 的完整格式如下:

${prefix}-${spring.profile.active}.${file-extension}
  • prefix 預設為 spring.application.name 的值,也可以通過配置項 spring.cloud.nacos.config.prefix來配置。
  • spring.profile.active 即為當前環境對應的 profile,詳情可以參考 Spring Boot文件。 注意:當 spring.profile.active 為空時,對應的連線符 - 也將不存在,dataId 的拼接格式變成 ${prefix}.${file-extension}
  • file-exetension 為配置內容的資料格式,可以通過配置項 spring.cloud.nacos.config.file-extension 來配置。目前只支援 propertiesyaml 型別。

3. 通過 Spring Cloud 原生註解 @RefreshScope 實現配置自動更新:

package com.springcloud.nacosconfig.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Date: 2019/7/14
 * @Time: 18:13
 * @email: inwsy@hotmail.com
 * Description:
 */
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }
}

4. 測試

首先通過呼叫 Nacos Open APINacos Server 釋出配置:dataId 為example.properties,內容為useLocalCache=true

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=spring-cloud-nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=false"

執行 NacosConfigApplication,呼叫 curl http://localhost:8080/config/get,返回內容是 true

再次呼叫 Nacos Open APINacos server 釋出配置:dataId 為example.properties,內容為useLocalCache=false

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=spring-cloud-nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=true"

再次訪問 http://localhost:8080/config/get,此時返回內容為false,說明程式中的useLocalCache值已經被動態更新了。

至此,Nacos配置管理已經介紹完成。

參考:

https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

Github-示例程式碼

相關文章