准备仓库

  1. 将2,3,4三个项目的配置文件存放至git仓库
  2. 将2,3,4三个项目的配置文件复制到config目录
    • item-service-dev.yml
    • order-service-dev.yml
    • user-service-dev.yml
  3. 添加override-none=true防止配置中心的配置覆盖本地的命令参数

    1. server:
    2. port: 8201
    3. spring:
    4. application:
    5. name: order-service
    6. cloud:
    7. config:
    8. override-none: true

    搭建配置中心

  4. 新建模块

  5. 添加依赖

    • eureka client
    • config server

      1. <dependencies>
      2. <dependency>
      3. <groupId>org.springframework.cloud</groupId>
      4. <artifactId>spring-cloud-config-server</artifactId>
      5. </dependency>
      6. <dependency>
      7. <groupId>org.springframework.cloud</groupId>
      8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      9. </dependency>
      10. <dependency>
      11. <groupId>org.springframework.boot</groupId>
      12. <artifactId>spring-boot-starter-test</artifactId>
      13. <scope>test</scope>
      14. </dependency>
      15. </dependencies>
  6. yml配置

    • 配置仓库地址
    • 存放配置文件的文件夹路径
      1. server:
      2. port: 6001
      3. spring:
      4. application:
      5. name: config-server
      6. cloud:
      7. config:
      8. server:
      9. git:
      10. uri: https://gitee.com/G318705208/microservice-learning
      11. search-paths: /SpringCloud01/config
      12. eureka:
      13. client:
      14. service-url:
      15. defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
  7. 启动类注解@EnableConfigServer

    确认配置中心服务器是否正确

  8. 检查服务注册中心是否有配置中心服务即config-server

  9. 访问配置中心得配置文件

    • http://localhost:6001/user-service/dev
    • http://localhost:6001/item-service/dev
    • http://localhost:6001/order-service/dev

      配置中心客户端

  10. 将2,3,4的配置文件全部注释

  11. 添加依赖 config client

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-config-client</artifactId>
    4. </dependency>
  12. 新建配置文件bootstrap.yml

  13. 添加配置:
    • eureka地址
    • 指定配置中心的服务id:CONFIG-SERVER
    • 指定加载的配置文件和profile
      1. eureka:
      2. client:
      3. service-url:
      4. defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
      5. spring:
      6. application:
      7. name: item-service
      8. cloud:
      9. config:
      10. discovery:
      11. enabled: true #允许从注册表发送配置中心地址
      12. service-id: config-server #配置中心服务id
      13. name: 配置文件名
      14. profile: 配置文件