(08)SpringCloud实战之Config配置

  1、概述java

  一、分布式系统面临的问题mysql

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

  二、Config是什么?github

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

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

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

  三、Config能干什么?数据库

  集中管理配置文件apache

  不一样环境不一样配置,动态化的配置更新,分环境部署好比dev/test/prod/beta/releasebootstrap

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

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

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

  四、与GitHub整合配置

  因为SpringCloud Config默认使用Git来存储配置文件(也有其余方式,好比支持svn和本地文件),但最推荐的仍是Git,并且使用的是http/https访问的形式。

  2、SpringCloud Config服务端配置

  一、建立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的信息

  二、模拟运维工程师在本地的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已经提交到了远程库。

  三、新建微服务 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

  四、测试经过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获取配置信息。

  五、配置读取规则

  (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客户端配置

  一、在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/

  二、将上一步提交到GitHub中

  三、 新建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);
    }
}

  四、测试,启动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

  4、配置实战

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

  一、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上,参照前面的作法

  二、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,出现以下界面说明成功。

  三、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)测试,启动334四、microservicecloud-config-eureka-client-700一、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上

  从新启动334四、microservicecloud-config-eureka-client-700一、microservicecloud-config-dept-client-8001

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

   测试成功。

相关文章
相关标签/搜索