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

原文地址:https://www.cnblogs.com/heqiyoujing/p/9445475.html

时间: 2024-08-29 14:57:43

springcloud(五):Spring Cloud 配置中心的基本用法的相关文章

记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖

spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 restart always 多节点的服务一直重启关闭重启关闭. 日志文件记录了一个异常: 国内国外搜了一遍都没有解决 org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean wit

spring cloud 注册中心 instance_id 配置

spring cloud 注册中心 instance_id 配置 consul 注册发现中心 instance_id 配置 ip + 端口 一般网上推荐的配置都是 ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} ${random.value} 每次再重启后都会生成一个随机数 会造成每次重启后会在 consul 上重新注册注册上一

微服务SpringCloud之Spring Cloud Config配置中心SVN

在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本篇学习下使用svn作为配置中心. 一.Server 端 1.准备配置文件 这里在本地电脑安装了下svn server,并在https://cuiyw/svn/config-repo/config目录下提交了上一博客的3个配置文件. 2.创建Spring Cloud Config SVN  Serve

SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版

前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心(SpringCloud Config)的配置刷新和消息总线(RabbitMQ和Kafka)使用教程. SpringCloud Config Refresh 在上一篇中我们介绍了springcloud配置中心的本地使用和Git使用的用法,但是当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,需

SpringCloud(9)----mysql实现配置中心

本公司配置数据的管理是通过mysql进行配置管理,因为已经搭建好了,所以自己动手重新搭建一遍,熟悉整个流程.有关项目源码后期会补上github地址 微服务要实现集中管理微服务配置.不同环境不同配置.运行期间也可动态调整.配置修改后可以自动更新的需求,Spring Cloud Config同时满足了以上要求.  项目代码GitHub地址:https://github.com/yudiandemingzi/spring-cloud-study 一.项目搭建 本次主要用三个微服务 (1)Eureka-

Spring Cloud 注册中心Eureka

一.简介 最近在看Spring Cloud微服务,接下来的时间和大家一起分享我所看到的,公司现在用的是dubbo ,之后有时间也去了解了解dubbo的源码.与dubbo相比较,Spring Cloud 在微服务方面有很多全面的实践.今天主要和大家简单介绍一下其中的一个组件Eureka注册中心.Eureka同其他服务注册中心一样,支持高可用配置.如果Eureka以集群模式不熟,当集群中有分片出现故障时,那么Eureka就转入自我保护模式.它允许在分片故障期间继续提供服务的发现和注册,当故障分片恢复

spring cloud 注册中心--eureka注册与发现

本文详细介绍spring cloud微服务的默认注册中心--eureka注册与发现.开发环境需要Windows系统.jdk和intellij idea.与zookeeper注册中心相比,eureka不需要服务器中间件,而且有更高的可用性,但不保证强一致性. 用idea生成spring boot项目,选中jar包Cloud Discovery--Eureka Server,在启动类添加注解@EnableEurekaServer表名该服务为注册中心.下面分单节点和多节点(高可用)分别介绍配置文件:

(五)springcloud 断路器-Spring Cloud Netflix Hystrix

较低级别的服务中的服务故障可能导致级联故障一直到用户. 当对特定服务的调用超过circuitBreaker.requestVolumeThreshold(默认值:20个请求)且失败百分比大于circuit.rolllingStats.timeInMilliseconds定义的滚动窗口中的circuitBreaker.errorThresholdPercentage(默认值:> 50%)时(默认值:10秒) 设计原则: 防止单个服务的故障,耗尽整个系统服务的容器(比如tomcat)的线程资源,避免

SpringCloud(3) Config统一配置中心

config会从git拉取配置文件到本地,然后读取本地文件 eureka: client: service-url: #注册服务端地址 defaultZone: http://localhost:8761/eureka spring: application: name: config cloud: config: server: git: #配置文件git地址 uri: https://gitee.com/yejiaomin/config-repo #git用户名密码 username: xx