Spring Cloud Config

为什么要统一管理配置?

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 访问方式:

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值