SpringCloud Config 分布式配置中心

╰+攻爆jí腚メ 2022-03-17 01:52 490阅读 0赞

SpringCloud 2018

  • 微服务概述
  • SpringCloud 概述
  • 微服务工程搭建
  • Eureka 服务注册与发现
  • Ribbon 客户端负载均衡
  • Feign 服务接口调用
  • Hystrix 断路器
  • Zuul 路由网关
  • SpringCloud Config 分布式配置中心
  • SpringCloud 技术梳理

GitHub

  • https://github.com/hjyang19/microservicecloud.git
  • https://github.com/hjyang19/microservicecloud-config.git

SpringCloud Config 分布式配置中心

  • 1-SpringCloud Config 概述
    • 1.1-是什么
    • 1.2-怎么玩
    • 1.3-能干嘛
  • 2-SpringCloud Config 服务端
    • 2.1-读取配置
    • 2.2-读取规则
  • 3-SpringCloud Config 客户端
  • 4-SpringCloud Config 配置示例
    • 4.1-Github 配置文件
    • 4.2-Config 版 Eureka 服务端
    • 4.3-Config 版 Dept 微服务

1-SpringCloud Config 概述

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

SpringCloud 提供了 Config Server 来解决这个问题,每一个微服务自己带着一个 application.yml,上百个配置文件的管理。

1.1-是什么

在这里插入图片描述
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

1.2-怎么玩

SpringCloud Config分为服务端和客户端两部分

服务端:也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息、加密、解密信息等访问接口。

客户端:是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用 Git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。

1.3-能干嘛

集中管理配置文件

  • 不同环境不同配置,动态化的配置更新,分环境部署(dev、test、prod、beta、release)
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
  • 将配置信息以 Rest 接口的形式暴露。

GitHub 整合配置:由于 SpringCloud Config 默认使用 Git 来存储配置文件(也有其它方式,比如支持 SVN 和本地文件),但最推荐的还是 Git,而且使用的是 Http、Https 访问的形式。


2-SpringCloud Config 服务端

2.1-读取配置

a、在 GitHub 上新建一个名为 microservicecloud-config 的仓库
在这里插入图片描述
在这里插入图片描述
b、复制 microservicecloud-config 仓库 Https 协议的地址

  1. git@github.com:hjyang19/microservicecloud-config.git

c、本地硬盘新建目录 D:\config,克隆 microservicecloud-config 仓库

  1. git clone git@github.com:hjyang19/microservicecloud-config.git

在这里插入图片描述
d、本地目录 D:\config\microservicecloud-config 里新建 application.yml

保存格式必须为UTF-8
在这里插入图片描述

  1. spring:
  2. profiles:
  3. active:
  4. - dev
  5. ---
  6. spring:
  7. profiles: dev # 开发环境
  8. application:
  9. name: microservicecloud-config-atguigu-dev
  10. ---
  11. spring:
  12. profiles: test # 测试环境
  13. application:
  14. name: microservicecloud-config-atguigu-test

e、本地目录 D:\config\microservicecloud-config 仓库变更推送到 GitHub 上

  1. # 推送 D:\config\microservicecloud-config 仓库变更
  2. git status
  3. git add .
  4. git commit -m "init yml"
  5. git push origin master

在这里插入图片描述
在这里插入图片描述
f、右击父工程 microservicecloud,新建子模块 microservicecloud-config-3344(配置中心模块)
在这里插入图片描述
g、修改子模块 microservicecloud-config-3344 中 pom.xml
在这里插入图片描述

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>com.atguigu.springcloud</groupId>
  6. <artifactId>microservicecloud</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <artifactId>microservicecloud-config-3344</artifactId>
  10. <dependencies>
  11. <!-- SpringCloud Config -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-config-server</artifactId>
  15. </dependency>
  16. <!-- 避免 Config 的 Git 插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
  17. <dependency>
  18. <groupId>org.eclipse.jgit</groupId>
  19. <artifactId>org.eclipse.jgit</artifactId>
  20. <version>4.10.0.201712302008-r</version>
  21. </dependency>
  22. <!-- 图形化监控 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <!-- 熔断 -->
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-hystrix</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-eureka</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-config</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-jetty</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-web</artifactId>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-starter-test</artifactId>
  51. </dependency>
  52. <!-- 热部署插件 -->
  53. <dependency>
  54. <groupId>org.springframework</groupId>
  55. <artifactId>springloaded</artifactId>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-devtools</artifactId>
  60. </dependency>
  61. </dependencies>
  62. </project>

