【Spring Cloud 基础设施搭建系列】Spring Cloud Demo项目 使用Spring Cloud Config统一管理微服务配置

为什么要统一管理微服务配置

对于传统的单体应用,常使用配置文件管理所有配置。例如一个Spring Boot开发的单体应用,可将配置内容放在application.yml文件中。如果需要切换环境,可设置多个Profile,并在启动应用时指定spring.profles.active={profile}。当然也可借助Maven的Profile实现环境切换。
然而,在微服务架构中,微服务的配置管理一般有以下需求:

  • 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的。
  • 不同环境不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中是不同的。
  • 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阀值,并且在调整配置时不停止微服务。
  • 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。
    综上所述,对于微服务架构而言,一个通用的配置管理机制是必不可少的,常见做法是使用配置服务器管理配置。

Spring Cloud Config简介

Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment 和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容),因此可以很方便地实现对配置的版本控制与内容审计。

Config Client是Config Server的客户端,用于操作存储在Config Server中的配置属性。所有的微服务都指向 Config Server。各个微服务在启动时,会请求Config Server以获取所需要的配置属性,然后缓存这些属性以提高性能。
引入Spring Cloud Config后的架构如下:

在这里插入图片描述

编写Config Server

  1. 创建一个module,取名为cloud-config-server。
  2. 然后我们加入pom依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.cc.cloud</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>
  1. 编写启动类,在启动类上添加注解@EnableConfigServer,声明这是一个Config Server。
package com.cc.cloud.config.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApp.class, args);
    }
}

  1. 编写配置文件application.yml,并在其中添加以下内容。
server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cckevincyh/spring-cloud-demo.git
          search-paths: 'cloud-config'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

Spring Cloud Config 参数配置

  • spring.cloud.config.server.git.uri为github仓库地址
  • spring.cloud.config.server.git.search-paths为github配置文件路径
  • spring.cloud.config.label为设置config client请求的仓库分支
  • spring.cloud.config.server.git.username为github用户名
  • spring.cloud.config.server.git.password为github密码 github如果是私有仓库,必须填写用户名和密码
  • spring.cloud.config.server.git.default-label 配置config server默认请求的仓库分支
  • spring.cloud.config.server.git.skipSslValidation:配置服务器(Config Server)默认情况下会对 Git 服务器的 SSL 证书进行验证。可以通过设置为 true 来禁止验证。默认值为 false;

spring.cloud.config.labelspring.cloud.config.server.git.default-label的区别是什么?这一点我也有点疑惑,经过我实验之后总结如下:spring.cloud.config.label是配置在config client端的,默认值是master。spring.cloud.config.server.git.default-label是配置在config server端的,默认值应该也是master。如果配置了spring.cloud.config.server.git.default-labelv1.0,如果不带label参数去请求config server,默认访问的是v1.0的分支;如果config client不设置spring.cloud.config.label去访问config server,默认访问的也是v1.0的分支。

首先我在master分支上提交了一个配置文件test.yml,内容如下:

在这里插入图片描述

在config-serverf分支上也提交了一个配置文件test.yml,内容如下:

在这里插入图片描述

然后我的config-server的配置如下:

server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cckevincyh/spring-cloud-demo.git
          search-paths: 'cloud-config'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

然后当我访问:http://localhost:8090/test/xx 结果如下:

在这里插入图片描述

然后我更改spring.cloud.config.label为config-server,配置如下:

server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cckevincyh/spring-cloud-demo.git
          search-paths: 'cloud-config'
      label: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

然后当我重启后访问:http://localhost:8090/test/xx 结果如下:

在这里插入图片描述

所以两次的结果都是一样的,都是访问的是master分支的配置文件,说明spring.cloud.config.label在config server端配置无效的。

现在我们修改spring.cloud.config.server.git.default-label为config-server:

server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cckevincyh/spring-cloud-demo.git
          search-paths: 'cloud-config'
          default-label: config-server
      label: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

然后我们重启后访问:http://localhost:8090/test/xx ,最终结果如下:

在这里插入图片描述

结果可以发现访问的配置是config-seerver分支中的配置文件。所以说明spring.cloud.config.server.git.default-label的默认值应该是master,而且设置了spring.cloud.config.server.git.default-label就是配置了config server端默认请求的分支。

如果我们访问; http://localhost:8090/test/xx/master, 这里指定了分支是master,关于请求的URL匹配规则我后面会讲到,但是我这里设置的是请求master分支。结果如下:

在这里插入图片描述

