spring-cloud-config 配置中心实现

Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为server端和client端。

server端为分布式配置中心,是一个独立的微服务应用;client端为分布式系统中的基础设置或微服务应用,通过指定配置中心来管理相关的配置。

Spring Cloud Config 构建的配置中心,除了适用于 Spring 构建的应用外,也可以在任何其他语言构建的应用中使用。

Spring Cloud Config 默认采用 Git 存储配置信息,支持对配置信息的版本管理。

本示例主要内容

  • 配置中心演示client端和server端实现
  • 配置文件放在git(因github有时候不太稳定,我放到了国内服务器)
  • 版本切换(test、pro、dev)

Spring Cloud Config 特点

  • 提供server端和client端支持(Spring Cloud Config Server和Spring Cloud Config Client);
  • 集中式管理分布式环境下的应用配置;
  • 基于Spring环境,实现了与Spring应用无缝集成;
  • 可用于任何语言开发的程序;
  • 默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理;同时也支持配置从本地文件或数据库读取。

代码构建

server端实现

1.pom.xml添加maven依赖

    <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

2.application.yml配置

server:
port: 8001
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/tqlin/spring-boot-demo.git #因为github有时候不稳定,我这里改到了码云仓
searchPaths: /cloud-config/config-repo/ #配置文件目录
force-pull: true

3.CloudConfigServerApplication.java启动类

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(CloudConfigServerApplication.class, args);
}
}

client端实现

1.pom.xml添加maven依赖

    <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2.bootstrap.properties配置文件

spring.cloud.config.name=easy-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
  • spring.application.name:对应{application}部分

  • spring.cloud.config.profile:对应{profile}部分

  • spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用

  • spring.cloud.config.uri:配置中心的具体地址(sever端地址)

  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。

    特别注意:Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。

3.application.properties配置文件

spring.application.name=cloud-config-client
server.port=8002

运行示例

1.首先在码云上面创建一个文件夹config-repo用来存放配置文件,我们创建以下三个配置文件:

  // 开发环境
easy-config-dev.properties 内容为:easy.hello=dev config
// 测试环境
easy-config-test.properties 内容为:easy.hello=test config
// 生产环境
easy-config-pro.properties 内容为:easy.hello=pro config

根据上面构建的代码指定的项目地址为:https://gitee.com/tqlin/spring-boot-demo.git 目录为: /cloud-config/config-repo/

2.分别运行server端和client端

找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分别运行

3.测试server端

直接访问:http://localhost:8001/easy-config/dev

我们看到成功返回了开发配置文件信息

{
name: "easy-config",
profiles: [
"dev"
],
label: null,
version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96",
state: null,
propertySources: [
{
name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties",
source: {
easy.hello: "dev config"
}
}
]
}

访问:http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相应的会返回测试及正式环境的配置

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

以easy-config-dev.properties为例子,它的application是easy-config,profile是dev。client会根据填写的参数来选择读取对应的配置。

4.测试client端

访问:http://localhost:8002/hello 我们发现界面成功返回了 test config,说明测试配置文件client端读取成功了

我们修改bootstrap.properties配置的spring.cloud.config.profile的值为dev,重启client端,访问:http://localhost:8002/hello 这时候界面返回 dev config,表示开发配置访问成功。

资料

spring-cloud-config 配置中心快速上手的更多相关文章

  1. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  2. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  3. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  4. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  5. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  6. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

  7. 微服务SpringCloud之Spring Cloud Config配置中心SVN

    在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...

  8. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

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

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

  10. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

随机推荐

  1. python 之 并发编程(守护进程、互斥锁、IPC通信机制)

    9.5 守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就立即终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic process ...

  2. Python入门基础(8)

    上一篇介绍了一些python中函数的基本用法,那么,现在就来谈一谈python中的一些内置函数吧 python中的内置函数 1.map()函数 map()函数接受两个参数值,一个是函数,一个是Iter ...

  3. 20190719 NOIP模拟测试6 (考后反思)

    总分 130 排名第6 虽然与前几次进步了一些,但总会感觉到不安 因为我只是A掉了第一题,而第一题又是道水题,很显然的DP,我相信大佬们没A掉只是因为一些小问题(也许有大佬不屑于这种题吧,lockey ...

  4. Android 开发感想

    18年从.net转行做安卓开发,现在已经过去一年多了.说一下感想和心得体会! 一.开始 说一下我的经厉,从毕业开始出来工作一直是从事.net方向的开发工作.一开始也是没什么经验,加上也没有其他手艺就找 ...

  5. C#写进程守护程序

    最近写了好多次进程守护程序,今天在这里总结一下. 用到的知识点: 1.在程序中启动进程, 2.写Windows服务, 3.以及在Windows服务中启动带界面的程序 关于第三点的问题,我在我的上一篇博 ...

  6. 《VR入门系列教程》之6---VR硬件介绍及DK1

    第二章 VR硬件介绍     本章主要介绍当前比较流行的消费版VR设备,包括VR头显以及应用运行的PC和手机平台.     即使是在这工业高速发展的时代,一些大厂(比如Facebook的Oculus ...

  7. golang "[]uint8" to string

    关于Uinit8和Byte: The Go Programming Language Specification Numeric types uint8 the set of all unsigned ...

  8. JavaScript Boolean(逻辑)对象

    Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false). 实例 检查逻辑值 检查逻辑对象是 true 还是 false. 完整的 Boolean 对象参考手册 我们提供 J ...

  9. Java集合系列(二):ArrayList、LinkedList、Vector的使用方法及区别

    本篇博客主要讲解List接口的三个实现类ArrayList.LinkedList.Vector的使用方法以及三者之间的区别. 1. ArrayList使用 ArrayList是List接口最常用的实现 ...

  10. 提升10倍生产力:IDEA远程一键部署SpringBoot到Docker

    作者:陶章好 juejin.im/post/5d026212f265da1b8608828b 推荐阅读(点击即可跳转阅读) 1. SpringBoot内容聚合 2. 面试题内容聚合 3. 设计模式内容 ...