Spring Cloud-Config Server 分布式配置中心

约定不等于承诺〃 2022-05-21 08:16 256阅读 0赞

文章目录

  • Spring Cloud-Config Server 分布式配置中心
        • 1.Config Server基本使用
          • pom.xml
          • ConfigServerApp.java
          • application.yml
        • 2.Config Client基本使用
          • pom.xml
          • application.yml
          • bootstrap.yml
          • ConfigClientApp.java
          • ConfigClientController .java
        • 3.Config通配符模式
          • application.yml
        • 4.Config模式匹配
          • application.yml
        • 5.Config搜索路径配置
          • bootstrap.yml
        • 6.Config Server整合认证
          • pom.xml
          • application.yml
          • ConfigServerAuthApp.java

Spring Cloud-Config Server 分布式配置中心

 随着我们接触微服务、分布式系统这些概念和应用后,由于每个功能模块都能拆分为一个独立的服务,服务数量不断增加。一次业务可能需要多个服务来共同协调才能完成,为了方便服务配置文件统一管理以及维护,我们就需要用到分布式配置中心组件,Spring Cloud提供了Spring Cloud Config Server分布式配置中心组件,如果大家使用过zookeeper去做分布式配置文件或者disconf之类的就变得十分容易理解了。
 Spring Cloud Config Server支持配置放在配置服务的内存中,也支持将其放在远程Git仓库中,而在Spring Cloud提供的分布式配置服务中主要包含两个角色Config Server和Config Client。我们可以将公用的外部配置文件集中放在一个Git仓库中,然后建立一个Config Server来管理所有的配置文件,维护配置文件的时候只需要修改该Git仓库中的配置文件,其他所有服务都可以作为Config Client通过Config Server来获取所需配置文件,当然所有服务都通过同一个服务中心获取配置文件,那么依赖性就相对增大,若服务中心挂了之后其他服务都不能正常使用,所以这里是支持集群化部署的。

 在查阅资料的时候看到一张架构图,大家可以参考理解下
这里写图片描述

1.Config Server基本使用

 首先新建一个项目用于做配置中心服务使用,添加所需依赖

pom.xml
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-server</artifactId>
  4. <version>1.4.3.RELEASE</version>
  5. </dependency>

 我们只要在主程序上添加@EnableConfigServer注解,就可以使该服务变为配置服务

ConfigServerApp.java
  1. package com.ithzk.spring.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. /** * Hello world! * */
  6. @SpringBootApplication
  7. @EnableConfigServer
  8. public class ConfigServerApp
  9. {
  10. public static void main( String[] args )
  11. {
  12. SpringApplication.run(ConfigServerApp.class);
  13. }
  14. }

 这里我们把配置文件放在远程Git上,为了方便这里我暂用码云,然后将项目地址配置到我们该服务中

application.yml
  1. server:
  2. port: 12900
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://gitee.com/onethree/spring-cloud-config #配置文件的仓库地址

 我们在码云上新建一个项目,添加一个application.yml的配置文件
这里写图片描述
这里写图片描述
 完成以上步骤后启动服务,访问localhost:12900/abc-default.properties或者localhost:12900/abc-default.yml,由于abc-default.yml文件不存在,config server会默认去获取application.yml的配置。
这里写图片描述
 我们再新增一个config-dev.yml文件。
这里写图片描述
这里写图片描述
 无需重启,访问localhost:12900/abc-default.yml
这里写图片描述
 我们可以发现如果你获取的配置文件不存在则会自动获取默认的配置文件,如果存在则会将默认的和本身存在的一起获取,如果属性名称相同,指定文件中的属性会覆盖默认文件中的属性,例如此处abc。我们还可以通过访问localhost:12900/配置文件名称/default/master查看该配置文件名可获取对应的配置文件。
这里写图片描述
这里写图片描述

2.Config Client基本使用

 上面我们搭建了一个Config server配置中心,当我们服务需要使用配置中心里面的配置时该服务就可以被看作是一个config client,下面给一个小示例来简单介绍下如何是使用config client去获取config server里我们所需的配置文件。

pom.xml
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>ch.qos.logback</groupId>
  7. <artifactId>logback-classic</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>ch.qos.logback</groupId>
  11. <artifactId>logback-core</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-config</artifactId>
  16. <version>1.4.3.RELEASE</version>
  17. </dependency>
application.yml
  1. server:
  2. port: 13900
bootstrap.yml
  1. spring:
  2. cloud:
  3. config:
  4. uri: http://localhost:12900 # config-server地址,配置server相关的属性应该放到 bootstrap.yml 中,bootstrap.yml会在加载application.yml之前加载
  5. profile: dev
  6. label: master #当使用 git 的时候 lable 默认是 master
  7. application:
  8. name: config #获取name-profile对应的文件,会获以config开头dev的文件
