Spring Cloud:Config配置

    技术2022-07-12  72

    简介:

    Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

    Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。 使用Config Server,您可以集中管理所有环境中应用程序的外部属性。 客户端和服务器上的概念与Spring Environment和PropertySource抽象完全相同,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。 当应用程序从开发人员迁移到测试人员并进入生产过程时,您可以管理这些环境之间的配置,并确保应用程序具备迁移时所需的一切。 服务器存储后端的默认实现使用git,因此它轻松支持配置环境的标记版本,并且可用于管理内容的各种工具。 添加替代实现并将其插入Spring配置很容易。

    Features

    Spring Cloud Config Server features:

    HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)Encrypt and decrypt property values (symmetric or asymmetric)Embeddable easily in a Spring Boot application using @EnableConfigServer

    Config Client features (for Spring applications):

    Bind to the Config Server and initialize Spring Environment with remote property sourcesEncrypt and decrypt property values (symmetric or asymmetric)

    特征


    Spring Cloud Config Server功能:

    HTTP,用于外部配置的基于资源的API(名称-值对,或等效的YAML内容)

    加密和解密属性值(对称或非对称)

    使用@EnableConfigServer可轻松嵌入到Spring Boot应用程序中

    Config Client功能(用于Spring应用程序):

    绑定到Config Server并使用远程属性源初始化Spring Environment

    加密和解密属性值(对称或非对称)


    环境准备:在自己GitHub上新建一个Repository作为config center;注册中心使用的consul

    命名规范:lable 分支名,application 应用名(自定),profile 如:dev、test、prod
    /{lable}/{application}-{profile}.yml/{application}-{profile}.yml/{application}/{profile}/{lable}
    如:

    Config Center Server

    POM

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

    YAML

    lable:分支名(branch)uri:gitclone地址search-paths:搜索路径 server: port: 3344 spring: application: name: cloud-config-center cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name} prefer-ip-address: true config: server: git: uri: https://github.com/DeadAndLive/springcloud-config.git search-paths: - springcloud-config label: master

    启动类

    加上 @EnableConfigServer注解
    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class CloudConfigCenter3344Application { public static void main(String[] args) { SpringApplication.run(CloudConfigCenter3344Application.class, args); } }

    Config Client

    POM

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

    YAML必须为bootstrap.yml

    Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。**初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。**这两个上下文共享一个从外部获取的Environment。Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。

    management.endpoints.web.exposure.include:暴露监控端口,供config动态刷新 当Config Client需要刷新时,需要以POST发送刷新请求。如: curl -X POST "http://localhost:3355/actuator/refresh"

    server: port: 3355 spring: application: name: cloud-config-client cloud: consul: host: localhost port: 8500 discovery: prefer-ip-address: true service-name: ${spring.application.name} config: label: master name: application profile: dev uri: http://localhost:3344 management: endpoints: web: exposure: include: "*"

    Controller

    @RefreshScope

    是spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载。

    import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping(value = "/configInfo") public String getConfigInfo() { return configInfo; } }
    这里的{config.info}就是

    测试:http://localhost:3355/configInfo

    Processed: 0.012, SQL: 10