欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Cloud Alibaba | Nacos配置管理

程序员文章站 2023-04-05 08:17:34
Spring Cloud Alibaba | Nacos配置管理 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特殊说明,本系列文章全采用以上版本 [TOC] 上一篇 "《Spring Cloud Alibaba | Nacos服务注册 ......

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配置管理已经介绍完成。