编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

SpringConfig配置中心学习总结(spring 配置中心原理)

wxchong 2024-10-28 20:05:30 开源技术 32 ℃ 0 评论

01

前言

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

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

02

简单介绍

SpringCloud-config Server的maven依赖

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

解决方案架构示意图


03

Config-Server配置中心示意图

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

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

spring:
  application:
    name: config-center
  cloud:
    config:
      enabled: true
      server:
        git:
          uri: http://172.2*.4.*0/e**s/application-config.git
          username: **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是必须首先开发和部署的模块,并且应与其他业务模块分离单独出来。部署好之后,才能开发和部署其他子模块项目。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表