ConfigClientApp.java
  1. package com.ithzk.spring.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /** * Hello world! * */
  5. @SpringBootApplication
  6. public class ConfigClientApp
  7. {
  8. public static void main( String[] args )
  9. {
  10. SpringApplication.run(ConfigClientApp.class);
  11. }
  12. }
ConfigClientController .java
  1. package com.ithzk.spring.cloud.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /** * @author hzk * @date 2018/7/4 */
  6. @RestController
  7. public class ConfigClientController {
  8. @Value("${profile}")
  9. private String profile;
  10. @GetMapping("getProfile")
  11. public String getProfile(){
  12. return profile;
  13. }
  14. }

 访问接口localhost:13900/getProfile会从config server配置的远程仓库中获取对应名称的配置文件。
这里写图片描述

3.Config通配符模式

 之前我们配合的config server是从指定的spring-cloud-config仓库中获取配置文件,当我们需要从多个仓库中获取的时候,我们可以利用Config Server提供的通配符配置模式去配置,这里我们建好三多个仓库并放好配置文件。
这里写图片描述
这里写图片描述
 如何让我们Config Server同时可以从多个仓库中获取配置文件呢,这里需要修改一下配置,将之前指定仓库改为{application}。

application.yml
  1. server:
  2. port: 12900
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://gitee.com/onethree/{ application}

 通过ip:port/仓库名称-dev.properties(yml)即可从获取指定仓库中所有配置属性。
这里写图片描述
这里写图片描述

4.Config模式匹配

 上面我们使用通配符模式可以使一个Config Server同时连接多个仓库获取配置文件,我们还可以设置模式匹配使特定仓库配置文件具有特定规则才可以获取。

application.yml
  1. server:
  2. port: 12900
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://gitee.com/onethree/spring-cloud-config # 默认配置
  9. repos:
  10. simple:
  11. uri: https://gitee.com/onethree/simple # 当访问的是 simple 的时候执行
  12. special:
  13. uri: https://gitee.com/onethree/special # 当访问的是 special 并且符合以下规则的时候请求的 uri
  14. pattern: special*/dev*,special*/test*

 这里我们可以看到只要符合simple前缀都可以从simple仓库中获取配置文件。
这里写图片描述
这里写图片描述
 但是我们special配置了特定规则访问,只有符合规则才可以获取special仓库内容,否则获取默认配置仓库内容。
这里写图片描述
这里写图片描述

5.Config搜索路径配置

 如果我们想在仓库里面建不同文件夹去管理不同配置文件,可以实现后获取吗?这里,我们先建一个searchabc文件夹然后新建一个配置文件。
这里写图片描述
 通过访问路径获取可以发现获取的是默认的配置文件,也就是说无法获取到指定文件夹里配置文件内容
这里写图片描述
 那么我们需要怎么去获取指定目录下配置文件呢,只需增加配置

bootstrap.yml
  1. server:
  2. port: 12900
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://gitee.com/onethree/spring-cloud-config # 默认配置
  9. search-paths:
  10. - searchabc # 以 searchabc 开头的会被转到 searchabc 目录
  11. clone-on-start: true # 启动就加载文件
  12. #username: 读时可以不设但是写时建议设置
  13. #password:

这里写图片描述

 通过以上配置,我们就可以达到从指定目录下获取配置文件的效果,这里还可以配置用户名和密码控制安全性。

6.Config Server整合认证

 我们可以整合spring-boot提供的安全配置达到config server实现认证

pom.xml
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-server</artifactId>
  4. <version>1.4.3.RELEASE</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-security</artifactId>
  9. </dependency>
application.yml
  1. server:
  2. port: 12900
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://gitee.com/onethree/spring-cloud-config # 默认配置
  9. search-paths:
  10. - searchabc # 以 searchabc 开头的会被转到 searchabc 目录
  11. clone-on-start: true
  12. security:
  13. user:
  14. name: configserver
  15. password: configserver
  16. basic:
  17. enabled: true
ConfigServerAuthApp.java
  1. package com.ithzk.spring.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. /** * Hello world! * */
  6. @SpringBootApplication
  7. @EnableConfigServer
  8. public class ConfigServerAuthApp
  9. {
  10. public static void main( String[] args )
  11. {
  12. SpringApplication.run(ConfigServerAuthApp.class);
  13. }
  14. }

 启动服务之后访问发现需要认证才可获取配置。
这里写图片描述
这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 分布式配置

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud

    相关 Spring Cloud 分布式配置

    什么是配置中心 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心