springcloud(五):Spring Cloud 配置中心的基本用法
Spring Cloud 配置中心的基本用法
1. 概述
本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明。
配置中心服务器部分内容包括:服务创建,git,svn,native后端的配置,各种url访问
配置中心客户端部分内容包括:访问配置、failfast,重试
2. Spring Cloud Config的服务端
2.1. 简述
我们在开发大的系统时,由于服务较多,相同的配置(如数据库信息、缓存、开关量等)会出现在不同的服务上,如果一个配置发生变化,则可能需要修改很多的服务配置。为了解决这个问题,spring cloud提供配置中心。
首先所有的公共配置存储在相同的地址(存储的地方可以是git,svn和本地文件),然后配置中心从这些地方读取配置以restful发布出来,其它服务可以调用接口获取配置信息。
2.2. 配置服务
引入关键jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
通过@EnableConfigServer可以激活配置中心服务。配置中心可以单独做服务,也可以嵌入到其它服务中。推荐用单独做服务方式使用配置中心。
@SpringBootApplication
@EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务
public class CloudGitConfigServerApplication {
public static void main(String[] args) {
args = new String[1];
args[0] = "--spring.profiles.active=gitsimple2";
SpringApplication.run(CloudGitConfigServerApplication.class, args);
}
}
由于配置文件的存储的多样性,下面介绍每种配置形式如何配置。所有的配置都配置在application-*.yml中
2.3. git后端
Spring Cloud配置中心的后端系统可以是:
- VCS(如git,svn等)
- 本地文件
本节我们介绍git配置
配置参数主要配置中application-gitsimple2.yml
spring:
application:
name: special
cloud:
config:
server:
git:
# 配置文件只搜索url目录下的searchPaths
uri: https://github.com/hryou0922/spring_cloud.git
# 指定搜索路径,如果有多个路径则使用,分隔
searchPaths: cloud-config-git/simple2/configspecial,cloud-config-git/simple2/default
# 对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件
basedir: /tmp/spring-cloud-repo
# 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
force-pull: true
spring.cloud.config.server.git.url:指定配置文件所在远程git库的url地址
spring.cloud.config.server.git.searchPaths:和上面的参数url配合使用,定位git库的子目录。指定搜索路径,如果有多个路径则使用,分隔
spring.cloud.config.server.git.basedir:对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件。默认存储在系统临时目录下,目录名的前缀为config-repo-,如在linux下时可能是/tmp/config-repo-。因为/tmp下的内容有可能被误删,所有为了保险,最好修改存储目录。如果你修改存储目录,你可以修改spring.cloud.config.server.git.basedir
spring.cloud.config.server.git.force-pull:配置中心通过git从远程git库读取数据时,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置。设置force-pull=true,则强制从远程库中更新本地库
以上是一些常用的配置,其它配置可以自己看配置类MultipleJGitEnvironmentRepository类
2.4. svn后端
svn的配置方法和git差不多,主要使用”spring.cloud.config.server.svn.*”。这里略
2.5. 文件系统后端
除了使用从git/svn下载配置文件,你可以从classpath目录或本地文件系统中加载配置文件。通过spring.cloud.config.server.native.searchLocations配置地址.这里又分为两类:
- 从本地目录加载配置文件:以file开头
- file:///${user.home}/config-repo
默认值:file:./, file:./config
- file:///${user.home}/config-repo
- 从classpath中加载配置文件:以classpath开头
- 如果不配置值,则默认值:classpath:/, classpath:/config
以classpath为例,file的用法和classpath用法相同,这里略.
spring:
profiles:
# native:启动从本地读取配置文件,必须指定active的值,才可以使用本地文件配置模式
active: native
# 自定义配置文件路径
cloud:
config:
server:
native:
searchLocations: classpath:/config/simple2/
spring.profiles.active: 如果使用本地系统配置,则此值必须是native
spring.cloud.config.server.native.searchLocations: 指定配置文件的路径
2.6. 启动和测试
通过CloudGitConfigServerApplication就可以启动服务
在浏览器中输入如下URL,可以访问到配置文件
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
下面通过具体例子说明以上url的意思。如果我们的配置文件名称cloud-config-simple2.yml,则其和URL中各个字段对应的值为:
- application: cloud-config
- profile: simple2
- label: 9500e50f08c43e3e4391175c8f6d5a326b11302f
我们访问以下地址都可以访问到配置文件config-simple2.yml和特定版本下此文件:
http://127.0.0.1:10888/cloud-config/simple2
http://127.0.0.1:10888/cloud-config/simple2/9500e50f08c43e3e4391175c8f6d5a326b11302f
http://127.0.0.1:10888/cloud-config-simple2.yml
http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.yml
http://127.0.0.1:10888/cloud-config-simple2.properties
http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.properties
3. Spring Cloud Config的客户端
配置中心服务端配置成功后,然后其它服务从配置中心获取配置文件,这样的服务被称为客户端。
3.1. 配置客户端
引入jar:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
只要是@SpringBootApplication注解启动的spring boot即可
@SpringBootApplication
public class SimpleCloudServiceApplication {
public static void main(String[] args) {
args = new String[1];
args[0] = "--spring.profiles.active=simple2";
SpringApplication.run(SimpleCloudServiceApplication.class, args);
}
}
3.2. 配置参数
请将配置中心的相关配置配置在bootstrap-.yml中,不要配置appliaction-.yml。因为服务启动时,会从bootstrap中读取配置,然后从远程配置中心读取配置文件,最后再从appliaction中获取配置,如果有相同的配置项,则后面的会覆盖前面读到的值。所以如果配置中心的配置配置在appliaction,则配置项不会有任何效果。
bootstrap-simple2.yml
spring:
cloud:
# 配置服务器的地址
config:
uri: http://127.0.0.1:10888
# 要读取配置文件读取的值
name: cloud-config
# 如果不设置此值,则系统设置此值为 spring.profiles.active
profile: dev
# 可以使用之前的版本。默认值可以是git label, branch name or commit id。可以使用多个Label,多个Label可以使用逗号分隔
# label:
# true: 如果访问配置中心失败,则停止启动服务
fail-fast: true
# 配置重试,默认是重试6次,最初是延迟1s再次重试,如果再失败,则延迟1.1*1s、1.1*1.1*1s、… 。可以使用这个配置
retry:
initial-interval: 2000
# 最多重试次数
max-attempts: 6
# 最大重试间隔
max-interval: 4000
# 每次重试时间是之前的倍数
multiplier: 1.2
重要参数的解释如下:
配置中心的url
即从哪里读取配置文件,通过“spring.cloud.config.url”配置
要读取哪些配置文件
由以下参数共同决定,和”2.6. 启动和测试”结合看加深理解。
- “spring.cloud.config.name”:配置文件名称,对应上文的读取URL中的{applicaion}值
- “spring.cloud.config.profile”:配置文件的profile,对应上文的URL中的{profile}值
- “spring.cloud.config.label”: 可以使用之前的版本。默认值可以是git label, branch name or commit id。可以使用多个Label,多个Label可以使用逗号分隔
快速失败
如果要求客户端访问配置中心失败,则立即停止启动服务,则设置“spring.cloud.config.label”为 true
重试
如果访问配置失败,则自动重试。默认是重试6次,最初是延迟1s再次重试,如果再失败,则延迟1.1*1s、1.1*1.1*1s、… 。通过下面参数可以修改值:
- “spring.cloud.config.retry.initial-interval”:第一次失败,延迟多久重试
- “spring.cloud.config.retry.max-attempts”:最多重试次数
- “spring.cloud.config.retry.max-interval”: 最大重试间隔
“spring.cloud.config.retry.multiplier”: 每次重试时间是之前的倍数
如果要实现重试功能,需要引入新的jar
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
3.3. 启动和测试
启动SimpleCloudServiceApplication,在浏览器输入http://127.0.0.1:10082/simple,会返回以下信息,则表示成功
{"age":112,"name":"git2-default-dev","randomNum":53}
转自:https://blog.csdn.net/hry2015/article/details/77870854?utm_source=tuicool&utm_medium=referral
springcloud(五):Spring Cloud 配置中心的基本用法的更多相关文章
- springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容
Spring Cloud 配置中心采用数据库存储配置内容 转自:Spring Cloud Config采用数据库存储配置内容[Edgware+] Spring Cloud Server配置中心采用了G ...
- spring cloud 配置中心
1. spring cloud配置中心server 1.1 创建git仓库 首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支.自己学习可以用公开库 ...
- 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.
spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 ...
- Spring Cloud配置中心(Config)
Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...
- (七)Spring Cloud 配置中心config
spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...
- Spring Cloud配置中心搭建(集成Git)
1. 在Github(或其他)创建配置中心仓库bounter-config-repo,然后在仓库创建两个配置文件:simon.properties.susan.properties,链接如下: htt ...
- Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...
- Spring Cloud配置中心内容加密
从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理. 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够 ...
- Spring Cloud配置中心高可用搭建
本文通过config server连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行. 引入依赖 <dependencies> <dependency> ...
随机推荐
- what is the CCA?
Clear Channel Assessment (CCA) is one of two carrier sense mechanisms in WLAN (or WiFi). It is defin ...
- xpath爬虫实例,爬取图片网站百度盘地址和提取码
某套图网站,套图以封面形式展现在页面,需要依次点击套图,点击广告盘链接,最后到达百度网盘展示页面. 这一过程通过爬虫来实现,收集百度网盘地址和提取码,采用xpath爬虫技术 1.首先分析图片列表页,该 ...
- 【0809 | Day 12】可变长参数/函数的对象/函数的嵌套/名称空间与作用域
可变长参数 一.形参 位置形参 默认形参 二.实参 位置实参 关键字实参 三.可变长参数之* def func(name,pwd,*args): print('name:',name,'pwd:',p ...
- 《深入理解Java虚拟机》-Java代码是如何运行的
问题一:Java与C++区别 1.Java需要运行时环境,包括Java虚拟机以及Java核心类库等. 2.C++无需额外的运行时,通常编译后的代码可以让机器直接读取,即机器码 问题一:Java为什么要 ...
- 002——Netty之Netty介绍
Netty出现背景 Java NIO难用 据说存在bug 业界其他NIO框架不成熟 Netty主要解决两个相应关注领域 (1)异步和事件驱动的实现. (2)一组设计模式,将应用逻辑与网络层解耦. 特性 ...
- 在linux中用同一个版本的R 同时安装 Seurat2 和 Seurat3
在linux中用同一个版本的R 同时安装 Seurat 2 和 Seurat 3 Seurat 作为单细胞分析中的重量级R包,有多好用用,用过的人都知道.Seurat 分析流程基本涵盖了单细胞分析中 ...
- 消息中间件——RabbitMQ(四)命令行与管控台的基本操作!
前言 在前面的文章中我们介绍过RabbitMQ的搭建:RabbitMQ的安装过以及各大主流消息中间件的对比:,本章就主要来介绍下我们之前安装的管控台是如何使用以及如何通过命令行进行操作. 1. 命令行 ...
- v语言怎么玩
直接上github: https://github.com/vlang/v 前戏 大概是在6月份的时候,在github上看到了这个玩意,我以为是??? 我下意识的去查了一下有没有人在讨论这个语言,但是 ...
- @Validated和@Valid区别
注解地方 @Validated:可以用在类型.方法和方法参数上.但是不能用在成员属性(字段)上 @Valid:可以用在方法.构造函数.方法参数和成员属性(字段)上 两者是否能用于成员属性(字段)上直接 ...
- 牛客网2016.4.11(两个数相加为sum/计数一个int型的二进制有多少个1/二叉树是否左右对称)
求最小的两个数相加为sum //求最小的两个数相加为sum public ArrayList<Integer> FindNumbersWithSum(int [] array,int su ...