第十三章:SpringCloud Config Client的配置

简介: This is the default behaviour for any application which has the Spring Cloud Config Client on the classpath.

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文件里。
img_96510fc485af69dd935dea59f7d768f9.png
image.png
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
img_ab141cbeaed7fac0bde94f7c593276d5.png
image.png
4. 写一个获取配置信息的controller
img_caafa59fc351e41043da9bb97647b7c0.png
xxx
5. 测试是否连接上server
img_0d91a0fceebabf89113449a3e89e028e.png
image.png

img_16573734b035996c40f90239fdb3bfe7.png
image.png

img_8df5ccec4c859884ff81f5ae3ce4362e.png
image.png

注意:

一般来说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 文件夹,然后获取配置文件

相关文章
|
8天前
|
JavaScript
vue.config.ts配置环境变量
vue.config.ts配置环境变量
14 0
|
16天前
|
SpringCloudAlibaba Java Nacos
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
|
23天前
Springcloud-ribbon和hystrix配置
Springcloud-ribbon和hystrix配置
7 0
|
1月前
|
移动开发 监控 小程序
mPaaS常见问题之uniapp ios端云打包的配置config文件如何解决
mPaaS(移动平台即服务,Mobile Platform as a Service)是阿里巴巴集团提供的一套移动开发解决方案,它包含了一系列移动开发、测试、监控和运营的工具和服务。以下是mPaaS常见问题的汇总,旨在帮助开发者和企业用户解决在使用mPaaS产品过程中遇到的各种挑战
26 0
|
1月前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
783 0
|
1月前
|
SpringCloudAlibaba Dubbo 前端开发
【三】SpringCloud Alibaba之Nacos整合篇(作为配置中心)
【三】SpringCloud Alibaba之Nacos整合篇(作为配置中心)
233 0
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【二】SpringCloud Alibaba之Nacos整合篇(配置负载均衡)
【二】SpringCloud Alibaba之Nacos整合篇(配置负载均衡)
257 0
|
1月前
|
开发工具 git 微服务
【二十三】搭建SpringCloud项目六(Config)配置中心动态刷新
【二十三】搭建SpringCloud项目六(Config)配置中心动态刷新
19 0
|
1月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
200 0
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
145 0