h、修改子模块 microservicecloud-config-3344,目录 /src/main/resources 中新建 application.yml

  1. server:
  2. port: 3344
  3. spring:
  4. application:
  5. name: microservicecloud-config
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. # GitHub 仓库地址
  11. uri: https://github.com/hjyang19/microservicecloud-config.git
  12. #uri: git@github.com:hjyang19/microservicecloud-config.git # SSH 协议可能访问不到数据

i、修改子模块 microservicecloud-config-3344,目录 /src/main/java 中新建包 com.atguigu.springcloud

j、修改子模块 microservicecloud-config-3344,包 com.atguigu.springcloud 中新建启动类 Config_3344_StartSpringCloudApp.java

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. @SpringBootApplication
  6. @EnableConfigServer
  7. public class Config_3344_StartSpringCloudApp {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
  10. }
  11. }

k、修改 Windows 网络映射配置

找到 C:\Windows\System32\drivers\etc 路径下的 hosts 文件,添加内容:

  1. 127.0.0.1 config-3344.com

l、测试 Config 服务端

测试子模块 microservicecloud-config-3344 是否可以从 GitHub 上获取配置内容

  • 启动子模块 microservicecloud-config-3344
  • http://config-3344.com:3344/application-dev.yml
    在这里插入图片描述
  • http://config-3344.com:3344/application-test.yml
    在这里插入图片描述
  • http://config-3344.com:3344/application-xxx.yml

    不存在的配置
    在这里插入图片描述

2.2-读取规则

在这里插入图片描述
/{application}-{profile}.yml

  • http://config-3344.com:3344/application-dev.yml
  • http://config-3344.com:3344/application-test.yml
  • http://config-3344.com:3344/application-xxx.yml

/{application}/{profile}[/{label}]

  • http://config-3344.com:3344/application/dev/master
  • http://config-3344.com:3344/application/test/master
  • http://config-3344.com:3344/application/xxx/master

/{label}/{application}-{profile}.yml

  • http://config-3344.com:3344/master/application-dev.yml
  • http://config-3344.com:3344/master/application-test.yml

3-SpringCloud Config 客户端

a、在本地目录 D:\config\microservicecloud-config 里新建 microservicecloud-config-client.yml

  1. spring:
  2. profiles:
  3. active:
  4. - dev
  5. ---
  6. server:
  7. port: 8201
  8. spring:
  9. profiles: dev
  10. application:
  11. name: microservicecloud-config-client
  12. eureka:
  13. client:
  14. service-url:
  15. defaultZone: http://eureka-dev.com:7001/eureka/
  16. ---
  17. server:
  18. port: 8202
  19. spring:
  20. profiles: test
  21. application:
  22. name: microservicecloud-config-client
  23. eureka:
  24. client:
  25. service-url:
  26. defaultZone: http://eureka-test.com:7001/eureka/

b、本地目录 D:\config\microservicecloud-config 仓库变更推送到 GitHub 上

  1. # 推送 D:\config\microservicecloud-config 仓库变更
  2. git status
  3. git add .
  4. git commit -m "init yml"
  5. git push origin master

在这里插入图片描述
c、右击父工程 microservicecloud,新建子模块 microservicecloud-config-client-3355
在这里插入图片描述
d、修改子模块 microservicecloud-config-client-3355 中 pom.xml
在这里插入图片描述

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>com.atguigu.springcloud</groupId>
  6. <artifactId>microservicecloud</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <artifactId>microservicecloud-config-client-3355</artifactId>
  10. <dependencies>
  11. <!-- SpringCloud Config 客户端 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-config</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-actuator</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-hystrix</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-eureka</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-config</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-jetty</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-web</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-test</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>springloaded</artifactId>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-devtools</artifactId>
  51. </dependency>
  52. </dependencies>
  53. </project>

e、修改子模块 microservicecloud-config-client-3355,目录 /src/main/resources 中新建 bootstrap.yml

  1. spring:
  2. cloud:
  3. config:
  4. # Github 上读取的资源名称,注意没有 yml 后缀名
  5. name: microservicecloud-config-client
  6. profile: dev # 访问的配置项
  7. label: master
  8. # 微服务启动后先去找端口 3344 服务,通过 SpringCloud Config 获取 GitHub 的服务地址
  9. uri: http://config-3344.com:3344

