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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM