Spring Cloud7-Config配置中心

1.概述

1.1 微服务面临的配置问题

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。
由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题

1.2 SpringCloud Config

1.2.1 概述

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

1.2.2 分类

SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口.
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

1.2.3 优缺点

集中管理配置文件

不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release

运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息

当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置

将配置信息以REST接口的形式暴露

1.2.4 推荐与Git整合

由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),
但最推荐的还是Git,而且使用的是http/https访问的形式

2.Config服务端

2.1 pom

	<!-- springCloud Config -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>

2.2 yml

spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
         ##gitlab或github的等git仓库的地址
          uri: [email protected]:mygithub/microservicecloud-config.git 

2.3 main

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

2.4 访问格式

2.4.1 /{application}-{profile}.yml

http://config.xxxx.com/application-dev.yml  
http://config.xxxx.com/application-test.yml  
http://config.xxxx.com/application-xxx.yml(不存在的配置)  

2.4.2 /{application}/{profile}[/{label}]

http://config.xxxx.com/application/dev/master  
http://config.xxxx.com/application/test/master  
http://config.xxxx.com/application/xxx/master  

2.4.3 /{label}/{application}-{profile}.yml

http://config.xxxx.com/master/application-dev.yml  
http://config.xxxx.com/master/application-test.yml  

3.Config客户端

3.1 bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
      profile: test   #本次访问的配置项
      label: master   
      #指向的本次config服务的地址
      uri: http://config.xxxx.com/ 
 


– 总结于网络视频及网络文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章