第十三章:SpringCloud Config Client的配置

本文介绍如何使用 Spring Cloud Config 实现配置中心,并详细解释了配置文件的正确放置位置、必要的依赖项以及如何通过 Controller 获取配置信息。此外,还探讨了如何利用 Git 提升配置管理的效率和安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This is the default behaviour for any application which has the Spring Cloud Config Client on the classpath. When a config client starts up it binds to the Config Server (via the bootstrap configuration property spring.cloud.config.uri) and initializes Spring Environment with remote property sources. The net result of this is that all client apps that want to consume the Config Server need a bootstrap.yml (or an environment variable) with the server address in spring.cloud.config.uri (defaults to "http://localhost:8888").

文档大意: configclient 服务启动后,默认会先访问bootstrap.yml,然后绑定configserver,然后获取application.yml 配置。如果仅仅在application.yml 配置了url:http://127.0.0.1:8080 这样默认会使用8888端口,(配置无效)。 所以, #####1. 我们将绑定configserver的配置属性应该放在bootstrap.yml文件里。

#####2. pom

            <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
复制代码

因为用到mvc,所以添加web依赖 #####3.application

#####4. 写一个获取配置信息的controller
#####5. 测试是否连接上server

####注意: 一般来说git上放的yml名称都以 ${application.name}-dev.yml 命名。 因为:如果client服务没有声明application.name的话,如果去configserver里拉取配置,cloud会默认拉取application.yml。 当然:你按照规范,给application.name:laojiao 然后git上放个对应配置文件laojiao.yml 或者 laojiao-xxxxx.yml 注意命名唯一,这样client就能找到对应的配置文件。否则默认去找application.yml ##高性能配置: ###一、git repo 仓库设置 (将上面的替换。隔离性高)Placeholders in Git URI

or a "one repo per profile" policy using a similar pattern but with {profile}.

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/myorg/{application}
复制代码

意思是: 一个项目 创建一个专门的git仓库,该仓库里只存放对应服务名称的配置文件。 例如: 我有个laojiao和tiantian模块,我就创建两个 git仓库,一个叫 laojiao,一个叫tiantian,然后在模块里的spring.application.name 就是laojiao / tiantian。最后,在config server里的配置文件git: uri: https://github.com/myorg/{application} 这个{application}通配符会自动获取模块对应的仓库去获取配置文件。 ###二、Placeholders in Git Search Paths 获取一个git仓库下的文件夹名称,来找配置

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: '{application}'
复制代码

https://github.com/spring-cloud-samples/config-repo路径下,找对应的laojiao / tiantian 文件夹,然后获取配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值