说明当指定了分支的时候,请求的配置文件就是指定分支下的配置文件,如果没有指定分支的时候,访问的是spring.cloud.config.server.git.default-label中设置的分支,如果spring.cloud.config.server.git.default-label没有配置,则默认访问master分支的。

本地存储配置的方式

Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

示例如下:

server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        native:
          search-locations: file:C:\Users\c\Desktop\spring-cloud-demo\cloud-config
      label: config-server
  profiles:
    active: native
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

然后我再本地的C:\Users\c\Desktop\spring-cloud-demo\cloud-config路径下面创建了一个local.yml文件,内容如下:

spring-cloud:
  config:
    test: 'hello world -- local'

然后重启访问: http://localhost:8090/local/xx , 结果如下:

在这里插入图片描述

可以参考: Spring Cloud 探索 | 分布式配置中心(Config Server) 或者官网 Spring Cloud Config 或者Pivotal的官方文档 Configuring with Git

Config Server的端点

可以使用Config Server的端点获取配置文件的内容。端点与配置文件的映射规则如下:

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

以上端点都可以映射到{application}-{profile}.yml这个配置文件,{application}
表示微服务的名称,{label}对应Git仓库的分支,默认是master。

我提交了几个测试的配置文件在config-server分支,文件如下:

  • test.yml文件

在这里插入图片描述

  • test-dev.yml文件

在这里插入图片描述

  • test-prod.yml文件

在这里插入图片描述

然后我们的config-server配置如下:

server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cckevincyh/spring-cloud-demo.git
          search-paths: 'cloud-config'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
  • 测试/{application}/{profile}[/{label}]
  1. 我们访问 http://localhost:8090/test/dev/config-server

在这里插入图片描述

访问的是test-dev.yml和test.yml文件。

  1. 我们访问 http://localhost:8090/test/prod/config-server

在这里插入图片描述

访问的是test-prod和test.yml文件。

  • 测试/{label}/{application}-{profile}.yml
  1. 我们访问 http://localhost:8090/config-server/test-dev.yml

在这里插入图片描述

访问的是test-dev.yml和test.yml文件。

  1. 我们访问 http://localhost:8090/config-server/test-prod.yml

在这里插入图片描述

访问的是test-prod和test.yml文件。

  • 测试/{label}/{application}-{profile}.properties
  1. 我们访问 http://localhost:8090/config-server/test-dev.properties

在这里插入图片描述

访问的是test-dev.yml和test.yml文件,但是是properties文件格式的数据。

  1. 我们访问 http://localhost:8090/config-server/test-prod.properties

在这里插入图片描述

访问的是test-prod.yml和test.yml文件,但是是properties文件格式的数据。

Spring Cloud Config实现配置的"继承与"组合"

事实上,可使用Spring Cloud Config实现配置的"继承与"组合",
比如我们有一个应用:microservice-test ,其profile是dev,那么其实Spring Cloud Config会查找如下几个文件:

microservice-test-dev.yml
microservice-test.yml
application-dev.yml
application.yml

对于相同属性的配置,从上至下优先级逐渐递减;最终获得的配置属性是四个文件的组合。由此,不难分析,可如下规划几个配置文件:

  • microservice-test-dev.yml 作为指定应用在指定profile下的配置文件
  • microservice-test.yml 作为制定应用在任何profile下都通用的配置文件
  • application-dev.yml 作为所有应用在指定profile下的配置文件
  • application.yml 作为所有应用在任何profile下都通用的配置文件

测试示例:

在之前基础上加入两个配置文件,application.yml和application-dev.yml

首先是application-dev.yml:

在这里插入图片描述

然后是application.yml:

在这里插入图片描述
然后我们访问 http://localhost:8090/test/dev/config-server

在这里插入图片描述

可以发现最终获取的是如下的配置文件:

test-dev.yml
test.yml
application-dev.yml
application.yml

编写Config Client

我们改造之前的微服务 cloud-service-order和cloud-service-member。

  1. 首先加入pom的依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. 创建配置文件bootstrap.yml,分别在cloud-service-member和cloud-service-order中添加如下配置:
spring:
  application:
    #对应config server所获取的配置文件的{application}
    name: cloud-service-member
  cloud:
    config:
      # 配置中心服务地址
      uri: http://localhost:8090/
      # profile对应config server所获取的配置文件中的{profile}
      profile: dev
      #指定Git仓库的分支,对应config server所获取的配置文件的{label}
      label: config-client
spring:
  application:
    #对应config server所获取的配置文件的{application}
    name: cloud-service-order
  cloud:
    config:
      # 配置中心服务地址
      uri: http://localhost:8090/
      # profile对应config server所获取的配置文件中的{profile}
      profile: dev
      #指定Git仓库的分支,对应config server所获取的配置文件的{label}
      label: config-client

