Spring Cloud 微服务之Config(九)

22 篇文章 1 订阅
18 篇文章 0 订阅

一、什么是Spring Cloud Config?

  • Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分。
  • Spring Cloud Config 服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。
  • Spring Cloud Config 客户端可以通过配置中心来获取配置信息,在启动时加载配置。
  • Spring Cloud Config 的配置中心默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。

二、搭建GIT环境

  • 创建仓库

    在这里插入图片描述

  • 创建文件

    • master分支

      # config-dev.yml
      config:
        info: "config info for dev(master)"
      # config-test.yml
      config:
        info: "config info for test(master)"
      # config-prod.yml
      config:
        info: "config info for prod(master)"
      
    • dev分支

      # config-dev.yml
      config:
        info: "config info for dev(dev)"
      # config-test.yml
      config:
        info: "config info for test(dev)"
      # config-prod.yml
      config:
        info: "config info for prod(dev)"
      

三、服务端示例

  • 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    
  • 添加配置

    • 启动类

      @SpringBootApplication
      @EnableEurekaClient
      @EnableConfigServer
      public class SpringcloudConfigServerApplication {
          public static void main(String[] args) {
              SpringApplication.run(SpringcloudConfigServerApplication.class, args);
          }
      }
      
    • application.yml配置文件

      server:
        port: 8888
      spring:
        application:
          name: config-server
        cloud:
          config:
            server:
              # 配置存储配置信息的Git仓库
              git:
                uri: https://gitee.com/prochick/spring-cloud-config.git
                # Git用户名
                username: xxx
                # Git密码
                password: xxx
                # 指定是否开启启动时直接从git获取配置
                clone-on-start: true
      
      eureka:
        instance:
          prefer-ip-address: true
          instance-id: config-server-8888
        client:
          fetch-registry: false
          register-with-eureka: true
          service-url:
            defaultZone: http://localhost:8010/eureka/
      
  • 访问说明

    # 获取配置信息
    /{label}/{application}-{profile}
    # 获取配置文件信息
    /{label}/{application}-{profile}.yml
    
    • application

      代表应用名称,默认为配置文件中的spring.application.name,如果配置了spring.cloud.config.name,则为该名称

    • label

      代表分支名称,对应配置文件中的spring.cloud.config.label

    • profile

      代表环境名称,对应配置文件中的spring.cloud.config.profile

  • 测试使用

    # 访问http://localhost:8888/master/config-dev来获取master分支上dev环境的配置信息
    # 访问http://localhost:8888/master/config-dev.yml来获取master分支上dev环境的配置文件信息
    # 访问http://localhost:8888/master/config-test.yml来获取master分支上test环境的配置文件信息
    # 访问http://localhost:8888/dev/config-dev.yml来获取dev分支上dev环境的配置文件信息
    

四、客户端示例

  • 添加依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    
  • 添加配置

    • 配置文件

      bootstrap.yml

      server:
        port: 9999
      spring:
        application:
          name: config-client
      
        cloud:
          config:
            # 配置中心地址
            uri: http://localhost:8888
            # 分支名称
            label: master
            # 配置文件名称
            name: config
            # 配置后缀名称
            profile: dev
      
      eureka:
        instance:
          prefer-ip-address: true
          instance-id: config-client-9999
          client:
            fetch-registry: false
            register-with-eureka: true
            service-url:
              defaultZone: http://localhost:8010/eureka/
       
      # 暴露刷新监控 
      management:
        endpoints:
          web:
            exposure:
              include: 'refresh'
      
  • 控制器类

    @RestController
    @RefreshScope
    public class ConfigController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String getConfigInfo() {
    
            return configInfo;
        }
    }
    
    
  • 测试使用

    # 访问http://localhost:9999/configInfo 可以获取到dev分支下dev环境的配置
    
    

五、安全认证示例

  • 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
  • 添加配置

    • 服务端配置

      server:
        port: 8888
      spring:
        application:
          name: config-server
        # 配置存储配置信息的Git仓库
        cloud:
          config:
            server:
              git:
                # 访问地址
                uri: https://gitee.com/prochick/spring-cloud-config.git
                # Git用户名
                username: xxx
                # Git密码
                password: xxx
                # 指定是否开启启动时直接从git获取配置
                clone-on-start: true
        # 配置用户名和密码
        security: 
          user:
            name: xxx
            password: xxx
      
    • 客户端配置

      server:
        port: 9999
      spring:
        application:
          name: config-client
      
        cloud:
          config:
            # 配置中心地址
            uri: http://localhost:8888
            # 分支名称
            label: master
            # 配置文件名称
            name: config
            # 配置后缀名称
            profile: dev
            # 配置中心用户名
            username: xxx
            # 配置中心密码
            password: xxx
      
      

六、集群搭建示例

  • 添加配置

    bootstrap.yml

    server:
      port: 9999
    spring:
      application:
        name: config-client
    
      cloud:
        config:
          # 分支名称
          label: master
          # 配置文件名称
          name: config
          # 配置后缀名称
          profile: dev
          # 集群绑定
          discovery:
            enabled: true
            service-id: config-server
    
    eureka:
      instance:
        prefer-ip-address: true
        instance-id: config-client-9999
        client:
          fetch-registry: true
          register-with-eureka: true
          service-url:
            defaultZone: http://localhost:8010/eureka/
    
  • 测试访问

    # 访问eureka-server
    

    在这里插入图片描述

    # 访问http://localhost:9999/configInfo 可以获取到dev分支下dev环境的配置
    

【源码地址】:GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小吉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值