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

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

#application.yml文件内容
spring:
  profiles:
    active: 
    - dev
---
spring:
  profiles: dev
  application:
    name: application-dev
---
spring:
  profiles: test
  application:
    name: application-test
#config-test.properties文件内容
name=spring-config-test

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

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

Spring Cloud Config服务端

  • 引用pom文件
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
  • 需要在启动类上加 @EnableConfigServer 注解
@SpringBootApplication
@EnableConfigServer
public class DemoConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoConfigServerApplication.class, args);
    }
}
  • 在application.yml中配置Git仓库地址
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Lailaimonkey/code.git  #配置git地址
          #username:    # git仓库的账号(公开仓库无需账号信息)
          #passphrase:   # git仓库的密码(公开仓库无需账号信息)
          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。

{"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配置文件信息

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

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

name: config-git-config-dev
spring:
  application:
    name: application-dev
  profiles:
    active:
    - dev

Spring Client Config客户端

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

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

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

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String profile(){
        return this.profile;
    }

}

大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值