config client 配置说明

  • spring.cloud.config.name:配置中心{application}
  • spring.cloud.config.profile:对应配置中心的{profile}
  • spring.cloud.config.label:从指定分支读取配置文件
  • spring.cloud.config.uri:配置中心服务地址
  • spring.cloud.config.discovery.enabled:true开启配置服务发现
  • spring.cloud.config.discovery.service-id:配置服务实例名称,这样就可以使用负载均衡来配置config服务
  • spring.cloud.config.fail-fast:true配置客户端快速失败

值得注意的是,以上属性应配置在bootstrap.yml,而不是application.yml中。如果配置在application.yml中,该部分配置就不能正常工作。

Spring Cloud有一个"引导上下文"的概念,这是主应用程序上下文(Application Context)的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.* (yml或properties)中的属性不同,引导上下文加载bootstrap.* 中的属性。配置在bootstrap.* 中的属性有更高的优先级,因此默认情况下它们不能被本地配置覆盖。

  1. 接下来,我们在config-client分支上提交两个配置文件,cloud-service-member-dev.yml和cloud-service-order-dev.yml。把我们之前配置在cloud-service-member和cloud-service-order的application.yml信息复制过去。
  • cloud-service-member-dev.yml

在这里插入图片描述

  • cloud-service-order-dev.yml

在这里插入图片描述

  1. 然后我们启动项目

在cloud-service-member的控制台打印如下:

2019-10-12 15:49:43.524  INFO [cloud-service-member,,,] 11980 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8090/
2019-10-12 15:49:48.744  INFO [cloud-service-member,,,] 11980 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=cloud-service-member, profiles=[dev], label=config-client, version=d97fa30ba090c2836e9e6feae4f8fa6b5e2390f0, state=null
2019-10-12 15:49:48.744  INFO [cloud-service-member,,,] 11980 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/cloud-service-member-dev.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/application-dev.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/application.yml'}]}
2019-10-12 15:49:48.786  INFO [cloud-service-member,,,] 11980 --- [           main] com.cc.cloud.member.MemberApp            : No active profile set, falling back to default profiles: default
2019-10-12 15:49:49.679  WARN [cloud-service-member,,,] 11980 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-10-12 15:49:49.741  WARN [cloud-service-member,,,] 11980 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'hystrix.stream' contains invalid characters, please migrate to a valid format.
2019-10-12 15:49:49.977  INFO [cloud-service-member,,,] 11980 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6f2598ad-4fc5-3785-9225-e4bfe3cbbcdf
2019-10-12 15:49:50.003  INFO [cloud-service-member,,,] 11980 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.cc.cloud.member.feign.OrderFeign' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 15:49:50.336  INFO [cloud-service-member,,,] 11980 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$236f23be] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 15:49:50.920  INFO [cloud-service-member,,,] 11980 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8762 (http)
2019-10-12 15:49:50.946  INFO [cloud-service-member,,,] 11980 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

在cloud-service-order的控制台打印如下:

2019-10-12 15:18:38.267  INFO [cloud-service-order,,,] 14884 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8090/
2019-10-12 15:18:44.249  INFO [cloud-service-order,,,] 14884 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=cloud-service-order, profiles=[dev], label=config-client, version=d97fa30ba090c2836e9e6feae4f8fa6b5e2390f0, state=null
2019-10-12 15:18:44.250  INFO [cloud-service-order,,,] 14884 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/cloud-service-order-dev.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/application-dev.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/application.yml'}]}
2019-10-12 15:18:44.308  INFO [cloud-service-order,,,] 14884 --- [           main] com.cc.cloud.order.OrderApp              : No active profile set, falling back to default profiles: default
2019-10-12 15:18:45.447  WARN [cloud-service-order,,,] 14884 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-10-12 15:18:45.604  WARN [cloud-service-order,,,] 14884 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'hystrix.stream' contains invalid characters, please migrate to a valid format.
2019-10-12 15:18:45.966  INFO [cloud-service-order,,,] 14884 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=c4c264e0-1445-37de-8e5c-987c985f7e63
2019-10-12 15:18:45.995  INFO [cloud-service-order,,,] 14884 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.cc.cloud.order.feign.MemberFeign' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 15:18:46.402  INFO [cloud-service-order,,,] 14884 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$282eda35] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 15:18:46.969  INFO [cloud-service-order,,,] 14884 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8765 (http)
2019-10-12 15:18:46.998  INFO [cloud-service-order,,,] 14884 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-12 15:18:46.998  INFO [cloud-service-order,,,] 14884 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13

