SpringCloud--Config Server配置中心学习总结_springcloudconfig原理总结

📅 2026/7/3 6:33:31 👁️ 阅读次数 📝 编程学习
SpringCloud--Config Server配置中心学习总结_springcloudconfig原理总结

前言

使用分布式微服务开发和部署项目,其中的一个优势就是:使用分布式配置协调服务,达到统一配置存储和使用。

比如百度的disconf,阿里的diamand。都是不错的分布式统一配置框架,SpringCloud-config Server则是SpringCloud系列下的分布式统一配置组件服务。

简单介绍

SpringCloud-config Server的maven依赖

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

解决方案架构示意图

Config-Server配置中心示意图

Spring cloud config是将配置保存在远程服务gitlab/git/svn进行托管,小编这里用的是gitlab,其用法和结构原理跟git/github用起来差不多。

另一篇文章介绍:SpringCloud分布式配置中心浅谈

项目中的gitlab配置信息如下:

spring:application:name: config-centercloud:config:enabled: trueserver:git:uri: http://172.2*.4.*0/e**s/application-config.gitusername: **s # git账户用户password: **s1234 #git账户密码search-paths: /config-s*o,/config-t*s,/config-c*,/config-h*s...# git项目中子目录

git.uri就是配置的gitlab的地址,以及usrname和password信息。search-paths是各子系统的配置对应文件目录。

configServer项目启动的时候,Spring就会去读取gitlab连接和配置信息,并创建连接以读取各个子项目的配置信息。

当然,configServer也要自己在ereka中进行服务注册,以供其他子模块服务可以访问和消费信息。

eureka:client:service-url:defaultZone: http://17*.2*.4.*0:8000/eureka/

项目中的注解

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

其中:@EnableConfigServer就是标注本项目实例,是ConfigServer的实例。结合上面的@Configuration和@EnableAutoConfiguration自动加载配置注解,Spring就会去自动加载配置信息。例如上面所说的连接gitlab等。

 

启动之后,configServer经由ereka服务注册和发现,供其他子模块系统消费。所以,configServer是必须首先开发和部署的模块,并且应与其他业务模块分离单独出来。部署好之后,才能开发和部署其他子模块项目。

 

 

同名原创公众号:程序大视界