Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config 实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于 Spring 构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:GIT仓库、SVN 仓库、本地化文件系统。
下面我们将构建一个基于 Git 存储的分布式配置中心,并对客户端进行改造,让其能够从配置中心获取配置信息并绑定到代码中。
准备工作
准备一个 Git 仓库,在 Github 上面创建了一个文件夹 config-repo 用来存放配置文件,为了模拟生产环境,我们创建以下三个配置文件:
// 开发环境
config-server-dev.yml
// 测试环境
config-server-test.yml
// 生产环境
config-server-prod.yml
每个配置文件中都写一个属性 info.profile, 属性值分别是 dev/test/prod
Server 端
创建一个基础的 Spring Boot 工程,命名为:service-config-server
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>
配置文件
在 application.yml 中添加配置服务的基本信息以及 Git 仓库的相关信息
server:
port: 9300
spring:
application:
name: service-config-server
cloud:
config:
server:
git:
uri: https://github.com/carry-chan/spring-cloud # 配置git仓库的地址
search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/
Spring Cloud Config 也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会从默认的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来制定配置文件的位置。虽然 Spring Cloud Config 提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用 Git 的方式。
如果我们的 Git 仓库需要权限访问,那么可以通过配置下面的两个属性来实现
spring.cloud.config.server.git.username:访问 Git 仓库的用户名
spring.cloud.config.server.git.password:访问 Git 仓库的用户密码
启动类
package com.carry.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer
@SpringBootApplication
public class ServiceConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ServiceConfigServerApplication.class, args);
}
}
到此 Server 端相关配置已经完成。
测试
启动eureka-server、service-config-server,浏览器直接访问 http://localhost:9300/config-server/dev 返回信息如下:
{
"name": "config-server",
"profiles": [
"dev"
],
"label": null,
"version": "beb220098b44c60cf99277f064a19d52e7ebeb91",
"state": null,
"propertySources": [
{
"name": "https://github.com/carry-chan/spring-cloud/config-repo/config-server-dev.yml",
"source": {
"info.profile": "dev"
}
}
]
}
上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明 Server 端已经成功获取了 Git 仓库的配置信息。
如果直接查看配置文件中的配置信息可访问 http://localhost:9300/config-server-dev.yml 返回:
info:
profile: dev
修改配置文件config-server-dev.yml中配置信息为dev v0,再次在浏览器访问 http://localhost:9300/config-server-dev.yml 返回:dev v0,说明 Server 端会自动读取最新提交的内容。
仓库中的配置文件会被转换成 Web 接口,访问可以参照以下的规则:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的 URL 会映射 {application}-{profile}.yml 对应的配置文件,其中 {label} 对应 Git 上不同的分支,默认为 master。
Client 端
创建一个基础的 Spring Boot 应用,命名为 service-config-client。
POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置文件
需要配置两个配置文件,application.yml 和 bootstrap.yml,配置分别如下:
application.yml
server:
port: 9400
spring:
application:
name: service-config-client
bootstrap.yml
spring:
cloud:
config:
name: config-server # 对应 {application} 部分
profile: dev # 对应 {profile} 部分
label: master # 对应 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存储,则该参数无用
discovery:
enabled: true
service-id: service-config-server #springcloud config的服务名
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/
注意:上面这些与 Spring Cloud Config 相关的属性必须配置在 bootstrap.yml 中,config 部分内容才能被正确加载。因为 config 的相关配置会先于 application.yml,而 bootstrap.yml 的加载也是先于 application.yml。
启动类
package com.carry.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ServiceConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ServiceConfigClientApplication.class, args);
}
}
在 Controller 中使用 @Value
注解来获取 Server 端参数的值
package com.carry.springcloud; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; @RestController
public class ClientController { @Value("${info.profile}")
private String profile; @GetMapping("/info")
public Mono<String> hello() {
return Mono.justOrEmpty(profile);
}
}
测试
依次启动项目eureka-server、service-config-server、service-config-client,访问 http://localhost:9400/info 返回dev 说明已经正确的从 Server 端获取到了参数。
手动修改Git仓库中 config-server-dev.yml 的值,再次访问 http://localhost:9400/info 依旧返回dev,这是因为 Spring Cloud Config 分服务端和客户端,服务端负责将 Git 中存储的配置文件发布成 REST 接口,客户端可以从服务端 REST 接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置。客户端如何去主动获取新的配置信息呢,Spring Cloud 已经给我们提供了解决方案,每个客户端通过 POST 方法触发各自的 /actuator/refresh。
Refresh功能
修改客户端即 service-config-client 项目
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
增加了spring-boot-starter-actuator
包,spring-boot-starter-actuator
是一套监控的功能,可以监控程序在运行时状态,其中就包括/actuator/refresh
的功能。
开启更新机制
需要给加载变量的类上面加 @RefreshScope
,在客户端执行/actuator/refresh
的时候就会更新此类下面的变量值。
package com.carry.springcloud; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; @RestController
@RefreshScope
public class ClientController { @Value("${info.profile}")
private String profile; @GetMapping("/info")
public Mono<String> hello() {
return Mono.justOrEmpty(profile);
}
}
配置
Spring Boot 1.5.X 以上默认开通了安全认证,所以要在配置文件 application.yml 中添加以下配置以将/actuator/refresh
这个 Endpoint 暴露出来
management:
endpoints:
web:
exposure:
include: refresh
测试
重启 service-config-client 项目
访问 http://localhost:9400/info 返回dev
我将 Git 上对应配置文件里的值改为dev v0
执行 curl -X POST http://localhost:9400/actuator/refresh
,返回["config.client.version","info.profile"]
再次访问 http://localhost:9400/info 返回dev v0
这就说明客户端已经得到了最新的值,Refresh 是有效的。
Webhook
现在虽然可以不用重启服务就更新配置了,但还是需要我们手动操作,这样还是不可取的。所以,这里就要用到git的webhooks来达到自动更新配置。
打开git上配置仓库的地址,添加webhooks
上面的Payload URL就填写我们的配置中心触发刷新的地址,当然这里不能写localhost,要外网访问地址才行。还有这里面有个Secret的秘钥验证,如果这里填写的话,在配置文件上要写上encrypt.key与之对应。
Spring Cloud学习笔记【九】配置中心Spring Cloud Config的更多相关文章
- Spring框架学习笔记(5)——Spring Boot创建与使用
Spring Boot可以更为方便地搭建一个Web系统,之后服务器上部署也较为方便 创建Spring boot项目 1. 使用IDEA创建项目 2. 修改groupid和artifact 3. 一路n ...
- Spring框架学习笔记(8)——spring boot+mybatis plus+mysql项目环境搭建
之前写的那篇Spring框架学习笔记(5)--Spring Boot创建与使用,发现有多小细节没有提及,,正好现在又学习了mybatis plus这款框架,打算重新整理一遍,并将细节说清楚 1.通过I ...
- 【Spring Cloud学习之五】配置中心
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.什么是配置中心在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实 ...
- Spring框架学习笔记(7)——Spring Boot 实现上传和下载
最近忙着都没时间写博客了,做了个项目,实现了下载功能,没用到上传,写这篇文章也是顺便参考学习了如何实现上传,上传和下载做一篇笔记吧 下载 主要有下面的两种方式: 通过ResponseEntity实现 ...
- spring实战学习笔记(一)spring装配bean
最近在学习spring boot 发现对某些注解不是很深入的了解.看技术书给出的实例 会很疑惑为什么要用这个注解? 这个注解的作用?有其他相同作用的注解吗?这个注解的运行机制是什么?等等 spring ...
- SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法
系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 SpringMVC 的 ...
- Spring框架学习笔记(10)——Spring中的事务管理
什么是事务 举例:A给B转500,两个动作,A的账户少500,B的账户多500 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 一.注解添加事务管理方 ...
- Spring框架学习笔记(9)——Spring对JDBC的支持
一.使用JdbcTemplate和JdbcDaoSupport 1.配置并连接数据库 ①创建项目并添加jar包,要比之前Spring项目多添加两个jar包c3p0-0.9.1.2.jar和mysql- ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config
目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...
随机推荐
- 【原创】TimeSten安装与配置
1.安装TimeSten 2.安装时要指定TNS_ADMIN_LOCATION,即tnsnames.ora的路径,因为tt会根据这个连接Oracle.C:\TimesTen\tt1122_32\net ...
- hiho 1617 - 方格取数 - dp
题目链接 描述 给定一个NxN的方格矩阵,每个格子中都有一个整数Aij.小Hi和小Ho各自选择一条从左上角格子到右下角格子的路径,要求路径中每一步只能向右或向下移动,并且两条路径不能相交(除了左上右下 ...
- django框架-DRF工程之权限功能
1.相对于flask,原生而言django,DRF做的则更加的合理化,想要给予用户相应的权限,首先需要在settings中进行配置 REST_FRAMEWORK = { 'DEAFAULT_PERMI ...
- NOIp2018模拟赛三十五
两道大数据结构把我砸懵 成绩:未提交 Orz xfz两道正解 A:[BZOJ4049][CREC2014B]mountainous landscape B:CJB的大作(CF改编题)
- 微信小程序手势滑动卡片案例
最近工作中有项目要使用微信小程序技术进行开发,其中一项功能困扰了我很久,卡片滑动动效以及手势识别.经过一番研究和参考,现在把成果展示.记录自己踩到的坑,如果大家有需要,也可以帮助到大家. 效果图: 首 ...
- CodeForces-766D Mahmoud and a Dictionary 并查集 维护同类不同类元素集合
题目链接:https://cn.vjudge.net/problem/CodeForces-766D 题意 写词典,有些词是同义词,有些是反义词,还有没关系的词 首先输入两个词,需要判断是同义还是是反 ...
- CF17E Palisection(manacher)
题意 给出一个长度为N的字符串S,问S中有多少个回文子串对(i,j)使得i,j在S中的位置相交?(N<=2*106) 题解 #include<iostream> #include&l ...
- Android群英传-拼图游戏puzzle-6点吐槽
一.缘由 经常写文章,混了一些C币.最近在深入学习Android应用开发,就从商城里买了一本<Android群英传>.这本书的内容,不是纯粹的入门那种,分几个章节,重点讲解Activit ...
- hadoop-06-http服务
hadoop-06-http服务 su root service httpd status service httpd stop vi /etc/httpd/conf/httpd.conf 修改:Do ...
- ACM POJ 1146 ID Codes
题目大意:输入一个字符串.输出它的下一个字典序排列. 字典序算法思想: 1.从右向左寻找字符串找出第一个a[i]<a[i+1]的位置i; 2.从右向左找出第一个大于a[i]的元素a[j]; 3. ...