上一篇文章讲解了Spring Cloud 整合 nacos 实现服务注册与发现,nacos除了有服务注册与发现的功能,还有提供动态配置服务的功能。本文主要讲解Spring Cloud 整合nacos实现动态配置服务。主要参考官方部署手册点我

前提条件

先下载nacos并启动nacos服务。操作步骤详见Nacos 快速入门

整合步骤

1. 添加依赖

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.12.RELEASE</version>
</dependency>

版本nacos2.1.x.RELEASE 对应的是 Spring Boot 2.1.x 版本。版本 2.0.x.RELEASE 对应的是 Spring Boot 2.0.x 版本,版本 1.5.x.RELEASE 对应的是 Spring Boot 1.5.x 版本。版本不匹配的话,会出现很多莫名其妙的问题。nacos依赖版本要和nacos服务端版本要一致。

2. 新建 nacos 配置

nacos控制台添加配置列表:

设置dataIdnacos-config,文件后缀为Properties,设置内容user.name=jack

3. bootstrap.properties 配置

application.yml同目录下创建bootstrap.yml文件,并配置Nacos服务地址以及namespace(没有就不需要配置):

spring:
application:
name: nacos-config-client
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
namespace: 68468122-8955-45ee-a5b7-3d87972325b1

4. 配置dataId

dataId对应步骤2里面的dataId,有两种配置方式,一种是官方自动构建dataId ,另一种是指定dataId

4.1 自动配置 dataId

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

${prefix}-${spring.profiles.active}.${file-extension}
  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
  • spring.profiles.active 即为当前环境对应的 profile。 注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 propertiesyaml 类型。

比如项目名称为nacos-config-client,当前环境为test,格式文件为properties,那就需要新建一个dataIdnacos-config-client.properties配置。

4.2 手动设置 dataId

NacosConfigProperties类里面name字段就是配置dataId

public class NacosConfigProperties {
/**
* nacos config dataId name.
*/
private String name;
//省略其他配置
}

bootstrap.yml添加spring.cloud.nacos.config.name就可以设置dataId

5.获取数据

通过@Value就能获取配置文件的数据:

@Component
@RefreshScope
public class TestConfig { @Value(value = "${user.name:null}")
private String test; public String getTest(){
return test;
}

要实现配置的自动更新,需要添加Spring Cloud原生注解 @RefreshScopecontroller直接调用即可:

@RestController
public class TestController { @Autowired
private TestConfig testConfig; @GetMapping("/config")
public String testConfig(){
String config = testConfig.getTest();
return config;
}
}

如果想通过@NacosValues注解获取数据,需要引入nacos-config-spring-boot-starter依赖:

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>

总结

  • nacos-config配置首先添加spring-cloud-starter-alibaba-nacos-config依赖。
  • 在配置列表添加配置
  • bootstrap.properties添加nacos server地址和namespace
  • 配置dataId有两种方式
    • 手动配置,配置spring.cloud.nacos.config.name
    • 自动配置,根据${prefix}-${spring.profiles.active}.${file-extension}规则配置,其中prefix为项目名称,spring.profiles.active为项目运行环境,file-extension配置内容的数据格式。
  • 通过@Value(value = "${user.name:null}")设置在字段上就能获取到属性,要实现自动更新配置需要添加@RefreshScope注解。

源码

参考

Spring Cloud 整合 nacos 实现动态配置中心的更多相关文章

  1. Spring Cloud Alibaba 整合 Nacos 实现服务配置中心

    在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...

  2. Spring Cloud第十一篇 | 分布式配置中心高可用

    ​ 本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  3. Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】

    Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-26 |  本文接之前的<Spring Clou ...

  4. Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】

    Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-24 |  Spring Cloud Confi ...

  5. Spring Cloud(九):配置中心(消息总线)【Finchley 版】

    Spring Cloud(九):配置中心(消息总线)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-05-07 |  我们在 Spring Cloud(七):配置中心 ...

  6. Spring Cloud config之一:分布式配置中心入门介绍

    Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...

  7. Spring Cloud Config实现集群配置中心

    Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,可以管理集群中的配置文件.使用Git.SVN等版本管理系统存放配置文件,配置服务器会到版本管理系统获取配置,集群中的配置 ...

  8. Spring Cloud第十篇 | 分布式配置中心Config

    ​ 本文是Spring Cloud专栏的第十篇文章,了解前九篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  9. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

随机推荐

  1. 275. H 指数 II--Leetcode_暴力

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/h-index-ii 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 题目的大意是 ...

  2. Find-Vulnerability 自动化探测扫描工具简介

    Fvuln 简介 F-vuln(全称:Find-Vulnerability)是一款自动化探测扫描工具,主要适用于日常安全服务.渗透测试人员和RedTeam红队人员使用 它集合的功能包括: 存活IP探测 ...

  3. 网安等保-Linux服务器之最新Ubuntu-22.04-LTS系统内核优化与安全加固配置脚本使用分享

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 前言 ...

  4. Filter中的FilterChain.doFilter(req,resp)的报错解决

    服务器内部错误:500 Request processing failed; nested exception is java.lang.IllegalStateException: 提交响应后无法调 ...

  5. C#使用BouncyCastle生成PKCS#12数字证书

    背景 生成数字证书用于PDF文档数字签名 数字证书需要考虑环境兼容性,如linux.windows 网上资料不全或版本多样 本文章主要介绍了在C#中使用BouncyCastle生成PKCS#12个人信 ...

  6. 端口安全 | DHCP snooping

    1.端口安全用于防止mac地址的欺骗.mac地址泛洪攻击.主要思想就是在交换机的端口下通过手工或者自动绑定mac地址,这就就只能是绑定的mac地址能够通过. 2.通过静态的端口绑定:将mac地址手工静 ...

  7. npm 和 maven 使用 Nexus3 私服 | 前后端一起学

    前文<Docker 搭建 Nexus3 私服 >介绍了在 docker 环境下安装 nexus3 以及 nexus3 的基本操作和管理,本文分别介绍 npm(前端)和 maven(后端)如 ...

  8. pathlib路径问题

    下面是我的文件框架 app ------ file1---- .py1 file2---- .py2 config.py 我在config文件中设置了变量参数 BASE_DIR = pathlib.P ...

  9. Linux实例常用内核网络参数介绍与常见问题处理---重要

    文章转载自:https://help.aliyun.com/knowledge_detail/41334.html 本文主要介绍如下几点内容,您可以根据实际需要选择. 查看和修改Linux实例内核参数 ...

  10. MySQL集群搭建(6)-双主+keepalived高可用

    双主 + keepalived 是一个比较简单的 MySQL 高可用架构,适用于中小 MySQL 集群,今天就说说怎么用 keepalived 做 MySQL 的高可用. 1 概述 1.1 keepa ...