zoukankan      html  css  js  c++  java
  • springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法

    1. 概述

    本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明。 
    配置中心服务器部分内容包括:服务创建,git,svn,native后端的配置,各种url访问 
    配置中心客户端部分内容包括:访问配置、failfast,重试

    2. Spring Cloud Config的服务端

    2.1. 简述

    我们在开发大的系统时,由于服务较多,相同的配置(如数据库信息、缓存、开关量等)会出现在不同的服务上,如果一个配置发生变化,则可能需要修改很多的服务配置。为了解决这个问题,spring cloud提供配置中心。 
    首先所有的公共配置存储在相同的地址(存储的地方可以是git,svn和本地文件),然后配置中心从这些地方读取配置以restful发布出来,其它服务可以调用接口获取配置信息。

    2.2. 配置服务

    引入关键jar

     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
     </dependency>

    通过@EnableConfigServer可以激活配置中心服务。配置中心可以单独做服务,也可以嵌入到其它服务中。推荐用单独做服务方式使用配置中心。

    @SpringBootApplication
    @EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务
    public class CloudGitConfigServerApplication {
    
        public static void main(String[] args) {
            args = new String[1];
            args[0] = "--spring.profiles.active=gitsimple2";
            SpringApplication.run(CloudGitConfigServerApplication.class, args);
        }
    }

    由于配置文件的存储的多样性,下面介绍每种配置形式如何配置。所有的配置都配置在application-*.yml中

    2.3. git后端

    Spring Cloud配置中心的后端系统可以是:

    • VCS(如git,svn等)
    • 本地文件

    本节我们介绍git配置 
    配置参数主要配置中application-gitsimple2.yml

    spring:
      application:
        name: special
      cloud:
        config:
          server:
            git:
              #  配置文件只搜索url目录下的searchPaths
              uri: https://github.com/hryou0922/spring_cloud.git
              # 指定搜索路径,如果有多个路径则使用,分隔
              searchPaths: cloud-config-git/simple2/configspecial,cloud-config-git/simple2/default
              # 对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件
              basedir: /tmp/spring-cloud-repo
              # 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
              force-pull: true

    spring.cloud.config.server.git.url:指定配置文件所在远程git库的url地址 

    spring.cloud.config.server.git.searchPaths:和上面的参数url配合使用,定位git库的子目录。指定搜索路径,如果有多个路径则使用,分隔

    spring.cloud.config.server.git.basedir:对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件。默认存储在系统临时目录下,目录名的前缀为config-repo-,如在linux下时可能是/tmp/config-repo-。因为/tmp下的内容有可能被误删,所有为了保险,最好修改存储目录。如果你修改存储目录,你可以修改spring.cloud.config.server.git.basedir

    spring.cloud.config.server.git.force-pull:配置中心通过git从远程git库读取数据时,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置。设置force-pull=true,则强制从远程库中更新本地库

    以上是一些常用的配置,其它配置可以自己看配置类MultipleJGitEnvironmentRepository类

    2.4. svn后端

    svn的配置方法和git差不多,主要使用”spring.cloud.config.server.svn.*”。这里略

    2.5. 文件系统后端

    除了使用从git/svn下载配置文件,你可以从classpath目录或本地文件系统中加载配置文件。通过spring.cloud.config.server.native.searchLocations配置地址.这里又分为两类:

    • 从本地目录加载配置文件:以file开头 
      • file:///${user.home}/config-repo 
        默认值:file:./, file:./config
    • 从classpath中加载配置文件:以classpath开头 
      • 如果不配置值,则默认值:classpath:/, classpath:/config

    以classpath为例,file的用法和classpath用法相同,这里略.

    spring:
      profiles:
        # native:启动从本地读取配置文件,必须指定active的值,才可以使用本地文件配置模式
        active: native
        # 自定义配置文件路径
      cloud:
        config:
          server:
            native:
              searchLocations: classpath:/config/simple2/

    spring.profiles.active: 如果使用本地系统配置,则此值必须是native 
    spring.cloud.config.server.native.searchLocations: 指定配置文件的路径

    2.6. 启动和测试

    通过CloudGitConfigServerApplication就可以启动服务

    在浏览器中输入如下URL,可以访问到配置文件

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties

    下面通过具体例子说明以上url的意思。如果我们的配置文件名称cloud-config-simple2.yml,则其和URL中各个字段对应的值为:

    • application: cloud-config
    • profile: simple2
    • label: 9500e50f08c43e3e4391175c8f6d5a326b11302f

    我们访问以下地址都可以访问到配置文件config-simple2.yml和特定版本下此文件: 
    http://127.0.0.1:10888/cloud-config/simple2 
    http://127.0.0.1:10888/cloud-config/simple2/9500e50f08c43e3e4391175c8f6d5a326b11302f 
    http://127.0.0.1:10888/cloud-config-simple2.yml 
    http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.yml 
    http://127.0.0.1:10888/cloud-config-simple2.properties 
    http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.properties

    3. Spring Cloud Config的客户端

    配置中心服务端配置成功后,然后其它服务从配置中心获取配置文件,这样的服务被称为客户端。

    3.1. 配置客户端

    引入jar:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>

    只要是@SpringBootApplication注解启动的spring boot即可

    @SpringBootApplication
    public class SimpleCloudServiceApplication {
    
        public static void main(String[] args) {
            args = new String[1];
            args[0] = "--spring.profiles.active=simple2";
            SpringApplication.run(SimpleCloudServiceApplication.class, args);
        }
    }

    3.2. 配置参数

    请将配置中心的相关配置配置在bootstrap-.yml中,不要配置appliaction-.yml。因为服务启动时,会从bootstrap中读取配置,然后从远程配置中心读取配置文件,最后再从appliaction中获取配置,如果有相同的配置项,则后面的会覆盖前面读到的值。所以如果配置中心的配置配置在appliaction,则配置项不会有任何效果。

    bootstrap-simple2.yml

    spring:
      cloud:
        # 配置服务器的地址
        config:
          uri: http://127.0.0.1:10888
          # 要读取配置文件读取的值
          name: cloud-config
          # 如果不设置此值,则系统设置此值为 spring.profiles.active
          profile: dev
          # 可以使用之前的版本。默认值可以是git label, branch name or commit id。可以使用多个Label,多个Label可以使用逗号分隔
          # label:
          # true: 如果访问配置中心失败,则停止启动服务
          fail-fast: true
          # 配置重试,默认是重试6次,最初是延迟1s再次重试,如果再失败,则延迟1.1*1s、1.1*1.1*1s、… 。可以使用这个配置
          retry:
            initial-interval: 2000
            # 最多重试次数
            max-attempts: 6
            # 最大重试间隔
            max-interval: 4000
            # 每次重试时间是之前的倍数
            multiplier: 1.2

    重要参数的解释如下: 
    配置中心的url 
    即从哪里读取配置文件,通过“spring.cloud.config.url”配置

    要读取哪些配置文件 
    由以下参数共同决定,和”2.6. 启动和测试”结合看加深理解。 
    - “spring.cloud.config.name”:配置文件名称,对应上文的读取URL中的{applicaion}值 
    - “spring.cloud.config.profile”:配置文件的profile,对应上文的URL中的{profile}值 
    - “spring.cloud.config.label”: 可以使用之前的版本。默认值可以是git label, branch name or commit id。可以使用多个Label,多个Label可以使用逗号分隔

    快速失败 
    如果要求客户端访问配置中心失败,则立即停止启动服务,则设置“spring.cloud.config.label”为 true

    重试 
    如果访问配置失败,则自动重试。默认是重试6次,最初是延迟1s再次重试,如果再失败,则延迟1.1*1s、1.1*1.1*1s、… 。通过下面参数可以修改值:

    • “spring.cloud.config.retry.initial-interval”:第一次失败,延迟多久重试
    • “spring.cloud.config.retry.max-attempts”:最多重试次数
    • “spring.cloud.config.retry.max-interval”: 最大重试间隔
    • “spring.cloud.config.retry.multiplier”: 每次重试时间是之前的倍数

      如果要实现重试功能,需要引入新的jar

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
            </dependency>

    3.3. 启动和测试

    启动SimpleCloudServiceApplication,在浏览器输入http://127.0.0.1:10082/simple,会返回以下信息,则表示成功

    {"age":112,"name":"git2-default-dev","randomNum":53}

    转自:https://blog.csdn.net/hry2015/article/details/77870854?utm_source=tuicool&utm_medium=referral
  • 相关阅读:
    社会科学 | 游戏开发 | 浅谈游戏中的符号象征主义
    数据挖掘 | 数据隐私(4) | 差分隐私 | 差分隐私概论(下)(Intro to Differential Privacy 2)
    数据挖掘 | 数据隐私(3) | 差分隐私 | 差分隐私概论(上)(Intro to Differential Privacy 1)
    数据挖掘 | 数据隐私(2) | 差分隐私 | 数据重构化攻击(Reconstruction Attacks)
    数据挖掘 | 数据隐私(1) | 差分隐私 | 挑战数据隐私(Some Attempts at Data Privacy)
    Opencv | 图形学 | Mingw64 | 如何正确地用MinGW64编译与配置vscode的Opencv环境
    机器学习 | 强化学习(8) | 探索与开发(Exploration and Exploitation)
    机器学习 | 强化学习(7) | 融合学习与规划(Integrating Learning and Planning)
    机器学习 | 强化学习(5) | 价值函数拟合(Value Function Approximation)
    C#进阶
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/9445475.html
Copyright © 2011-2022 走看看