SpringCloud系列——Config 配置中心
前言
Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持。有了配置服务器,您就有了一个中心位置来管理跨所有环境的应用程序的外部属性。本文记录实现一个配置中心、客户端获取配置参数、refresh手动刷新
官方文档:https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html
帮助文档:https://spring.io/guides/gs/centralized-configuration/
Config Server
首先我们基于之前的代码,在springCloud工程下面新建一个Config Server,是一个springboot项目,并且在Eureka上面注册服务(还不会服务注册与发现的,请戳:SpringCloud系列——Eureka 服务注册与发现),本例使用的是GitHub

maven引jar
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
登录GitHub,新建一个public仓库:config-server,并且添加测试项目对应的配置文件:myspringboot-dev.properties,并设置几个值

配置文件
server.port=
spring.application.name=config-server
eureka.client.serviceUrl.defaultZone=http://localhost:/eureka/
#健康检查(需要spring-boot-starter-actuator依赖)
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=
# 续约到期时间(默认90秒)
eureka.instance.lease-expiration-duration-in-seconds= #连接GitHub
spring.cloud.config.server.git.uri=https://github.com/huanzi-qch/config-server.git
spring.cloud.config.server.git.search-paths=config-server
spring.cloud.config.label=master
spring.cloud.config.server.git.username=******
spring.cloud.config.server.git.password=******
启动类加入注解@EnableConfigServer
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
启动项目,访问http://localhost:1112/myspringboot-dev.properties/,发现有中文乱码
注:仓库中的配置文件会被转换成web接口,访问规则:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties

解决中文乱码,参考:https://blog.csdn.net/sinat_38843093/article/details/79960777
新建自定义解析器MyPropertiesHandler,继承PropertiesPropertySourceLoader,重写方法
/**
* 解决中文乱码问题
* 参考:https://blog.csdn.net/sinat_38843093/article/details/79960777
*/
public class MyPropertiesHandler extends PropertiesPropertySourceLoader { @Override
public String[] getFileExtensions() {
return new String[]{"properties", "xml"};
} @Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
ArrayList<PropertySource<?>> list = new ArrayList<>();
Properties properties = getProperties(resource);
if (!properties.isEmpty()) {
list.add(new PropertiesPropertySource(name, properties));
}
return list;
} private Properties getProperties(Resource resource) throws IOException {
Properties properties = new Properties();
InputStream inputStream = resource.getInputStream();
properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
inputStream.close();
return properties;
}
}
resources文件夹下面新建META-INF文件夹,在里面创建spring.factories文件,指定使用我们自定义的解析器
org.springframework.boot.env.PropertySourceLoader=cn.huanzi.qch.config.configserver.MyPropertiesHandler
重新启动项目,在自定义解析器后进行断点调试,发现解析的时候中文乱码问题得以解决,但响应回去还是乱码


解决http响应中文乱码问题
配置文件添加
#解决http响应数据中文乱码问题
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-
最终效果

我们去GitHub修改配置中心的值,看下config server能不能实时获取最新数据

改完后刷新http://localhost:1112/myspringboot-dev.properties/,配置中心可以实时获取最新数据

Config Client
客户端我们直接用之前的项目:myspringboot,这里就当做一个在Eureka上注册了的普通springboot项目
maven引入jar
<!-- config-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
application.properties
#设置服务端口
server.port=
spring.application.name=myspringboot #eureka
eureka.client.serviceUrl.defaultZone=http://localhost:/eureka/
#健康检查(需要spring-boot-starter-actuator依赖)
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=
# 续约到期时间(默认90秒)
eureka.instance.lease-expiration-duration-in-seconds=
#超时时间
feign.httpclient.connection-timeout=
使用优先级更高的bootstrap.properties进行config的配置,因为
#关闭spring cloud config,spring cloud默认要从config中读取配置,通过该配置,只从本地application.properties中读取配置
#spring.cloud.config.enabled=false #配置文件名(当应用名跟配置文件相同时可以不用配置)
spring.cloud.config.name=myspringboot
# dev 开发环境配置文件 | test 测试环境 | pro 正式环境
spring.cloud.config.profile=dev
# 远程仓库的分支
spring.cloud.config.label=master #指定配置中心名称(如果使用eureka可以这样配置)
#spring.cloud.config.discovery.service-id=config-server
#启用发现服务功能
#spring.cloud.config.discovery.enabled=true #配置服务中心地址(如果不使用eureka可以直接配置url路径)
spring.cloud.config.uri=http://localhost:/
如果使用从eureka获取配置中心实例,则要在指定服务之前进行注册配置,否则会报错,因为你还没在Eureka注册就去Eureka查找配置中心,如:
#设置服务端口
server.port=
spring.application.name=myspringboot #eureka
eureka.client.serviceUrl.defaultZone=http://localhost:/eureka/
#健康检查(需要spring-boot-starter-actuator依赖)
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=
# 续约到期时间(默认90秒)
eureka.instance.lease-expiration-duration-in-seconds=
#超时时间
feign.httpclient.connection-timeout= #关闭spring cloud config,spring cloud默认要从config中读取配置,通过该配置,只从本地application.properties中读取配置
#spring.cloud.config.enabled=false #配置文件名(当应用名跟配置文件相同时可以不用配置)
spring.cloud.config.name=myspringboot
# dev 开发环境配置文件 | test 测试环境 | pro 正式环境
spring.cloud.config.profile=dev
# 远程仓库的分支
spring.cloud.config.label=master #指定配置中心名称(如果使用eureka可以这样配置)
spring.cloud.config.discovery.service-id=config-server
#启用发现服务功能
spring.cloud.config.discovery.enabled=true #配置服务中心地址(如果不使用eureka可以直接配置url路径)
#spring.cloud.config.uri=http://localhost:1112/

