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

- 日理万妓 2021-12-08 21:21 500阅读 0赞

Spring Cloud Config(配置中心)

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以为所有环境中的应用程序管理其外部属性。它非常适合spring应用,也可以使用在其他语言的应用上。随着应用程序通过从开发到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。

Spring Cloud Config服务端特性

  • HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
  • 属性值的加密和解密(对称加密和非对称加密)
  • 通过使用@EnableConfigServer在Spring boot应用中非常简单的嵌入。

Config客户端的特性(特指Spring应用)

  • 绑定Config服务端,并使用远程的属性源初始化Spring环境。
  • 属性值的加密和解密(对称加密和非对称加密)

将配置文件上传到git服务器上

小编一共上传了三个文件,在根目录上传两个application.yml和config-test.properties

  1. #application.yml文件内容
  2. spring:
  3. profiles:
  4. active:
  5. - dev
  6. ---
  7. spring:
  8. profiles: dev
  9. application:
  10. name: application-dev
  11. ---
  12. spring:
  13. profiles: test
  14. application:
  15. name: application-test
  16. #config-test.properties文件内容
  17. name=spring-config-test

要根目录新建config-git文件夹,文件夹里新建config-dev.yml

  1. #config-dev.yml文件内容
  2. name: config-git-config-dev

Spring Cloud Config服务端

  • 引用pom文件


    org.springframework.cloud
    spring-cloud-config-server
  • 需要在启动类上加 @EnableConfigServer 注解

    @SpringBootApplication
    @EnableConfigServer
    public class DemoConfigServerApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(DemoConfigServerApplication.class, args);
    3. }

    }

  • 在application.yml中配置Git仓库地址

    spring:
    application:

    1. name: config-server

    cloud:

    1. config:
    2. server:
    3. git:
    4. uri: https://github.com/Lailaimonkey/code.git #配置git地址
    5. #username: # git仓库的账号(公开仓库无需账号信息)
    6. #passphrase: # git仓库的密码(公开仓库无需账号信息)
    7. search-paths: config-git # git仓库地址下的相对搜索地址(可用使用通配符),可以配置多个,用,分割。 此配置加载根目录和config-git目录

    server:
    port: 8080

  • Config Server文件映射
    Config Server启动以后,我们可以通过它暴露的端点获取配置文件内容,http请求地址与配置文件映射关系如下:

    映射{application}-{profile}.properties文件

    /{ application}/{ profile}/[{ label}]
    /{ label}/{ application}-{ profile}.properties
    /{ application}-{ profile}.properties
    /{ label}/{ application}-{ profile}.yml
    /{ application}-{ profile}.yml

{application}通常使用微服务名称,对应Git仓库中文件名的前缀;
{profile}对应{application}-后面的dev、pro、test等;
{label}对应Git仓库的分支名,默认为master。

  • 启动server验证
    访问 http://localhost:8080/application/test 或 http://localhost:8080/application/dev 显示application.yml配置文件test或dev环境的信息,下面显示的test环境信息

    { “name”:”application”,”profiles”:[“test”],”label”:null,”version”:”99bc093f7fdc77012767b1ca58bba7675ef91360”,”state”:null,”propertySources”:[{ “name”:”https://github.com/Lailaimonkey/code.git/application.yml (document #2)”,”source”:{ “spring.profiles”:”test”,”spring.application.name”:”application-test”}},{ “name”:”https://github.com/Lailaimonkey/code.git/application.yml (document #0)”,”source”:{ “spring.profiles.active[0]”:”dev”}}]}

访问 http://localhost:8080/config-test.properties 显示config-test.properties配置文件信息

  1. name: spring-config-test
  2. spring.application.name: application-test
  3. spring.profiles.active[0]: dev

访问 http://localhost:8080/config-dev.yml 显示config-git文件夹下config-dev.yml信息

  1. name: config-git-config-dev
  2. spring:
  3. application:
  4. name: application-dev
  5. profiles:
  6. active:
  7. - dev

Spring Client Config客户端

  • 引用pom文件


    org.springframework.cloud
    spring-cloud-starter-config
  • Spring Boot应用程序启动时加载application.yml/application.properties。Spring Cloud中有“引导上下文”的概念,引导上下文加载bootstrap.yml/bootstrap.properties,而且具有更高的优先级,默认情况下bootstrap.yml/bootstrap.properties中的属性不能被覆盖。

  • 在application.yml同级目录下创建bootstrap.yml文件,内容为:

    spring:
    cloud:

    1. config:
    2. name: application #需要从github上读取资源名称,注意没有yml后缀名
    3. profile: dev #本次访问配置项
    4. label: master
    5. uri: http://localhost:8080 #本微服务启动后先去找8080服务,通过SpringCloudConfig
  • 编写测试用的Controller

    @RestController
    public class ConfigController {

    1. @Value("${profile}")
    2. private String profile;
    3. @GetMapping("/profile")
    4. public String profile(){
    5. return this.profile;
    6. }

    }

大功告成!

发表评论

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

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

相关阅读

    相关 Spring Cloud Config远程配置

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