Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. [1]

简单来说,Spring Cloud Config在分布式环境下提供了一种动态获取配置文件的一种方式。

它可以用来管理整个环境中的配置文件

我们这里拿Git作为演示。

1、简单搭建

1)、创建配置中心,代码如下:

pom文件:

<

bootstrap.yml:

server:#当前Config服务的端口号配置port: 65010
spring:application:name: configservercloud:config:server:git:uri: https://github.com/wuyusrc/DemoTestsearch-paths: configtestusername: usernamepassword: password

其中:url是指git地址,search-paths是指匹配查询的路径名,username是git上的用户名,password是指git上的密码。

启动程序增加EnableConfigServer注解:

在github上,我们上传几个测试文件

启动配置中心服务,测试一下。

Config支持请求的参数格式为:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties[1]

其中application为应用名,对应测试例子中的ctest,profile为环境名,对应测试例子中的dev,label为分支名,分支名默认是master。

2)、创建Config Client,代码如下:

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

bootstrap.yml文件:

server:port: 65011
spring:application:#本程序的服务名name: configclientcloud:config:#配置中心的URLuri: http://localhost:65010/#指定的环境名profile: dev#指定的分支名#label: master#文件前缀名称name: ctest

创建一个Controller对外提供服务:

@RestController
@RefreshScope
@RequestMapping("clientconfig")
public class ClientConfigController {@Value("${from}")private String fromString;@GetMapping("/from")public String getEv() {return fromString;}
}

启动Config Client测试一下吧。

可以看出来,client已经成功获取到配置文件了。

2、高可用

1)、改造一下配置中心

pom文件:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

bootstrap.yml:

server:#当前Config服务的端口号配置port: 65010
spring:application:name: configservercloud:config:server:git:uri: https://github.com/plsjava/plstestsearch-paths: testplsconfigusername: usernamepassword: password
eureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/

启动文件增加EnableEurekaClient注解和EnableConfigServer注解。

2)、改造一下Config Client。

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

bootstrap.yml:

spring:application:#对应配置中心所获取的配置文件的{application}name: ctestcloud:config:#配置中心的URL#uri: http://localhost:65010/#指定的环境名profile: dev#指定的分支名#label: master#文件前缀名称#name: ctestdiscovery:enabled: trueservice-id: configserver
eureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/

application.yml:

server:port: 65012

Controller:

@EnableEurekaClient
@RestController
@RefreshScope
@RequestMapping("clientconfig")
public class ClientConfigController {@Value("${from}")private String fromString;@GetMapping("/from")public String getEv() {return fromString;}
}

启动注册中心,配置文件和Config Client。

注册中心显示如下:

请求服务显示如下:

可以看出来,已经成功获取了配置文件。

架构图应该是这样:

好了,Config这样就可以搭建起来了。

参考

  1. ^abSpring Cloud Config(官方文档) https://cloud.spring.io/spring-cloud-config/reference/html/

spring cloud config动态刷新_Spring Cloud学习笔记--配置中心(Config)相关推荐

  1. Spring Cloud构建微服务架构:分布式配置中心【Dalston版】

    Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端也称为 ...

  2. Spring Boot进阶之Web进阶 学习笔记

    前言 昨天 -> 带女朋友和小表弟去了动物园,看了<全球风暴>电影. 今天 -> 学习了慕课网的Spring Boot进阶之Web进阶的视频和该项目 项目源码,看了一个基于Sp ...

  3. 分布式配置中心-Config

    文章目录 一.常规的配置设置方式 1.传统配置管理的缺点 二.分布式配置中心--Config 1.Config的配置文件命名规则 2.config小demo (1)前期准备 1)创建一个git项目 2 ...

  4. iframe的src动态修改并刷新_微服务中配置中心Config+消息总线Bus,实现分布式自动刷新配置

    技术/杨33 一.分布式配置中心Config 一套集中的.动态的配置管理,实现统一配置微服务中的每个子服务. Spring Cloud Config为微服务架构提供了集中化的外部配置支持,配置服务器为 ...

  5. pom文件配置多个远程仓库地址_分布式配置中心Config

    一 配置中心认识 1 服务配置的现状 2 常用的配置管理解决方案缺点 a.硬编码[缺点:需要修改代码,繁琐.风险大] b.写在properties里面[缺点:在集群环境下,需要替换和重启] c.写在x ...

  6. springcloud 分布式配置中心 config server config client

    ---------------------------------------------------------------------------------------------------- ...

  7. 搭建SpringCloud配置中心 Config Server

    项目地址: 链接:https://pan.baidu.com/s/1Mxo0ltvZbpz_r8mCU-mSpw  提取码:3j4a 问题解答: SpringCloud Config 分布式配置文件中 ...

  8. 将Spring Security OAuth2授权服务JWK与Consul 配置中心结合使用

    将Spring Security OAuth2授权服务JWK与Consul 配置中心结合使用 概述 在前文中介绍了OAuth2授权服务简单的实现密钥轮换,与其不同,本文将通过Consul实现我们的目的 ...

  9. spring cloud 2020.0.3 学习记录(四)配置中心config以及bus实现自动配置更新

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 目录 前言 一.依赖 二.config 1.server端 2.client端 三.cloud bus实现配置自动更新 加入依赖 更新 ...

最新文章

  1. 使用独立主机时需要关注的问题
  2. 数据库面试题目经典大全
  3. mybatis example处理and、or关系的方法
  4. css里面的let,js中let和var定义变量的区别
  5. 2020网络数据平面峰会-无损网络,真的无损?-杨益锋
  6. MYSQL集群的备份与恢复最终版本
  7. 数据库触发器调用python_python批量删除数据库触发器 | 学步园
  8. 【原创】自己动手写控件----XSmartNote控件
  9. Introduction to Computer Networking学习笔记(二十七):BitTorrent
  10. 如何在windows上 安装更新 显卡的驱动
  11. python自动打印如何实现-Python 实现自动完成A4标签排版打印功能
  12. Android netd 简单分析和配置iptable
  13. java 过载_java语言中基础类的覆盖与过载
  14. WTS 2.1.18124.1 彻底抛弃了 15063(Win 10 创意者更新)
  15. HAU2022冬训营字符串1
  16. 《C Primer Plus》读后感
  17. positio有哪些属性?position的属性如何使用
  18. java dump内存泄露分析方法
  19. 洗牌问题(C语言实现)
  20. 最小二乘法的原理与计算

热门文章

  1. Spring Cloud中关于@EnableFeignClients注解的属性字段basePackages
  2. Opatch java 路径_Windows平台下opatch apply报错:OUI-67073
  3. 高效能人士的七个习惯_有史以来最具影响力管理类书籍 高效能人士的七个习惯 让你成为一名成功高效的人士...
  4. h3c trunk口改access_H3CNE配置VLAN的Access链路端口和Trunk链路端口
  5. ideadebug热更新_Spring Boot 在IDEA中debug时的hot deployment(热部署)
  6. 的udp的接收端如何看速率_计算机网络 TCP与UDP
  7. 一支python教学_第一只python爬虫
  8. apache shiro怎么升级_Spring Boot 整合 Shiro ,两种方式全总结!
  9. Java队列Queue的使用
  10. Win10错误代码0x80070541是怎么回事