其他分享
首页 > 其他分享> > (08)SpringCloud实战之Config配置

(08)SpringCloud实战之Config配置

作者:互联网

  一、概述

  1、分布式系统面临的问题

  微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理就很复杂了。

  2、Config是什么?

   如图,ABC三个微服务的配置文件放到远程Git上,同步到Local Git由Config Server统一管理。

  SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个外部化的中心配置。

  SpringCloud Config分为服务端和客户端两部分。服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器,并为客户端提供获取配置信息,加密/解密信息等访问接口。客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

  3、Config能干什么?

  集中管理配置文件

  不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release

  运行期间动态调整配置,不载需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息

  当配置发生变化时,服务不需要重启,即可感知到配置的变化并应用新的配置

  将配置信息以rest接口的形式暴露

  4、与GitHub整合配置

  由于SpringCloud Config默认使用Git来存储配置文件(也有其他方式,比如支持svn和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式。

  二、SpringCloud Config服务端配置

  1、创建GitHub仓库并且clone到本地

  (1)用自己的GitHub账号在GitHub上新建一个名为microservicecloud-config的新Repository

  (2) 由上一步获得SSH协议的Git地址:拷贝出来的(https://github.com/wrenlei/microservicecloud-config.git)

  (3)在本地硬盘目录上新建git仓库并clone 

  新建目录:D:\20200320\mySpringCloud,右键->点击Git Base Here

  执行命令:$ git clone https://github.com/wrenlei/microservicecloud-config.git,从服务器clone项目到本地

  结果如图,项目microservicecloud-config已经clone下来了,并且下面有隐藏的.git文件夹,包含了git的信息

  2、模拟运维工程师在本地的git目录操作

  (1)在本地D:\20200320\mySpringCloud\microservicecloud-config下新建 application.yml,一定保存为UTF-8格式

spring:
  profiles:
    active:
      - dev:
---
spring:
  profiles: dev #开发环境
  application:
    name: microservicecloud-config-atguigu-dev
---
spring:
  profiles: test #测试环境
  application:
    name: microservicecloud-config-atguigu-test
#请保存为UTF-8格式

  (2)将该文件推送到GitHub上

  在git命令窗口进入到microservicecloud-config目录,执行 cd microservicecloud-config,git status 查看一下发现文件已经发生变化

  执行命令:git add .(将当前目录下修改的所有代码从工作区添加到暂存区 . 代表当前目录)

  执行命令:git commit -m "init file"(将缓存区内容添加到本地仓库,引号中是注释)

  执行命令:git push origin master(将本地版本库推送到远程服务器)

  执行过程中可能要认证,$ git config --global user.email "xxx@xxx.com" 、$ git config --global user.name "xxx",根据提示操作即可。

  刷新GitHub,发现application.yml已经提交到了远程库。

  3、新建微服务 microservicecloud-config-3344 

  pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-3344</artifactId>
  
  <dependencies>
  <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-server</artifactId>
   </dependency>
   <!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
   <!--  
    <dependency>
     <groupId>org.eclipse.jgit</groupId>
     <artifactId>org.eclipse.jgit</artifactId>
     <version>4.10.0.201712302008-r</version>
   </dependency>
   -->
   <!-- 自己定义的api -->
   <dependency>
     <groupId>com.atguigu.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   
   <!-- Ribbon相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-ribbon</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   
   <!-- feign相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>
   
   <!-- hystrix和 hystrix-dashboard相关-->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
   </dependency> 
   
    <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>
</project>

  application.yml

server:
  port: 3344

spring:
  application:
    name: microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/wrenlei/microservicecloud-config.git #GitHub上面的git仓库名字

  创建主启动类 Config_3344_StartSpringCloudApp.java

package com.atguigu.springcloud;

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

@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp {

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

  修改hosts文件,添加映射:127.0.0.1 config-3344.com

  4、测试通过Config微服务是否可以从GitHub上获取配置信息

  (1)启动微服务 microservicecloud-config-3344,并贴出GitHub上的配置,方便进行对比

  输入:http://config-3344.com:3344/application-dev.yml 测试可以正确读取GitHub上信息

  输入:http://config-3344.com:3344/application-test.yml 测试可以正确读取GitHub上信息

  输入:http://config-3344.com:3344/application-1234.yml(不存在的配置),只能读到GitHub上配置的头文件

  成功实现了用SpringCloud Config通过GitHub获取配置信息。

  5、配置读取规则

  (1)/{application} -{profile}.yml

  (2)/{application} /{profile}[/{label}]

  举例:http://config-3344.com:3344/application/dev

  举例:http://config-3344.com:3344/application/test

  举例:http://config-3344.com:3344/application/dev/master

  举例:http://config-3344.com:3344/application/test/master

  (3)/{label}/{application}-{profile}.yml

  举例:http://config-3344.com:3344/master/application-dev.yml

  举例:http://config-3344.com:3344/master/application-test.yml

  (4)/{application} -{profile}.properties

  (5)/{label}/{application}-{profile}.properties

  三、SpringCloud Config客户端配置

  1、在D:\20200320\mySpringCloud\microservicecloud-config下新建microservicecloud-config-client.yml,保存为utf-8

spring:
  profiles:
    active:
      - dev:
---
server:
  port: 8201
spring:
  profiles: dev #开发环境
  application:
    name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test #测试环境
  application:
    name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-test.com:7001/eureka/

  2、将上一步提交到GitHub中

  3、 新建microservicecloud-config-client-3355

  pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-client-3355</artifactId>
  
  <dependencies>
  <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>
   
   <!-- Ribbon相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-ribbon</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   
   <!-- feign相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>
   
   <!-- hystrix和 hystrix-dashboard相关-->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
   </dependency> 
   
    <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
   
  </dependencies>
</project>

  bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要从GitHub上读取的资源名称,注意没有yml后缀名
      profile: dev #本次访问的配置项
      label: master
      uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

  application.yml

spring:
  application:
    name: microservicecloud-config-client

  window下增加hosts映射:C:\Windows\System32\drivers\etc\hosts:127.0.0.1 client-config.com

  新建ConfigClientRest.java类,验证是否能从GitHub上读取配置

package com.atguigu.springcloud.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController public class ConfigClientRest { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping("/config") public String getConfig() { String str = "applicationName:"+applicationName+"\t eurekaServers:"+eurekaServers+"\t port:"+port; System.out.println("*********str:"+str); return str; } }

  新建主启动类 ConfigClient_3355_StartSpringCloudApp.java

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp {

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

  4、测试,启动Config配置中心3344微服务和客户端配置3355微服务

  (1)自测:http://config-3344.com:3344/application-dev.yml,访问成功

 (2)bootstrap.yml里面的profile值是什么,决定从github上读取什么

  bootstrap.yml里面的profile值是dev,则读取dev的配置,默认在github上的端口号是8201:http://client-config.com:8201/config

  bootstrap.yml里面的profile值是test,则读取test的配置,test默认在github上的端口号是8202:http://client-config.com:8202/config

  四、配置实战

  通过上面的演示,Config服务端配置完成,且测试通过,我们可以和Config+GitHub进行配置修改并获得内容。此时我们做一个eureka服务+一个Dept访问的微服务,将两个微服务的配置统一由GitHub获取 实现统一配置分布式管理,完成多环境的变更。

  1、Git配置文件本地配置

  (1)在D:\20200320\mySpringCloud\microservicecloud-config路径下新建microservicecloud-config-eureka-client.yml,保存为utf-8

spring:
  profiles:
    active:
      - dev:
---
server:
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格

spring:
  profiles: dev #开发环境
  application:
    name: microservicecloud-config-eureka-client

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。
    fetch-registry: false    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
server:
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格

spring:
  profiles: test #测试环境
  application:
    name: microservicecloud-config-eureka-client

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。
    fetch-registry: false    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

  (2)在D:\20200320\mySpringCloud\microservicecloud-config路径下新建microservicecloud-config-dept-client.yml,保存为utf-8

spring:
  profiles:
    active:
      - dev:
---
server:
  port: 8001

spring:
  profiles: dev #开发环境
  application:
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB01              # 数据库名称
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件

eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #访问路径可以显示IP地址

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

---
server:
  port: 8001

spring:
  profiles: test #测试环境
  application:
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB02              # 数据库名称
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件

eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: microservicecloud-dept8001
    prefer-ip-address: true     #访问路径可以显示IP地址

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

  将这两个文件上传到GitHub上,参照前面的做法

  2、Config版的eureka服务端

  (1)新建工程 microservicecloud-config-eureka-client-7001

  pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-eureka-client-7001</artifactId>
  
  <dependencies>
  <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>
   
   <!--eureka-server服务端 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
   </dependency>
   
   <!-- Ribbon相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-ribbon</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   
   <!-- feign相关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>
   
   <!-- hystrix和 hystrix-dashboard相关-->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
   </dependency> 
   
    <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
   
  </dependencies>
</project>

  application.yml

spring:
  application:
    name: microservicecloud-config-eureka-client

  bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-eureka-client #需要从GitHub上读取的资源名称,注意没有yml后缀名
      profile: dev
      label: master
      uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

  Config_Git_EurekaServerApplicaion.java

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer    //EurekaServer服务器端启动类,接受其它微服务注册进来
public class Config_Git_EurekaServerApplicaion
{
  public static void main(String[] args)
  {
   SpringApplication.run(Config_Git_EurekaServerApplicaion.class, args);
  }
}

  (2)测试,启动 microservicecloud-config-eureka-client-7001 输入 http://eureka7001.com:7001,出现如下界面说明成功。

  3、Config版的dept微服务 

  (1)参考8001,拷贝新建microservicecloud-config-dept-client-8001 

  pom.xml

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-dept-client-8001</artifactId>
  
  <dependencies>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   <dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
     <groupId>com.atguigu.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
   </dependency>
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
   </dependency>
   <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-core</artifactId>
   </dependency>
   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
   
   <!-- 将微服务provider侧注册进eureka -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   
   
   <!-- 修改后立即生效,热部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>
  
</project>

  application.yml

spring:
  application:
    name: microservicecloud-config-dept-client

  bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要从GitHub上读取的资源名称,注意没有yml后缀名
      profile: test 
      label: master
      uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

  主启动类及其他业务逻辑代码直接copy微服务microservicecloud-provider-dept-8001的,不做改动

  (2)测试,启动3344、microservicecloud-config-eureka-client-7001、microservicecloud-config-dept-client-8001

  此时microservicecloud-provider-dept-8001的bootstrap.yml配置的是test。输入:http://localhost:8001/dept/list,可以看到数据库配置是02

   如果microservicecloud-provider-dept-8001的bootstrap.yml配置的是dev输入:http://localhost:8001/dept/list,可以看到数据库配置是01

   (3)这是直接在项目工程中修改的 bootstrap.yml配置,下面验证一下从GitHub上读取配置

  把D:\20200320\mySpringCloud\microservicecloud-config的microservicecloud-config-dept-client.yml修改dev改为读取3号库,传到GitHub上

  重新启动3344、microservicecloud-config-eureka-client-7001、microservicecloud-config-dept-client-8001

  此时microservicecloud-provider-dept-8001的bootstrap.yml配置的是dev输入:http://localhost:8001/dept/list,可以看到数据库配置是03

   测试成功。

 

  

 

  

   

 

标签:Config,SpringCloud,08,springframework,microservicecloud,spring,org,config,cloud
来源: https://www.cnblogs.com/javasl/p/12524951.html