bootstrap.yml

  • applicaiton.yml用户级的资源配置项。
  • bootstrap.yml系统级的,优先级更加高

Spring Cloud 会创建一个 Bootstrap Context,作为 Spring 应用的 Application Context父上下文

初始化的时候,Bootstrap Context 负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。

Bootstrap 属性有高优先级。默认情况下,它们不会被本地配置覆盖。Bootstrap context 和 Application Context 有着不同的约定,所以新增了一个 bootstrap.yml 文件,保证 Bootstrap Context 和 Application Context 配置的分离。

f、修改子模块 microservicecloud-config-client-3355,目录 /src/main/resources 中新建 application.yml

  1. spring:
  2. application:
  3. name: microservicecloud-config-client

g、修改子模块 microservicecloud-config-client-3355,目录 /src/main/java 中新建包 com.atguigu.springcloud

h、修改子模块 microservicecloud-config-client-3355,包 com.atguigu.springcloud 中新建启动类 ConfigClient_3355_StartSpringCloudApp.java

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class ConfigClient_3355_StartSpringCloudApp {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);
  8. }
  9. }

i、修改子模块 microservicecloud-config-client-3355,目录 /src/main/java 中新建包 com.atguigu.springcloud.rest

j、修改子模块 microservicecloud-config-client-3355,包 com.atguigu.springcloud 中新建 ConfigClientRest.java

验证是否能从 GitHub 上读取配置。

  1. package com.atguigu.springcloud.rest;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. public class ConfigClientRest {
  7. @Value("${spring.application.name}")
  8. private String applicationName;
  9. @Value("${eureka.client.service-url.defaultZone}")
  10. private String eurekaServers;
  11. @Value("${server.port}")
  12. private String port;
  13. @RequestMapping("/config")
  14. public String getConfig() {
  15. String str = "applicationName: " + applicationName +
  16. "\t eurekaServers:" + eurekaServers + "\t port: " + port;
  17. System.out.println("******str: " + str);
  18. return str;
  19. }
  20. }

k、修改 Windows 网络映射配置

找到 C:\Windows\System32\drivers\etc 路径下的 hosts 文件,添加内容:

  1. 127.0.0.1 client-config.com

l、测试 Config 客户端

子模块 microservicecloud-config-client-3355 通过访问子模块 microservicecloud-config-3344 获取GitHub 配置信息。

  • 启动子模块 microservicecloud-config-3344

    等待 Config 服务端完全启动。

  • http://config-3344.com:3344/application-dev.yml
    在这里插入图片描述
  • 启动子模块 microservicecloud-config-client-3355

    bootstrap.yml 里面的 profile 值是什么,决定从 Github 上读取什么。

    • 当前 bootstrap.yml 里是 profile : dev,dev 在 GitHub上端口号是 8021

      http://client-config.com:8201/config
      在这里插入图片描述

    • 若 bootstrap.yml 里是 profile : test,test 在 GitHub上端口号是 8202

      http://client-config.com:8202/config


4-SpringCloud Config 配置示例

目前情况

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

4.1-Github 配置文件

a、在本地目录 D:\config\microservicecloud-config 里新建 microservicecloud-config-eureka-client.yml

  1. spring:
  2. profiles:
  3. active:
  4. - dev
  5. ---
  6. server:
  7. port: 7001 # 注册中心端口 7001,冒号后面必须要有空格
  8. spring:
  9. profiles: dev
  10. application:
  11. name: microservicecloud-config-eureka-client
  12. eureka:
  13. instance:
  14. hostname: eureka7001.com # 冒号后面必须要有空格
  15. client:
  16. register-with-eureka: false # 当前 Eureka Server 自己不注册进服务列表中
  17. fetch-registry: false # 不通过 Eureka 获取注册信息
  18. service-url:
  19. defaultZone: http://eureka7001.com:7001/eureka/
  20. ---
  21. server:
  22. port: 7001 # 注册中心端口 7001,冒号后面必须要有空格
  23. spring:
  24. profiles: test
  25. application:
  26. name: microservicecloud-config-eureka-client
  27. eureka:
  28. instance:
  29. hostname: eureka7001.com # 冒号后面必须要有空格
  30. client:
  31. register-with-eureka: false # 当前 Eureka Server 自己不注册进服务列表中
  32. fetch-registry: false # 不通过 Eureka 获取注册信息
  33. service-url:
  34. defaultZone: http://eureka7001.com:7001/eureka/

