上篇介绍了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客户端的更多相关文章

  1. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  2. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  3. Spring Cloud Config(配置中心)

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.简介 Spring Cloud Config为分布式系统中的外部配置提供服务器和客 ...

  4. Spring Cloud Config中文文档

    https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...

  5. Spring Cloud Config 自动刷新所有节点

    全局刷新 详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p160-9.9.2节 1.使用Spring Cloud Config 客户端时,可以使用 /refresh ...

  6. spring cloud config配置

    参考: http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html http://www.ityouknow ...

  7. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

  8. Spring Cloud官方文档中文版-Spring Cloud Config(上)-服务端(配置中心)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  9. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

随机推荐

  1. xbee/xbeeRPOS1、xbee/xbeePROS2C802.15.4/Digimesh功能方法

    Digi XBee 802.15.4的第一个版本也称为S1,是基于Freescale的无线收发器片子设计的.最新的802.15.4模块(内部称号S1B)采用和Digi ZigBee模块相同SOC芯片设 ...

  2. 曙光服务器挂载EMC存储

    1.登录集群(用户名密码远程登录,然后切换到root用户) 2.连接主机:ssh node72 3.在主机下进行存储挂载: 1)fdisk -l 查看磁盘信息,如下图所示: 2)查看磁盘挂载信息:mo ...

  3. MySQL insert语句锁分析

    最近对insert的锁操作比较费解,所以自己动手,一看究竟.主要是通过一下三个sql来看一下执行中的sql的到底使用了什么锁. select * from information_schema.INN ...

  4. 23.HashMap

    HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...

  5. day11_雷神_udp、多进程等

    day11 1.网络编程 1.1 udp协议 client端 import json import socket server_addr = ('127.0.0.1',9090) sk = socke ...

  6. shell脚本基础教程

    一.什么是shell: shell解释:引用别人的话说:“Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言.” 简而言之, ...

  7. 使用TheFolderSpy监控文件夹的变化-邮件通知

    一.概述 当我们的文档或者代码文件发布在公网.共享文件夹中,其他用户具备访问或修改的权限时,就存在文档被覆盖或删除的分享.另外一个典型的场景,发布在Web服务器上的网页文件,在网站版本不更新的时间,服 ...

  8. Awake()跟Start()差在哪?

    刚开始学Unity的时候,最难搞定的就是这两个functions的差异,依照官方文件所描述的: Awake(): Awake is called when the script instance is ...

  9. MongoDB 用法入门(windows)①

    概述 大家对数据库肯定不陌生,肯定也有很多人用过MySQL,但是在用MySQL的时候各种建表,写表之间的关联让人非常头疼. MongoDB也是一种数据库,但是它不是用表,而是用集合来装数据的,我对这种 ...

  10. mysql数据表修复

    当数据库表被破坏,运行报错: Table './database/tablename' is marked as crashed and last (automatic?) repair failed ...