测试
我们直接在启动类进行测试
@EnableEurekaClient
@SpringBootApplication
@RestController
public class MyspringbootApplication{ public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
} @Value("${huanzi.qch.config.server.username}")
private String username; /**
* 访问首页
*/
@GetMapping("/index")
public String index(){
return "hello springboot!username:" + username;
} }
查看启动日志,客户端已经发现了配置中心,并且从配置中心发现了myspringboot配置文件

访问http://localhost:10087/index,值已经取到了

其实客户端(Config Client)也是可以读取服务端(Config Server)配置文件里面的值,例如:


客户端(Config Client)是可以读取到这个值的,由此可见,是客户端是读取了服务端的数据,而服务端负责实时获取GitHub上面的数据
refresh手动刷新
我们已经在客户端取到了配置中心的值,但当我们修改GitHub上面的值时,服务端(Config Server)能实时获取最新的值,但客户端(Config Client)读的是缓存,无法实时获取最新值
spring已经为我们解决了这个问题,那就是客户端使用post去触发refresh,获取最新数据,需要依赖spring-boot-starter-actuator
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
对应的controller类加上@RefreshScope
@RefreshScope
@EnableEurekaClient
@SpringBootApplication
@RestController
public class MyspringbootApplication{ public static void main(String[] args) {
SpringApplication.run(MyspringbootApplication.class, args);
} @Value("${huanzi.qch.config.server.username}")
private String username; /**
* 访问首页
*/
@GetMapping("/index")
public String index(){
return "hello springboot!username:" + username;
} }
启动后查看日志发现,actuator有个基础路径/actuator,同时还暴露了两个终端(不知道是哪两个端点...)

但是当我们post访问http://localhost:10087/actuator/refresh时,报404,这是什么回事?
注:这里插一句话:从网上找了个js的ajax(要注意content-type的类型)
var Ajax={
get: function(url, fn) {
// XMLHttpRequest对象用于在后台与服务器交换数据
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
// readyState == 4说明请求已完成
if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
// 从服务器获得数据
fn.call(this, xhr.responseText);
}
};
xhr.send();
},
// datat应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
post: function (url, data, fn) {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
// 添加http头,发送信息至服务器时内容编码类型
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
fn.call(this, xhr.responseText);
}
};
xhr.send(data);
}
}
Ajax.post("http://localhost:10087/actuator/refresh",null,function(data){console.log(data)})

经过层层查找,最后在帮助文档发现:默认情况下,自Spring Boot 2.0以来,默认情况下不会公开Actuator端点,需要手动暴露端点

配置文件暴露端点
#只暴露refresh,当然也可以暴露所有:=*
management.endpoints.web.exposure.include=refresh
重启客户端,我们将GitHub配置文件改回:huanzi.qch.config.server.username: 张三
访问测试接口,还是张三1

post调用refresh

刷新,数据更新

总结
这里总结一下遇到的坑:
调用refresh报404的时候,百度查找都是说默认安全拦截,配置关闭:management.security.enabled=false,配置上去的时候发现报错,波浪线,被弃用了,