b、在本地目录 D:\config\microservicecloud-config 里新建 microservicecloud-config-dept-client.yml

  1. spring:
  2. profiles:
  3. active:
  4. - dev
  5. ---
  6. server:
  7. port: 8001
  8. spring:
  9. profiles: dev
  10. application:
  11. name: microservicecloud-config-dept-client
  12. datasource:
  13. type: com.alibaba.druid.pool.DruidDataSource
  14. driver-class-name: org.gjt.mm.mysql.Driver
  15. url: jdbc:mysql://localhost:3306/cloudDB01
  16. username: root
  17. password: 123456
  18. dbcp2:
  19. min-idle: 5
  20. initial-size: 5
  21. max-total: 5
  22. max-wait-millis: 200
  23. mybatis:
  24. config-location: classpath:mybatis/mybatis.cfg.xml
  25. type-aliases-package: com.atguigu.springcloud.entities
  26. mapper-locations:
  27. - classpath:mybatis/mapper/**/*.xml
  28. eureka:
  29. client: # 客户端注册进 Eureka 服务列表
  30. service-url:
  31. defaultZone: http://eureka7001.com:7001/eureka
  32. instance:
  33. instance-id: dept-8001.com
  34. prefer-ip-address: true
  35. info:
  36. app.name: atguigu-microservicecloud-springcloudconfig01
  37. company.name: www.atguigu.com
  38. build.artifactId: "@project.artifactId@"
  39. build.version: "@project.version@"
  40. ---
  41. server:
  42. port: 8001
  43. spring:
  44. profiles: test
  45. application:
  46. name: microservicecloud-config-dept-client
  47. datasource:
  48. type: com.alibaba.druid.pool.DruidDataSource
  49. driver-class-name: org.gjt.mm.mysql.Driver
  50. url: jdbc:mysql://localhost:3306/cloudDB02
  51. username: root
  52. password: 123456
  53. dbcp2:
  54. min-idle: 5
  55. initial-size: 5
  56. max-total: 5
  57. max-wait-millis: 200
  58. mybatis:
  59. config-location: classpath:mybatis/mybatis.cfg.xml
  60. type-aliases-package: com.atguigu.springcloud.entities
  61. mapper-locations:
  62. - classpath:mybatis/mapper/**/*.xml
  63. eureka:
  64. client: # 客户端注册进 Eureka 服务列表
  65. service-url:
  66. defaultZone: http://eureka7001.com:7001/eureka
  67. instance:
  68. instance-id: dept-8001.com
  69. prefer-ip-address: true
  70. info:
  71. app.name: atguigu-microservicecloud-springcloudconfig02
  72. company.name: www.atguigu.com
  73. build.artifactId: "@project.artifactId@"
  74. build.version: "@project.version@"

c、本地目录 D:\config\microservicecloud-config 仓库变更推送到 GitHub 上

  1. # 推送 D:\config\microservicecloud-config 仓库变更
  2. git status
  3. git add .
  4. git commit -m "init yml"
  5. git push origin master

在这里插入图片描述

4.2-Config 版 Eureka 服务端

a、右击父工程 microservicecloud,新建子模块 microservicecloud-config-eureka-client-7001
在这里插入图片描述
b、修改子模块 microservicecloud-config-eureka-client-7001 中 pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>com.atguigu.springcloud</groupId>
  6. <artifactId>microservicecloud</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <artifactId>microservicecloud-config-eureka-client-7001</artifactId>
  10. <dependencies>
  11. <!-- SpringCloud Config 配置 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-config</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  19. </dependency>
  20. <!-- 热部署插件 -->
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>springloaded</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-devtools</artifactId>
  28. </dependency>
  29. </dependencies>
  30. </project>

c、修改子模块 microservicecloud-config-eureka-client-7001,目录 /src/main/resources 中新建 bootstrap.yml

  1. spring:
  2. cloud:
  3. config:
  4. name: microservicecloud-config-eureka-client # 从 Github 上读取的资源名称,注意没有 yml 后缀名
  5. profile: dev
  6. label: master
  7. uri: http://config-3344.com:3344 # SpringCloud Config 获取的服务地址

