1.Spring Boot 整合 Nacos 配置中心

本次使用版本
boot: 2.3.11.RELEASE ;nacos-starter: 0.2.7
普通 SpringBoot 项目配置项引入方式主要有
@Value(“${key}”)
@ConfigurationProperties(prefix = “prefix”)

使用Nacos作为配置中心后,相应的替换为
@NacosValue(value = “${key}”,autoRefreshed = true)
@NacosConfigurationProperties(dataId = “config-test”, prefix = “prefix”, autoRefreshed = true)
nacos整合SpringBoot项目,这里不赘述,直接见官方文档
注意以下几点
1.使用 @NacosValue 载入属性时,要实现动态刷新,需在 @NacosPropertySource和@NacosValue 同时加上autoRefreshed = true
2.使用了@NacosConfigurationPropertie载入属性时,只需要在@NacosConfigurationPropertie上加上autoRefreshed = true就可以
3.由于属性文件使用的是@NacosConfigurationPropertie,相应的@EnableConfigurationProperties就无法将其加载到Spring上下文,所以需要在加上@Component
3.dataId 有点需要注意,如果没有显式指定后缀名,它默认是使用的properties,意味着下面的格式只能选择properties,否则会导致意想不到的异常;同样的,想要配置成yaml格式的配置文件,dataId需要手动指定后缀名 xxxx.yaml ,同时代码里面也一样,否则也会有问题。不知 后续会不会在框架层面进行自动匹配
在这里插入图片描述

2.Spring Cloud 整合 Nacos 配置中心

基本使用

Spring Cloud 使用起来就更简单了,整合步骤 见链接: 官方文档.
这里只说需要注意的点:
1.创建配置文件时,Data ID的后缀需要和配置格式保持一致
代码默认为 properties;代码在引用时需要和Data ID保持一致
与Spring Boot 不同点在于,这里的Data ID 必须指定对应的后缀
2.Data ID 命名规则
${prefix}-${spring.profiles.active}.${file-extension}
${prefix}默认为 ${spring.application.name}

自动刷新

  • 主配置(spring.cloud.nacos.config)默认开启刷新。
  • 共享配置和扩展配置(spring.cloud.nacos.config.shared-configsspring.cloud.nacos.config.extension-configs)默认未开启刷新。

在开启自动前提下,根据以下规则刷新配置:

  • @ConfigurationProperties
  • @Value + @RefreshScope
GitHub 加速计划 / na / nacos
29.84 K
12.75 K
下载
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
最近提交(Master分支:3 个月前 )
28548c13 * Improve the retrieval of ConfigInfoState to facilitate the extension and implementation of databases like Oracle. * Add unit tests for the SQL construction part of the `ExternalConfigInfoPersistServiceImpl#findConfigInfoState` method. * Enhance the construction of the delete statement in AbstractMapper by using a unified appendWhereClause method to construct the WHERE clause. Modify appendWhereClause to be protected, allowing for customization based on different database types, such as adjustments according to column names. 11 小时前
49611915 * The error message is not user-friendly when adding duplicate permissions. (#12773) * The error message is not user-friendly when adding duplicate permissions.(#12773) 11 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