转载请注明作者及出处:
作者:银河架构师
Nacos Config
官网介绍
Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。
Spring Cloud Alibaba Nacos Config 是 Config Server 和 Client 的替代方案,客户端和服务器上的概念与 Spring Environment 和 PropertySource 有着一致的抽象,在特殊的 bootstrap 阶段,配置被加载到 Spring 环境中。当应用程序通过部署管道从开发到测试再到生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。
更详细的说明,请参看:
Nacos Config Wiki:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config
启动Nacos Server
由于本系列微服务框架服务注册与发现使用的是Nacos,所以,在启动微服务之前,均需启动Nacos Server,后续不在赘述。具体启动方式,可参考Spring Cloud进阶之路 | 一:服务注册与发现(nacos)。
创建配置客户端
使用前一篇文章的商品工程xmall-product作为配置客户端。
新建ApplicationProperties
package com.luas.xmall.configuration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "system.application") public class ApplicationProperties { private String name; private String version; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } }
新建ApplicationController,注入ApplicationProperties,并展示。
package com.luas.xmall.controller; import com.luas.xmall.configuration.ApplicationProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/application") public class ApplicationController { @Autowired private ApplicationProperties applicationProperties; @RequestMapping public ApplicationProperties info() { return applicationProperties; } }
启动xmall-product工程,端口为8080。
系统已正常启动。
访问http://localhost:8080/application,结果为空。
添加配置文件
打开nacos控制台,依次点击:配置管理->配置列表,进入配置列表界面。
新增配置
点击右侧+,新建配置。
关于DataId,官网的解释如下:
Nacos 中的某个配置集的 ID。配置集 ID 是组织划分配置的维度之一。Data ID 通常用于组织划分系统的配置集。一个系统或者应用可以包含多个配置集,每个配置集都可以被一个有意义的名称标识。Data ID 通常采用类 Java 包(如 com.taobao.tc.refund.log.level)的命名规则保证全局唯一性。此命名规则非强制。
在 Nacos Spring Cloud 中,DataId的完整格式如下:
- 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 来配置。目前只支持 properties 和 yaml 类型。
关于配置格式,通俗的讲,即为file-exetension。如配置格式选择yaml格式,则DataId后缀必须为yaml(注意,也可写为yml,与yaml同义,但与bootstrap配置中的file-extension必须一致),配置内容也必须按照yml格式编写。
本文以yml为例,后续会花专门的篇幅,来详细介绍Nacos Config用法。
编写完成后,点击发布,此时,已启动服务的控制台已经接收到了Nacos Server的配置。
访问http://localhost:8080/application,结果即为刚才配置的内容。
动态刷新
编辑xmall-product.yml配置,修改version为1.1,然后发布。
查看xmall-product控制台,已然接收到配置更改。
访问http://localhost:8080/application,结果已为适才修改的内容。
RefreshScope
也可以通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新。
新建ApplicationRefreshScopeController,通过原生注解实现配置自动刷新。
package com.luas.xmall.controller; import cn.hutool.core.map.MapUtil; 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; @RestController @RequestMapping("/application/refreshScope") @RefreshScope public class ApplicationRefreshScopeController { @Value("${system.application.name}") private String name; @Value("${system.application.version}") private String version; @RequestMapping public Object info() { return MapUtil.builder().put("name", name).put("version", version).build(); } }
访问http://localhost:8080/application,结果依然是适才修改的内容。
源码
github
https://github.com/liuminglei/SpringCloudLearning/tree/master/05/
gitee
https://gitee.com/xbd521/SpringCloudLearning/tree/master/05/
正文完!
微信搜索【银河架构师】,发现更多精彩。