d、修改子模块 microservicecloud-config-eureka-client-7001,目录 /src/main/resources 中新建 application.yml

  1. spring:
  2. application:
  3. name: microservicecloud-config-eureka-client

e、修改子模块 microservicecloud-config-eureka-client-7001,目录 /src/main/java 中新建包 com.atguigu.springcloud

f、修改子模块 microservicecloud-config-eureka-client-7001,包 com.atguigu.springcloud 中新建启动类 Config_Git_EurekaServerApplication.java

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class Config_Git_EurekaServerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
  10. }
  11. }

g、测试 Config 版 Eureka 服务端

  • 启动子模块 microservicecloud-config-3344

    等待 Config 服务端完全启动。

  • 启动子模块 microservicecloud-config-eureka-client-7001
  • http://eureka7001.com:7001/

    出现 Eureak 主页表示成功启动
    在这里插入图片描述

4.3-Config 版 Dept 微服务

a、右击父工程 microservicecloud,新建子模块 microservicecloud-config-dept-client-8001
在这里插入图片描述
b、修改子模块 microservicecloud-config-dept-client-8001 中 pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>com.atguigu.springcloud</groupId>
  6. <artifactId>microservicecloud</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <artifactId>microservicecloud-config-dept-client-8001</artifactId>
  10. <dependencies>
  11. <!-- SpringCloud Config 配置 -->
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-config</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-actuator</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-eureka</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.atguigu.springcloud</groupId>
  26. <artifactId>microservicecloud-api</artifactId>
  27. <version>${project.version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>junit</groupId>
  31. <artifactId>junit</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>mysql</groupId>
  35. <artifactId>mysql-connector-java</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>com.alibaba</groupId>
  39. <artifactId>druid</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>ch.qos.logback</groupId>
  43. <artifactId>logback-core</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.mybatis.spring.boot</groupId>
  47. <artifactId>mybatis-spring-boot-starter</artifactId>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter-jetty</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-starter-web</artifactId>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-starter-test</artifactId>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>springloaded</artifactId>
  64. </dependency>
  65. <dependency>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-devtools</artifactId>
  68. </dependency>
  69. </dependencies>
  70. </project>

c、修改子模块 microservicecloud-config-dept-client-8001,目录 /src/main/java 中新建包 com.atguigu.springcloud

d、复制子模块 microservicecloud-provider-dept-8001 中包 com.atguigu.springcloud 内容到子模块 microservicecloud-config-dept-client-8001 包 com.atguigu.springcloud

e、修改子模块 microservicecloud-config-dept-client-8001,包 com.atguigu.springcloud 启动类重命名为 DeptProvider8001_Config_App.java

f、复制子模块 microservicecloud-provider-dept-8001 中目录 /main/resources 内容到子模块 microservicecloud-config-dept-client-8001 目录 /main/resources

g、修改子模块 microservicecloud-config-dept-client-8001,目录 /src/main/resources 中新建 bootstrap.yml

  1. spring:
  2. cloud:
  3. config:
  4. name: microservicecloud-config-dept-client # Github 上读取的资源名称,注意没有 yml 后缀名
  5. # profile 配置是什么就取什么配置 dev or test
  6. #profile: dev
  7. profile: test
  8. label: master
  9. uri: http://config-3344.com:3344 # SpringCloud Config 获取的服务地址

g、修改子模块 microservicecloud-config-dept-client-8001,目录 /src/main/resources 中 application.yml

  1. spring:
  2. application:
  3. name: microservicecloud-config-dept-client

主要看 bootstrap.yml 文件里面的 profile: xxx 属性是什么值,从而确定它能从 GitHub 上取什么样的配置。若配置 profile: test 取值如下图路。具体各自数据库不同,从而依据配置得到分布式配置的目的。
在这里插入图片描述
h、测试 Config 版 Eureka 服务端

  • 启动子模块 microservicecloud-config-3344

    等待 Config 服务端完全启动。

  • 启动子模块 microservicecloud-config-eureka-client-7001
  • 启动子模块 microservicecloud-config-dept-client-8001
  • http://localhost:8001/dept/get/1

    默认配置 profile: test 访问数据库 cloudDB02。
    在这里插入图片描述
    在这里插入图片描述

  • http://localhost:8001/dept/get/1

    换成配置 profile: dev 访问数据库 cloudDB01。


发表评论

表情:
评论列表 (有 0 条评论,490人围观)

还没有评论,来说两句吧...

相关阅读