SpringCloud学习(九)Spring Cloud Config的介绍和使用

一. 综述

基本上每个微服务都是使用application.yml(properties)进行配置,在实际应用中集群中会有多个服务,每个服务可能都要部署多个实例,项目开始运营后,如何对项目中的配置进行管理?如何实现修改配置而不重启服务?
Spring Cloud已经为这些问题提供了解决方案:Spring Cloud Config
Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,通过对他们的配置可以很好的管理集群中的配置文件。实际应用中我们会将配置文件存放到外部系统(如Git、SVN等),然后Spring Cloud Config的服务器和客户端到这些外部系统中读取、使用配置。

二. 使用

SpringCloud版本:Greenwich.RELEASE
SpringBoot版本:2.1.4.RELEASE

  1. 在github上创建仓库

  2. 新建模块,springcloud-config-server,配置服务器,添加依赖(Eureka客户端依赖和Config服务器依赖)

    	</dependencies>
    		<!-- Eureka客户端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <!-- 这里会自动引入版本,类似parent标签继承 -->
            </dependency>
    
            <!-- Config服务端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <!-- <scope>import</scope>解决单继承问题,类似parent标签, -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
  3. 在该模块的主启动类上添加@EnableConfigServer注解,表示这是一个配置服务器
    另外添加@EnableEurekaClient注解,表示将该服务注册到Eureka中

  4. 编写配置

    server:
      port: 4000
    
    spring:
      #指定当前的环境------在pom文件中取值使用@profiles.active@
      profiles:
        active: @profiles.active@
      application:
        name: springcloud-config-server
      cloud:
        config:
          server:
            git:
              uri: https://github.com/xiaoshijiu333/springcloud-config.git   # config服务端配置,连接git(使用http形式加密码)
              username: xxxxxxxxxxxxx
              password: xxxxxxxxxxxxx
    
    # 注册到Eureka中
    eureka:
      instance:
        instance-id: ${info.application.name}
      client:
        service-url:
          defaultZone: http://localhost:7000/eureka/,http://feiyu:7001/eureka/
    
    
    #监控的info信息
    info:
      application.name: springcloud-config-server
      build.groupId: @project.groupId@
      build.artifactId: @project.artifactId@
      build.version: @project.version@
    
    

    主要就是,注册到Eureka中和连接Git仓库
    注意:这里连接Git仓库方式,uri使用https地址的形式要结合账号密码连接;
    之前我使用git@地址的形式,连接一直失败(应该是需要重新配置SSH密钥)
    为了方便,就直接使用https地址+账号密码了。

  5. 编写Config客户端,这里我们将Zuul的路由配置信息放到Git中,让Zuul模块作为Config客户端去加载Git中的配置。
    前面步骤一样的,新建Zuul模块,引入Zuul和Eureka客户端依赖;主启动类添加@EnableZuulProxy注解,@EnableEurekaClient注解可加可不加。

  6. 编写Config配置,先讲一个概念bootstrap.yml

    Spring Cloud 的程序在进行容器初始化的时候会先建立一个 “引导上下文” (BootStrap Context),在创建主应用的上下文。我们的主应用上下文通常是读取application.yml(properties)文件,而引导上下文则会读取bootstrap.yml(properties)文件。因为application.yml文的配置会在bootstrap.yml后面加载,如果两份配置文件同时存在的话,且存在key相同的配置,则application.yml的配置会覆盖bootstrap.yml的配置。

    默认情况都是使用引导上下文配置文件bootstrap.yml去读取远程配置,application.yml常规配置。

    bootstrap.yml配置代码

    # bootstrap.yml文件会在application.yml之前加载,是引导上下文文件,注意不能使用@@在pom中取值
    
    spring:
      application:
        name: springcloud-zuul-config-5001
      cloud:
        config:
          discovery:
            enabled: true
            service-id: springcloud-config-server  # 因为注册到了Eureka中,所以这里通过ServiceId指定服务名
          profile: dev  # 则检索的文件名为 springcloud-zuul-config-5001-dev.yml
          label: master # 指定git分支(没有默认值,必须要指定一下)
    
    # 注册到Eureka中
    eureka:
      instance:
        instance-id: ${info.application.name}
      client:
        service-url:
          defaultZone: http://localhost:7000/eureka/,http://feiyu:7001/eureka/
    

    检索的文件名为 springcloud-zuul-config-5001-dev.yml
    就是 { spring.application.name }-{ profile }.yml,如果没有定义 spring.application.name,则默认就是application

    application.yml配置代码

    server:
      port: 5001
    
    spring:
      #指定当前的环境------在pom文件中取值使用@profiles.active@
      profiles:
        active: @profiles.active@
    	
    # 监控的info信息
    info:
      application.name: springcloud-zuul-config-5001
      build.groupId: @project.groupId@
      build.artifactId: @project.artifactId@
      build.version: @project.version@
    
    
  7. 在第一步建的Git仓库中,添加springcloud-zuul-config-5001-dev.yml文件,并填写内容

    # 远程加载网关转发配置
    zuul:
      routes:
        fei:
          path: /fei/**
          url: http://www.taobao.com
        yu:
          path: /yu/**
          serviceId: http://www.baidu.com
    
  8. 测试,依次启动Eureka注册中心,config-server,zuul-config-client
    浏览器访问http://localhost:5001/fei/test跳转到淘宝错误页
    浏览器访问http://localhost:5001/yu/test跳转到百度错误页
    说明我们的Zuul客户端已经从git上加载到了路由规则配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值