Spring Cloud Config

    xiaoxiao2021-03-25  74

    为什么要统一管理配置?

    1、集中管理 2、不同环境不同配置 3、运行期间动态调整配置 4、自动刷新

    简介

    Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Cleint都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud非常适合Spring应用程序,当然也可与任何其他语言编写的应用 程序配合使用。

    Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault存储配置),因此可以方便的实现对配置的版本控制与内容审计。

    Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性。

    结构图

    实战Config Server

    在pom文件中添加依赖:spring-cloud-config-server

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

    在启动类上添加@EnableConfigServer注解

    @SpringBootApplication @EnableConfigServer public class SpringCloudConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigApplication.class, args); } }

    配置application.properties

    Spring Cloud Config Server从git仓库(必须提供)中提取远程客户端的配置:

    server.port=8080 # github管理配置 spring.cloud.config.server.git.uri=https://github.com/isosweet/Config-File.git

    更多的配置项:

    #配置git仓库位置 spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringBoot-Learning/ #配置仓库路径下的相对搜索位置,可以配置多个 spring.cloud.config.server.git.searchPaths=Chapter9-1-4/config-repo #访问git仓库的用户名 spring.cloud.config.server.git.username=username #访问git仓库的用户密码 spring.cloud.config.server.git.password=password

    启动应用访问:http://localhost:8080/database/dev

    在github上创建一个仓库,上传提交多个配置文件,比如:

    然后访问启动的项目,通过URL路径匹配相应的配置文件,URL与配置文件的映射关系如下:

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



    Spring Cloud Config Client

    客户端读取SpringCloud Config Server 中的配置信息

    1、添加Client依赖

    <!-- SpringCloud Config Client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>

    2、添加配置项

    spring.application.name=database spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:8080/

    配置项说明:

    spring.application.name:对应前配置文件中的{application}部分 spring.cloud.config.profile:对应前配置文件中的{profile}部分 spring.cloud.config.label:对应前配置文件的git分支 spring.cloud.config.uri:配置中心的地址

    3、从ConfigServer中读取配置项

    @RestController @RequestMapping("/config") public class ConfigClientController { @Value("${driver}") private String jdbcDriver; @Value("${url}") private String jdbcUrl; @Value("${username}") private String jdbcUsername; @Value("${password}") private String jdbcPassword; @GetMapping("/getDataBaseProp") public String getDataBaseProp(){ return jdbcDriver + "---" + jdbcUrl + "---" + jdbcUsername + "---" + jdbcPassword; }

    在应用启动的时候抛异常:

    Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘jdbc.driver’ in value “${jdbc.driver}”

    原因是:

    Spring Cloud应用程序通过创建“引导”上下文来运行,上下文是主应用程序的父上下文。 开箱即用,负责从外部源加载配置属性,还解密本地外部配置文件中的属性。 这两个上下文共享一个环境,它是任何Spring应用程序的外部属性的源。 Bootstrap属性的优先级高,因此默认情况下不能被本地配置覆盖。

    引导上下文使用与主应用程序上下文不同的定位方式来定位外部配置,因此您可以使用bootstrap.yml代替application.yml(或.properties),保持引导和主上下文的外部配置分开。


    /{application}-{profile}.properties 访问方式:

    转载请注明原文地址: https://ju.6miu.com/read-33837.html

    最新回复(0)