Spring Cloud Config:外部集中化配置管理
Spring Cloud Config:外部集中化配置管理
- SpringCloud学习教程
- SpringCloud
Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分,本文将对其用法进行详细介绍。
#Spring Cloud Config 简介
Spring Cloud Config 分为服务端和客户端两个部分。服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。客户端可以通过配置中心来获取配置信息,在启动时加载配置。Spring Cloud Config 的配置中心默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。
#在Git仓库中准备配置信息
由于Spring Cloud Config 需要一个存储配置信息的Git仓库,这里我们先在Git仓库中添加好配置文件再演示其功能,Git仓库地址为:https://gitee.com/macrozheng/springcloud-configopen in new window。
#配置仓库目录结构
#master分支下的配置信息
- config-dev.yml:
config:
info: "config info for dev(master)"
- config-test.yml:
config:
info: "config info for test(master)"
- config-prod.yml:
config:
info: "config info for prod(master)"
#dev分支下的配置信息
- config-dev.yml:
config:
info: "config info for dev(dev)"
- config-test.yml:
config:
info: "config info for test(dev)"
- config-prod.yml:
config:
info: "config info for prod(dev)"
#创建config-server模块
这里我们创建一个config-server模块来演示Spring Cloud Config 作为配置中心的功能。
#在pom.xml中添加相关依赖
<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中进行配置
server:
port: 8901
spring:
application:
name: config-server
cloud:
config:
server:
git: #配置存储配置信息的Git仓库
uri: https://gitee.com/macrozheng/springcloud-config.git
username: macro
password: 123456
clone-on-start: true #开启启动时直接从git获取配置
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
#在启动类上添加@EnableConfigServer注解来启用配置中心功能
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
#通过config-server获取配置信息
这里我们通过config-server来演示下如何获取配置信息。
#获取配置文件信息的访问格式
# 获取配置信息
/{label}/{application}-{profile}
# 获取配置文件信息
/{label}/{application}-{profile}.yml
#占位符相关解释
- application:代表应用名称,默认为配置文件中的spring.application.name,如果配置了spring.cloud.config.name,则为该名称;
- label:代表分支名称,对应配置文件中的spring.cloud.config.label;
- profile:代表环境名称,对应配置文件中的spring.cloud.config.profile。
#获取配置信息演示
启动eureka-server、config-server服务;
访问http://localhost:8901/master/config-devopen in new window来获取master分支上dev环境的配置信息;
- 访问http://localhost:8901/master/config-dev.ymlopen in new window来获取master分支上dev环境的配置文件信息,对比上面信息,可以看出配置信息和配置文件信息并不是同一个概念;
- 访问http://localhost:8901/master/config-test.ymlopen in new window来获取master分支上test环境的配置文件信息:
- 访问http://localhost:8901/dev/config-dev.ymlopen in new window来获取dev分支上dev环境的配置文件信息:
#创建config-client模块
我们创建一个config-client模块来从config-server获取配置。
#在pom.xml中添加相关依赖
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
#在bootstrap.yml中进行配置
server:
port: 9001
spring:
application:
name: config-client
cloud:
config: #Config客户端配置
profile: dev #启用配置后缀名称
label: dev #分支名称
uri: http://localhost:8901 #配置中心地址
name: config #配置文件名称
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
#添加ConfigClientController类用于获取配置
/**
* Created by macro on 2019/9/11.
*/
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
#演示从配置中心获取配置
启动config-client服务;
访问http://localhost:9001/configInfoopen in new window,可以获取到dev分支下dev环境的配置;
config info for dev(dev)
1
#获取子目录下的配置
我们不仅可以把每个项目的配置放在不同的Git仓库存储,也可以在一个Git仓库中存储多个项目的配置,此时就会用到在子目录中搜索配置信息的配置。
- 首先我们需要在config-server中添加相关配置,用于搜索子目录中的配置,这里我们用到了application占位符,表示对于不同的应用,我们从对应应用名称的子目录中搜索配置,比如config子目录中的配置对应config应用;
spring:
cloud:
config:
server:
git:
search-paths: '{application}'
- 访问http://localhost:9001/configInfoopen in new window进行测试,可以发现获取的是config子目录下的配置信息。
config info for config dir dev(dev)
1
#刷新配置
当Git仓库中的配置信息更改后,我们可以通过SpringBoot Actuator的refresh端点来刷新客户端配置信息,以下更改都需要在config-client中进行。
- 在pom.xml中添加Actuator的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 在bootstrap.yml中开启refresh端点:
management:
endpoints:
web:
exposure:
include: 'refresh'
- 在ConfigClientController类添加@RefreshScope注解用于刷新配置:
/**
* Created by macro on 2019/9/11.
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
- 重新启动config-client后,调用refresh端点进行配置刷新:
- 访问http://localhost:9001/configInfoopen in new window进行测试,可以发现配置信息已经刷新。
update config info for config dir dev(dev)
#配置中心添加安全认证
我们可以通过整合SpringSecurity来为配置中心添加安全认证。
#创建config-security-server模块
- 在pom.xml中添加相关依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 在application.yml中进行配置:
server:
port: 8905
spring:
application:
name: config-security-server
cloud:
config:
server:
git:
uri: https://gitee.com/macrozheng/springcloud-config.git
username: macro
password: 123456
clone-on-start: true #开启启动时直接从git获取配置
security: #配置用户名和密码
user:
name: macro
password: 123456
- 启动config-security-server服务。
#修改config-client的配置
- 添加bootstrap-security.yml配置文件,主要是配置了配置中心的用户名和密码:
server:
port: 9002
spring:
application:
name: config-client
cloud:
config:
profile: dev #启用配置后缀名称
label: dev #分支名称
uri: http://localhost:8905 #配置中心地址
name: config #配置文件名称
username: macro
password: 123456
使用bootstrap-security.yml启动config-client服务;
访问http://localhost:9002/configInfoopen in new window进行测试,发现可以获取到配置信息。
config info for dev(dev)
1
#config-sever集群搭建
在微服务架构中,所有服务都从配置中心获取配置,配置中心一旦宕机,会发生很严重的问题,下面我们搭建一个双节点的配置中心集群来解决该问题。
启动两个config-server分别运行在8902和8903端口上;
添加config-client的配置文件bootstrap-cluster.yml,主要是添加了从注册中心获取配置中心地址的配置并去除了配置中心uri的配置:
spring:
cloud:
config:
profile: dev #启用环境名称
label: dev #分支名称
name: config #配置文件名称
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
- 以bootstrap-cluster.yml启动config-client服务,注册中心显示信息如下:
- 访问http://localhost:9003/configInfoopen in new window,发现config-client可以获取到配置信息。
config info for config dir dev(dev)
1
#使用到的模块
springcloud-learning
├── eureka-server -- eureka注册中心
├── config-server -- 配置中心服务
├── config-security-server -- 带安全认证的配置中心服务
└── config-client -- 获取配置的客户端服务
Spring Cloud Config:外部集中化配置管理的更多相关文章
- Spring Cloud Config 分布式配置管理 5.3
Spring Cloud Config简介 在传统的单体式应用系统中,我们通常会将配置文件和代码放在一起,但随着系统越来越大,需要实现的功能越来越多时,我们又不得不将系统升级为分布式系统,同时也会将系 ...
- SpringCloud的配置管理:Spring Cloud Config
演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...
- spring cloud config —— git配置管理
目录 talk is cheep, show your the code Server端 pom.xml server的application.yml 配置文件 测试Server client端 po ...
- 【八】Spring Cloud Config
一.分布式系统面临的--配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的力度相对较小,因此系统中会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...
- spring cloud学习(六)Spring Cloud Config
Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...
- Spring Cloud config之一:分布式配置中心入门介绍
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...
- 【SpringCloud构建微服务系列】使用Spring Cloud Config统一管理服务配置
一.为什么要统一管理微服务配置 对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护 ...
- Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!
本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...
- Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config
目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...
- Spring Cloud Config、Apollo、Nacos配置中心选型及对比
Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...
随机推荐
- Java final 关键字使用
1 package com.bytezreo.finaltest; 2 3 /** 4 * 5 * @Description final 关键字使用 6 * @author Bytezero·zhen ...
- python爬虫 xpath入门与lxml库基本使用,我们一同学习xpath
目录 什么是XPath? xpath语法 知识点 节点 选取节点: 选取a节点下所有的href属性 ../ 选取父节点 bookstore/book 选取子元素li bookstore//book 不 ...
- 摆脱鼠标系列 - vscode - ctrl+up 光标上移动4行 ctrl+down 光标下移4行
为什么 摆脱鼠标系列 - vscode - ctrl+up 光标上移动4行 之前滚动屏幕总是用鼠标,现在改为 ctrl + 上箭头或下箭头 实现起来稍微有些麻烦 实现 需要安装 macros 插件 这 ...
- axios 报 登出跨域 withCredentials: false,
withCredentials: false, 默认值虽然是false,但是之前包装的时候设置成true了,所以最后再设置回来
- 基于C语言的串口AT指令发送实例解析
一 知识点 1 AI指令后面一定要加 \n\r 2 注意AT指令里面待双引号的这种,要使用斜杠隔开. 二 源码: void Set_Pdu_Mode(void) { u8 a = 1; if(atKe ...
- linux shell 字体颜色设置
使用 echo -e "\033[0;32;40m" 可以将字体设置成绿色. 这里必须使用echo 的选项 "-e",因为后面需要用到转义序列. 转义序列就是一 ...
- 关于Android studio无法勾选SDK的问题
这是我遇到的问题,相信也是大多数人遇到的问题,我的经历是之前下载过一次Android studio,用过一段时间后虚拟机出问题了,一直连不上,我都是用手机代替运行,发现太麻烦了,还是决定重新一遍,于是 ...
- 【UE虚幻引擎】干货!UE修改分辨率的3种方法
虚幻引擎作为一款实时3D创作工具,在游戏.建筑.影视动画.虚拟仿真等领域受到全球各行各业创作者广泛欢迎,在UE中获取和设置分辨率也是3D创作开发工作中的常用功能.本文介绍了在虚幻引擎中修改分辨率的3种 ...
- 计算机网络-DNS以及FastGitHub
前言 你是否观察到过这种现象,在访问Github时,有的时候能正常访问,有的时候再次刷新就访问不了,那么能不能有什么办法能一直访问.答案是有,就是在DNS层面能保证一直稳定获取可用并且快速的IP,这就 ...
- Python简单程序设计(计算程序设计(公式)篇)
如题: 解题方式如下: