spring cloud config客户端
上篇介绍了spring cloud config服务器,本篇介绍客户端。客户端主要是从config服务器获取配置信息。
代码示例
首先创建一个Maven项目,在pom.xml文件中添加依赖:
<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>
增加一个Controller,用于测试请求:
@RestController
public class IndexController { @Value("${profile}")
private String profile; @RequestMapping("/profile")
public String getProfile() {
return profile;
} }
配置文件:bootstrap.yml
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:18083 #更改配置服务器的位置,默认地址是http://localhost:8888
profile: test #对应spring.profiles.active
label: master #分支名。当配置服务器是git时,默认是master
配置文件:application.yml
server:
port:
启动服务,可以看到会请求http://localhost:18083,即从config server获取配置信息。也会打印出environment对象的值,即name(项目名称)、profiles(环境名称)、label(分支名称)。如下所示:

测试工作:
请求路径:http://localhost:18084/profile,测试结果:

如果要获取config-client-dev.yml文件中的内容,只需要改spring.cloud.config.profile: dev就行
配置内容的热加载:
如果不重启应用,能够做到配置的刷新吗?答案显然是可以的。
1、增加actuator依赖,暴露/refresh端点:
<dependency>
<!-- 监控管理(如:状态页和健康指标) -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、在上面的IndexController中增加@RefreshScope注解:
@RestController
@RefreshScope // 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
public class IndexController { @Value("${profile}")
private String profile; @RequestMapping("/profile")
public String getProfile() {
return profile;
} }
3、我们将config-client-test.yml中值改为
profile: abcd
并提交到git仓库
4、使用Postman(也可以使用其它工具)请求:
POST http://localhost:18084/refresh
请求结果是:
[
"config.client.version",
"profile"
]
最后再次访问http://localhost:18084/profile,将会看到返回值是 abcd,说明配置已刷新。配置自动刷新可参考:使用spring cloud bus自动刷新配置
遇到的坑:如果spring cloud使用的版本是 Edgware 以及之前,需要关掉actuator安全,把 manage.security.enabled 设为false
总结
1、spring cloud配置加载顺序: 加载bootstrap.* 配置 --> 连接config-server,加载远程配置 --> 加载application.* 配置
2、远程配置优先级高于本地配置,application.*会覆盖bootstrap.*中的配置
3、如果在远程配置中找不到匹配配置文件,则默认找远程的application.*文件
4、如果config客户端不配置项目名称,则会使用默认的项目名称(即application),也会查找远程的application.*文件
5、一般将不太会改变的配置放在bootstrap.*文件中,如spring.application.name
spring cloud config客户端的更多相关文章
- Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- Spring Cloud Config(配置中心)
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.简介 Spring Cloud Config为分布式系统中的外部配置提供服务器和客 ...
- Spring Cloud Config中文文档
https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...
- Spring Cloud Config 自动刷新所有节点
全局刷新 详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p160-9.9.2节 1.使用Spring Cloud Config 客户端时,可以使用 /refresh ...
- spring cloud config配置
参考: http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html http://www.ityouknow ...
- Spring Cloud Config 实现配置中心,看这一篇就够了
Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...
- Spring Cloud官方文档中文版-Spring Cloud Config(上)-服务端(配置中心)
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...
- Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...
随机推荐
- The current state of generics in Delphi( 转载)
The current state of generics in Delphi To avoid duplication of generated code, the compiler build ...
- js中push和pop的用法
push: 将新元素追加到一个数组中,并返回新的数组长度: 语法:arrayObj.push([item1 [item2 [. . . [itemN ]]]]) var number; var my_ ...
- MySQL 查看修改字符集
查看MYSQL数据库服务器和数据库字符集 方法一:show variables like '%character%'; 方法二:show variables like 'collation%'; sh ...
- Python学习第2章
1.字符串: python中创建字符串我们可以使用引号''或"'. python访问子字符串,可以使用方括号来截取字符串: var="hello world!" var2 ...
- MFC 多窗口通信时,使用RadioButton和Button时冲突问题
最近项目需要我们实现在两个窗口间进行通信,其中有个小功能如图所示: 当我点击GDIProgram中的Button1时,会更新Dialog的Radio1和Radio2的状态. Dialog中的Radio ...
- PHP 生成验证码(+图片没有显示的解决办法)
今天有需要用到验证码,就敲了个,毕竟用途比较广,所以打算把代码留下来,以后肯定用得上的.当然,今天在做的时候也是有一些问题的,分享出来吧,记录自己所犯的错误,避免以后再掉坑里. 先给个效果图(下面的真 ...
- Windows UDP sockets: recvfrom() fails with error 10054
https://stackoverflow.com/questions/34242622/windows-udp-sockets-recvfrom-fails-with-error-10054 #in ...
- Javascript高级编程学习笔记(10)—— 作用域、作用域链
昨天介绍了,JS中函数的作用域 什么词法环境之类的,可能很多小伙伴不太明白. 在今天的内容开始之前,先做个简短的声明: 词法环境这一概念是在ES5中提出的,因为词法环境主要用于保存let.const声 ...
- Java学习笔记43(打印流、IO流工具类简单介绍)
打印流: 有两个类:PrintStream,PrintWriter类,两个类的方法一致,区别在于构造器 PrintStream:构造方法:接收File类型,接收字符串文件名,接收字节输出流(Outpu ...
- Linux链接脚本学习--lds
一.概论 ld: GNU的链接器. 用来把一定量的目标文件跟档案文件链接在一起,并重新定位它们的数据,链接符号引用. 一般编译一个程序时,最后一步就是运行ld进行链接 每一个链接都被一个链接脚本所控制 ...