说明已经获取到远程仓库的,cloud-service-member-dev.yml和cloud-service-order-dev.yml文件。

多服务公共文件配置

上面我们的配置cloud-service-member-dev.yml和cloud-service-order-dev.yml可以发现其实是有很多公共的配置,我们可能有很多个服务之间都有一些common的配置,如果各自都配置在不同的文件中,那当我们需要更改这些common的配置,就需要更改很多个配置。所以特别麻烦。而且目前所有的配置文件都放置在同一个文件夹下面,到时候可能有很多不同服务的配置文件,还有不同环境的配置文件,如果都放在一起就特别麻烦,不方便管理。 所以接下来我就介绍一下怎么去配置多服务的公共配置文件,还有怎么去区分不同环境的配置,也就是怎么去分包。下面介绍的是我个人目前使用的配置,但不一定是最佳实践的方式,但是也是可以参考一下的。

Config Server的占位符

首先我们需要实现这些功能,要先了解Config Server的占位符。

Config Server的占位符支持{application}{profile}{label}

  • {application}对应的是你在config client端配置的spring.application.name
  • {profile}对应的是你在config client端配置的spring.cloud.config.profile
  • {label}对应的是你在config client端配置的spring.cloud.config.label

config server还有模式匹配,这里就不多介绍了。我们这里只需要用到占位符

很多场景下,可能把配置文件放在了Git仓库子目录中或者多个目录下,此时可以使用search-path指定,search-path同样支持占位符。 search-path可以配置多个路径,若有多个路径使用逗号隔开。还有需要注意的是,如果search-paths需要使用到占位符,需要带引号,比如search-paths: {application}是有问题的,需要设置成search-paths: '{application}'

下面给出我的配置:

首先

server:
  port: 8090
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cckevincyh/spring-cloud-demo.git
          # 指定搜索根路径下的所有目录,若有多个路径使用逗号隔开
          # {application} 映射到客户端的 spring.application.name;
          # {profile} 映射到客户端上的 spring.profiles.active;
          search-paths: 'cloud-config/default/common,cloud-config/{profile}/common,cloud-config/{profile}/{application}'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

解释:

  • cloud-config/default/common目录下是全部服务都可以共享的配置,不区分环境。

  • cloud-config/{profile}/common目录下是指定环境为{profile}的所有服务都可以共享的配置。

  • cloud-config/{profile}/{application}目录下是指定环境为{profile}并且服务名称为{application}才可以获取到。

比如我们的服务为cloud-service-member,在dev环境下,那么他就可以获取到cloud-config/default/common目录下的配置还有cloud-config/dev/common目录下的配置还有cloud-config/dev/cloud-service-member目录下的配置。

但是文件的命名还需要设置,我们之前提到的Spring Cloud Config实现配置的"继承与"组合",当我们的服务是cloud-service-member,会获取如下的配置。

cloud-service-member-dev.yml
cloud-service-member.yml
application-dev.yml
application.yml

所以我们可以把cloud-config/default/common目录下还有cloud-config/{profile}/commoncloud-config/{profile}/{application}目录下的配置文件名称都设置成application.yml,这样就比较common,不需要配置多个文件。所以我们就可以访问到这些文件了。

测试示例:

根据上面说的,我们整理一下我们的配置,并把一些common的抽取出来。

  1. 首先是cloud-config/default/common的application.yml配置文件

在这里插入图片描述

  1. cloud-config/{profile}/common的application.yml的配置文件,我这里只设置了dev环境的,也就是cloud-config/dev/common

在这里插入图片描述

  1. 最后是我们服务相关的cloud-config/{profile}/{application}目录下的application.yml文件。

在这里插入图片描述

在这里插入图片描述

最后别忘记该bootstrap.yml的label设置,改成share-config,因为我已经切换到share-config分支上去了。但是你会发现当你切换了分支之后每个config client端的label都需要重新设置,就很麻烦。所以我们可以在config server端配置一个default-label,设置default-label: share-config,然后在config client端就不做设置(如果设置了就会覆盖掉default-label的设置),这样就可以读取到default-label的设置了。

  1. 我们启动项目

我们可以查看到cloud-service-member打印的控制台如下:

