SpringCloud Config 简介

  在分布式系统中,由于服务组件过多,为了方便争对不通的环境下的服务配置文件统一管理,实时更新,所以出现了分布式配置中心组件。市面上开源的配置中心有很多,360的QConf、淘宝的diamond、百度的disconf都是解决这类问题。国外也有很多开源的配置中心Apache的Apache Commons Configuration等。SpringCloud中选用的是SpringCloud Config。

  SpringCloud Config分为Config Server和Config Client两部分,为分布式系统外部化配置提供了支持。 由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此SpringCloud Config非常适合Spring应用程序,当然也可与其他语言应用程序配合使用。

  Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置(开发,测试,生产,灰度),默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault存储配置),因此可以方便的实现对配置的版本控制与内容审计。 Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性。

SpringCloud Config带来的便利

1、集中管理配置,通过Config来对集群中所有组件服务的配置信息进行集中管理;

2、争对不同的环境进行不同的配置(开发,联调,测试,灰度,生产);

3、运行期间可动态调整,根据服务器的负载情况动态的设置连接池信息或者熔断阈值;

4、配置修改后,不需要关闭服务可自动更新配置;

SpringCloud Config入门

1、申请一个自己的git仓库,将测试项目得yml或properties文件上传至Git目录;

2、构建Config Service

<!-- 、引入Jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <!-- 、配置属性信息 -->
server:
port:
spring:
application:
name: ms-cfg-service
cloud:
config:
server:
git:
uri: https://gitee.com/******/springcloudconfig.git
username: ******
password: ****** <!-- 3、加注解@EnableConfigServer -->
@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}

备注:配置文件有三种访问方式,分别是:

1)通过application-{profiles}.yml来访问,eg:http://localhost:8080/application-dev.yml

2)通过/application/{profiles}/{lable}来访问, eg:http://localhost:8080/application/dev/master

3)通过/{lable}/application-{profiles}.yml来访问,eg:http://localhost:8080/master/application-dev.yml

3、构建Config Client

<!-- 1. 引入Jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> <!-- 2. 创建配置文件bootstrap.yml -->
spring:
application:
name: application
cloud:
config:
uri: http://localhost:8080/
profile: dev
label: master

备注: spring.application.name 对应访问规则中的{application}

    spring.cloud.config.profile 对应访问规则中的{profiles}

    spring.cloud.config.label 对应访问规则中的{lable}

SpringCloud config的常规用法

我们定义一个openTest开关,来控制业务逻辑代码走新的逻辑分支还是走老的业务逻辑分支

方法一:

<!-- 1、在属性文件中定义一个变量 -->
ycdhz.openTest=dev <!-- 2、在代码中通过@Value注解引用 -->
@Value("${ycdhz.openTest}")
private String openTest; public void findInfo(){
if(openTest.equal("dev")){
System.out.print("开发环境")
} else if (openTest.equal("test")){
System.out.print("测试环境")
} else {
System.out.print("生产环境")
}
} <!-- 3、修改属性文件,重启生效-->

方法二:

<!-- 1、再Client端工程,引入jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- 2、再Client端工程,开启refresh的监控端点 -->
management:
endpoints:
web:
exposure:
include: "*" 开启所有的端点 <!-- 3、在读取配置文件中的类上加入@RefreshScope -->
@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController { @Value("${openTest}")
private String openTest;
} <!-- 4、在git上修改openTest的配置文件 -->
通过Post请求,执行http://localhost:8001/actuator/refresh刷新接口

备注:不在需要重启,只需要通过Post执行刷新方法即可。但是当需要在集群中大面积修改的情况下依旧很繁琐,需要对每一个服务进行刷新。

方法三:

配置Config Client

<!-- 1、在client端工程,引入jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- 2、在client端工程,配置属性 -->
spring:
application:
name: application
cloud:
config:
uri: http://localhost:9000/
label: master
rabbitmq:
host: ****IP地址****
port: ****端口号****
virtual-host: ****host名****
username: root
password: root
connection-timeout: 10000
template:
mandatory: true
management:
endpoints:
web:
exposure:
include: "*"
server:
port: 8080

配置 config Service

<!-- 1、在Service端工程,引入jar包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- 2、在Service端工程,配置属性 -->
server:
port: 9000
spring:
application:
name: ms-cfg-service
cloud:
config:
server:
git:
uri: https://gitee.com/******/springcloudconfig.git
username: ******
password: ******
rabbitmq:
host: ****IP地址****
port: ****端口号****
virtual-host: ****host名****
username: root
password: root
connection-timeout: 10000
template:
mandatory: true <!-- 3、在Git上更新配置信息,访问bus-refresh刷新服务配置 -->
访问监控端点http://localhost:9000/actuator/bus-refresh刷新所有服务的配置信息