最后还是靠Google,在知乎(https://zhuanlan.zhihu.com/p/34784934)上面找到了答案
并且吐槽吐槽百度:
同样的关键字,Google搜出来的第一个就能解决问题

而垃圾百度,没一个可以...

代码开源
代码已经开源、托管到我的GitHub、码云:
GitHub:https://github.com/huanzi-qch/springCloud
码云:https://gitee.com/huanzi-qch/springCloud
SpringCloud系列——Config 配置中心的更多相关文章
- springCloud系列 Config配置中心
1.config服务的部署 2.yum文件的格式 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 3.热部署 4.配 ...
- SpringCloud之Config配置中心+BUS消息总线原理及其配置
一.配置中心作用 在常规的开发中,每个微服务都包含代码和配置.其配置包含服务配置.各类开关和业务配置.如果系统结构中的微服务节点较少,那么常规的代码+配置的开发方式足以解决问题.当系统逐步迭代,其微服 ...
- springcloud之config配置中心-Finchley.SR2版
本篇和大家分享的是springcloud-config配置中心搭建,写到这里突然想起自己曾今开源过基于Redis发布订阅编写的一个配置中心,刚看了git星数有点少哈哈,这里顺势发个连接欢迎大侠们点赞: ...
- SpringCloud系列之配置中心(Config)使用说明
大家好,最近公司新项目采用SpingCloud全家桶进行开发,原先对SpringCloud仅仅只是停留在了解的初级层面,此次借助新项目的契机可以深入实践下SpringCloud,甚是Happy.大学毕 ...
- Spring-Cloud之Config配置中心-7
一.我们前面基本上都是讲解的Spring Cloud Netflix的组件,下面我们会重点说Spring Cloud Config分布式配置中心.为什么需要这个组件来管理配置呢?在分布式应用开发过程中 ...
- SpringBoot(三) - Ribbon客户端负载均衡,Zuul网关,Config配置中心
1.Ribbon客户端负载均衡 1.1 依赖 1.2 配置信息 # feign默认加载了ribbon负载均衡,默认负载均衡机制是:轮询 # 负载均衡机制是添加在消费端(客户端)的,如果改为随机,指定服 ...
- SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版
前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心( ...
- SpringCloud学习系列之四-----配置中心(Config)使用详解
前言 本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程. SpringCloud Config Config 介绍 Spring Clou ...
- 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh
SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...
随机推荐
- javaWeb使用POI操作Excel
1.为项目添加POI POI官网链接 点进去之后下载(上边的是编译好的类,下边的是源代码) 解压文件夹,把下面三个文件复制到WebComtent>WEB-INF>lib文件夹下 再把这三个 ...
- postman-----使用CSV和Json文件实现批量接口测试
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; color: #454545 } span ...
- 读书笔记--Android Gradle权威指南(下)
前言 最近看了一本书<Android Gradle 权威指南>,收获挺多,就想着来记录一些读书笔记,方便后续查阅. 本篇内容是基于上一篇:读书笔记--Android Gradle权威指南( ...
- 补习系列(20)-大话 WebSocket 与 "尬聊"的实现
目录 一.聊聊 WebSocket 二.Stomp 是个什么鬼 三.SpringBoot 整合 WebSocket A. 引入依赖 B. WebSocket 配置 C. 控制器 D. 前端实现 四.参 ...
- mariaDB vs mysql
mariaDB vs mysql 今天遇到一个库使用的是mariaDB的数据库版本 Server version: 10.1.20-MariaDB MariaDB Server 理了一下mariaDB ...
- 『简单积性函数和dirichlet卷积』
简单积性函数 在学习欧拉函数的时候,相信读者对积性函数的概念已经有了一定的了解.接下来,我们将相信介绍几种简单的积性函数,以备\(dirichlet\)卷积的运用. 定义 数论函数:在数论上,对于定义 ...
- Asp.Net Core中HttpClient的使用方式
在.Net Core应用开发中,调用第三方接口也是常有的事情,HttpClient使用人数.使用频率算是最高的一种了,在.Net Core中,HttpClient的使用方式随着版本的升级也发生了一些变 ...
- vue项目使用websocket技术
一.为什么需要websocket? 前端和后端的交互模式最常见的就是前端发数据请求,从后端拿到数据后展示到页面中.如果前端不做操作,后端不能主动向前端推送数据,这也是http协议的缺陷. 因此,一种新 ...
- Docker最全教程之Go实战,墙裂推荐(十八)
前言 与其他语言相比,Go非常值得推荐和学习,真香!为什么?主要是可以直接编译成机器代码(性能优越,体积非常小,可达10来M,见实践教程图片)而且设计良好,上手门槛低.本篇主要侧重于讲解了Go语言的优 ...
- Java并发编程面试题 Top 50 整理版
本文在 Java线程面试题 Top 50的基础上,对部分答案进行进行了整理和补充,问题答案主要来自<Java编程思想(第四版)>,<Java并发编程实战>和一些优秀的博客,当然 ...