SpringCloud Config 公共配置文件的使用

2022-07-06 09:35:34

前言:

在SpringCloud中,我们一般会是SpringCloud Config作为配置中心,管理所有服务的配置信息,方便配置信息的管理与维护。但是随着微服务的逐渐增多,你会发现每一个服务都会有相同的配置文件。如果不对它们进行整合,势必会对配置信息的维护带了一定的烦恼。所以,我们这里将介绍一种配置公共文件的方法,具体如下文。

正文:

这种方法实现起来非常简单,我需要在Config配置中心的目录下,新建common-[profile_name].yml(例如:common-dev.yml),可以在该文件下配置一些公用配置信息,例如:

#公用配置
##-----------------------Spring配置---------------------
spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
  ###-----------------------Sleuth分布式跟踪配置---------------------
  sleuth:
    sampler:
      percentage: 1.0 #指定需要采样的请求的百分比,默认值是0.1(10%)。
  ###-----------------------Zipkin链路追踪配置---------------------
  zipkin:
    base-url: http://hanxiaozhang-zipkin-server/
  ###-----------------------Springboot admin 配置---------------------
  boot:
    admin:
      client:
        url:  http://localhost:8018
        username: user
        password: user

##-----------------------actuator健康检查配置---------------------
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

 然后在每一个微服务的bootstrap.yml中配置如下信息,就可以实现公共配置文件的取读了。

spring:
  application:
    name: hanxiaozhang-user-oauth2
  cloud:
    config:
      fail-fast: true  # 获取不到远程配置时,立即失败
      name: ${spring.application.name},common  # 配置公用的common
      profile: ${spring.profiles.active}
      label: master
      discovery:
        enabled: true
        service-id: hanxiaozhang-config

控制台输出日志如下:

2020-10-28 18:49:20.009  INFO [hanxiaozhang-zuul,,,] 37188 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=hanxiaozhang-zuul,common, profiles=[dev], label=master, version=5677f5faedcf827c4fe302a4885a165c52427c47, state=null
2020-10-28 18:49:20.010  INFO [hanxiaozhang-zuul,,,] 37188 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/hanxinghua2017/hanxiaozhang//config-repo/common-dev.yml'}, MapPropertySource {name='https://gitee.com/hanxinghua2017/hanxiaozhang//config-repo/hanxiaozhang-zuul-dev.yml'}]}

  • 作者:hanxiaozhang2018
  • 原文链接:https://blog.csdn.net/huantai3334/article/details/109339547
    更新时间:2022-07-06 09:35:34