备注:使用消息总线bus来实现,不再需要去争对一个个服务组件做刷新。原理如图:

SpringCloud入门(十): Config 统一配置中心的更多相关文章

  1. SpringCloud学习之Config分布式配置中心(八)

    统一配置中心概述 如果微服务架构中没有使用统一配置中心时,所存在的问题: 配置文件分散在各个项目里,不方便维护 配置内容安全与权限,实际开发中,开发人员是不知道线上环境的配置的 更新配置后,项目需要重 ...

  2. springcloud-spring cloud config统一配置中心

    统一配置中心 为什么需要统一配置中心? 统一配置中心顾名思义,就是将配置统一管理,配置统一管理的好处是在日后大规模集群部署服务应用时相同的服务配置一致,日后再修改配置只需要统一修改全部同步,不需要一个 ...

  3. Spring Cloud第十篇 | 分布式配置中心Config

    ​ 本文是Spring Cloud专栏的第十篇文章,了解前九篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  4. SpringCloud初体验:二、Config 统一配置管理中心

    Spring Cloud Config : 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. 配置中心也区分为服务端和客户端,本次体 ...

  5. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

  6. Spring Cloud之——Config(配置中心)

    Spring Cloud Config(配置中心) 大家好,有一段时间没有写技术博客了.由于工作上的事情,这方面很难分配时间.近几年随着服务化的兴起,一批服务化的框架应运而生,像dubbo,thrif ...

  7. 基于ZK构建统一配置中心的方案和实践

    背景: 近期使用Zk实现了一个简单的配置管理的小东西,在此开源出来,有兴趣的希望提出您的宝贵意见.如果恰巧您也使用或者接触过类似的东西, 也希望您可以分享下您觉得现在这个项目可以优化和改进的地方. 项 ...

  8. .NET Core微服务之基于Apollo实现统一配置中心

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.关于统一配置中心与Apollo 在微服务架构环境中,项目中配置文件比较繁杂,而且不同环境的不同配置修改相对频繁,每次发布都需要对应修改 ...

  9. Spring Cloud实战之初级入门(五)— 配置中心服务化与配置实时刷新

    目录 1.环境介绍 2.配置中心服务化 2.1 改造mirco-service-spring-config 2.2 改造mirco-service-provider.mirco-service-con ...

随机推荐

  1. Mycat的简介及安装

    Mycat简介: 1.1Mycat含义 简单的说,MyCAT就是: 一个彻底开源的,面向企业应用开发的“大数据库集群” 支持事务.ACID.可以替代Mysql的加强版数据库 一个可以视为“Mysql” ...

  2. Springboot使用Undertow

    Springboot使用Undertow Undertow 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式服务器 Undertow的特点: 轻量级:它是一个 Web 服务器,但不像传统的 ...

  3. Python-hashlib、OS、Random、sys、zipfile模块

    # print(sys.version) #python 版本 # print(sys.path) # print(sys.platform) #当前什么系统 # print(sys.argv) #当 ...

  4. Linux下反弹shell笔记

    0x00 NC命令详解 在介绍如何反弹shell之前,先了解相关知识要点. nc全称为netcat,所做的就是在两台电脑之间建立链接,并返回两个数据流 可运行在TCP或者UDP模式,添加参数 —u 则 ...

  5. 我的Keras使用总结(3)——利用bottleneck features进行微调预训练模型VGG16

    Keras的预训练模型地址:https://github.com/fchollet/deep-learning-models/releases 一个稍微讲究一点的办法是,利用在大规模数据集上预训练好的 ...

  6. 为什么你的程序配了classpath还是找不到类

    classpath简介 classpath是java程序时拥有的一个系统变量,这个变量可以通过如下方式获取 System.out.println(System.getProperty("ja ...

  7. MATLAB中mean的用法

    https://blog.csdn.net/wangyang20170901/article/details/78745587 MATLAB中mean的用法 转载仙女阳 最后发布于2017-12-07 ...

  8. 一文看懂神经网络初始化!吴恩达Deeplearning.ai最新干货

    [导读]神经网络的初始化是训练流程的重要基础环节,会对模型的性能.收敛性.收敛速度等产生重要的影响.本文是deeplearning.ai的一篇技术博客,文章指出,对初始化值的大小选取不当,  可能造成 ...

  9. python—异常处理try

    写代码的时候会遇到各种各样的异常,那么代码就不会继续往下走了.比如说10除以0是错误的,因为除数不能为零学会捕捉异常,在异常出现的时候我们要做什么操作. 捕捉异常 d={} #例如定义字典 try: ...

  10. SpringBoot系列(二)入门知识

    SpringBoot系列(二)入门知识 往期推荐 SpringBoot系列(一)idea新建springboot项目 引言 本来新建springboot项目应该放在入门知识这一章的,但是由于新建spr ...