2019-10-12 17:41:26.331  INFO [cloud-service-member,,,] 228 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8090/
2019-10-12 17:41:33.284  INFO [cloud-service-member,,,] 228 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=cloud-service-member, profiles=[dev], label=null, version=7a06c0804fd79c11a89c92180086628356227f1e, state=null
2019-10-12 17:41:33.284  INFO [cloud-service-member,,,] 228 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/dev/cloud-service-member/application.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/dev/common/application.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/default/common/application.yml'}]}
2019-10-12 17:41:33.328  INFO [cloud-service-member,,,] 228 --- [           main] com.cc.cloud.member.MemberApp            : No active profile set, falling back to default profiles: default
2019-10-12 17:41:34.280  WARN [cloud-service-member,,,] 228 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-10-12 17:41:34.343  WARN [cloud-service-member,,,] 228 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'hystrix.stream' contains invalid characters, please migrate to a valid format.
2019-10-12 17:41:34.567  INFO [cloud-service-member,,,] 228 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6f2598ad-4fc5-3785-9225-e4bfe3cbbcdf
2019-10-12 17:41:34.593  INFO [cloud-service-member,,,] 228 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.cc.cloud.member.feign.OrderFeign' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 17:41:34.939  INFO [cloud-service-member,,,] 228 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$8ae04ff4] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 17:41:35.710  INFO [cloud-service-member,,,] 228 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8762 (http)
2019-10-12 17:41:35.744  INFO [cloud-service-member,,,] 228 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-12 17:41:35.744  INFO [cloud-service-member,,,] 228 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13

我们可以查看到cloud-service-order打印的控制台如下:

2019-10-12 17:40:58.590  INFO [cloud-service-order,,,] 9096 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8090/
2019-10-12 17:41:06.193  INFO [cloud-service-order,,,] 9096 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=cloud-service-order, profiles=[dev], label=null, version=7a06c0804fd79c11a89c92180086628356227f1e, state=null
2019-10-12 17:41:06.193  INFO [cloud-service-order,,,] 9096 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/dev/cloud-service-order/application.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/dev/common/application.yml'}, MapPropertySource {name='https://gitee.com/cckevincyh/spring-cloud-demo.git/cloud-config/default/common/application.yml'}]}
2019-10-12 17:41:06.256  INFO [cloud-service-order,,,] 9096 --- [           main] com.cc.cloud.order.OrderApp              : No active profile set, falling back to default profiles: default
2019-10-12 17:41:07.945  WARN [cloud-service-order,,,] 9096 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-10-12 17:41:08.028  WARN [cloud-service-order,,,] 9096 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'hystrix.stream' contains invalid characters, please migrate to a valid format.
2019-10-12 17:41:08.266  INFO [cloud-service-order,,,] 9096 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=c4c264e0-1445-37de-8e5c-987c985f7e63
2019-10-12 17:41:08.293  INFO [cloud-service-order,,,] 9096 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.cc.cloud.order.feign.MemberFeign' of type [org.springframework.cloud.openfeign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 17:41:08.608  INFO [cloud-service-order,,,] 9096 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a2125ba1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-12 17:41:09.158  INFO [cloud-service-order,,,] 9096 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8765 (http)
2019-10-12 17:41:09.181  INFO [cloud-service-order,,,] 9096 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-12 17:41:09.181  INFO [cloud-service-order,,,] 9096 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13

可以看到我们的服务可以获取到cloud-config/default/common目录下的配置还有cloud-config/{profile}/common目录下的配置还有cloud-config/{profile}/{application}目录下的配置。这样就达到了我们多服务共享配置的目的了。

【参考】

SpringCloud与Docker微服务架构实战-完整版.pdf

spring cloud config中配置占位符searchPaths: {application}的坑

Spring Cloud Config - 统一配置中心

Spring-Cloud-Config 共享公共配置

Spring-Cloud-Config 多服务公共文件配置

参考

SpringCloud与Docker微服务架构实战-完整版.pdf

分布式远程获取配置文件config属性(修正)

Spring Cloud Config - 统一配置中心

Spring-Cloud-Config 共享公共配置

Spring-Cloud-Config 多服务公共文件配置

【SpringCloud Greenwich版本】目录

Spring Cloud 探索 | 分布式配置中心(Config Server)

spring cloud config中配置占位符searchPaths: {application}的坑

Spring Cloud(十四)Config 配置中心与客户端的使用与详细

spring Cloud-config(十一)

Spring Cloud Config远程配置中心

跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config

springcloud(六):配置中心git示例

源代码

https://gitee.com/cckevincyh/spring-cloud-demo/tree/config-server/

https://gitee.com/cckevincyh/spring-cloud-demo/tree/config-client/

https://gitee.com/cckevincyh/spring-cloud-demo/tree/share-config/

发布了647 篇原创文章 · 获赞 816 · 访问量 98万+

猜你喜欢

转载自blog.csdn.net/cckevincyh/article/details/102511938
今日推荐