欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud Config配置中心原理以及環(huán)境切換方式

 更新時(shí)間:2022年03月01日 14:47:39   作者:向大海走去  
這篇文章主要介紹了SpringCloud Config配置中心原理以及環(huán)境切換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Config配置中心原理以及環(huán)境切換

springCloud config項(xiàng)目,用來為分布式的微服務(wù)系統(tǒng)中提供集成式外部配置支持,分為客戶端和服務(wù)端

spring官方如下介紹:

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

簡(jiǎn)而言之: 通過配置服務(wù)(Config Server)來為所有的環(huán)境和應(yīng)用提供外部配置的集中管理,這些概念都通過spring的Environment和PropertySource來抽象,所以他可以適用于各類Spring應(yīng)用,它也能對(duì)應(yīng)用的開發(fā)環(huán)境、測(cè)試環(huán)境、生成環(huán)境的配置做切換、遷移

原理介紹

git服務(wù)器會(huì)從遠(yuǎn)程git拉取配置文件,并存入到本地git文件庫(kù),當(dāng)遠(yuǎn)程git不可用時(shí),會(huì)從本地git文件庫(kù)拉取配置信息

一、Config Server 引入依賴

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

接入配置中心,讓客戶端可以發(fā)現(xiàn)服務(wù)端,啟動(dòng)類加上@EnableConfigServer,@EnableDiscoveryClient注解,命名chu-config

配置中心服務(wù)器,會(huì)根據(jù)spring.cloud.config.server.git.uri來找到配置數(shù)據(jù)(它可以是git存儲(chǔ)庫(kù)的位置,也可以是本地文件),這是必須的,Config server才能從遠(yuǎn)程Git服務(wù)pull資源來配置在遠(yuǎn)程碼云倉(cāng)儲(chǔ)中新建application.yml和chu-user.yml配置文件

二、Config client

在項(xiàng)目中,基本上所有的基礎(chǔ)微服務(wù)都是config client,它們都通過config server做外部配置集中管理和動(dòng)態(tài)環(huán)境切換

客戶端默認(rèn)拉取規(guī)則如下:

/{name}-{profile}.yml
/{label}-{name}-{profile}.yml
  • name:即spring.application.name
  • profile: 激活的剖面
  • label: git分支,默認(rèn)是master

例如,這里搭建一個(gè)chu-user服務(wù):

引入客戶端依賴

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

在配置文件中通過spring.cloud.config.discovery.enabled=true和spring.cloud.config.discovery.service-id=chu-config來注冊(cè)發(fā)現(xiàn)配置中心服務(wù)端

測(cè)試:

上頭我們介紹了配置中心的拉取規(guī)則

遠(yuǎn)程碼云chu-user.yml上有個(gè)test:123的屬性,在chu-user服務(wù)上可通過@Value("${test}")注解獲取到,如果chu-user服務(wù)上的配置文件中也有個(gè)test:456的屬性,默認(rèn)情況下,Config Server優(yōu)先

經(jīng)過測(cè)試,我們證明了config client可以成功的拉取到遠(yuǎn)程服務(wù)器的配置文件,那么不同環(huán)境的配置切換拉取怎么做呢?

1.在遠(yuǎn)程碼云上改造chu-user.yml配置文件如下:

---
spring:
  profiles: dev
isDev: true
 
---
spring:
  profiles: pro
isDev: false

重啟config server,在瀏覽器輸入localhost:8888/chu-user-dev.yml可以成功拉取到配置信息

2.客戶端通過spring.cloud.config.profile=pro/dev來指定拉取的環(huán)境配置

測(cè)試:?jiǎn)?dòng)config server和config client,并在chu-user服務(wù)控制臺(tái)看到

分別拉取到application#pro環(huán)境和chu-user#pro環(huán)境信息,同時(shí)程序通過@Value("${isDev}")讀取配置值為false

每個(gè)資源也可以選擇在子目錄存儲(chǔ)配置文件,通過關(guān)鍵字searchPaths來查詢定義的目錄,例如

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/China_shenzhen_git/one-config-server
          search-paths: /config,'{application}'

將會(huì)從config目錄和與application相同名字的目錄中開始查詢配置文件

Config server如果希望客戶端能夠授權(quán)訪問配置中心庫(kù),可以引入security配置,引入依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
security:
  basic:
    enabled: true
  user:
    name: config-repo
    password: 123456

那么客戶端就需要增加spring.cloud.config.username=config-repo ,password=123456配置來授權(quán)訪問配置中心服務(wù)器

注意

  • spring profiles進(jìn)行不同環(huán)境版本配置分離、切換,通過spring.profiles.active=dev,mysql,如果配置文件基于文件的,服務(wù)器將優(yōu)先根據(jù){applicationName}.yml,在根據(jù)application.yml創(chuàng)建一個(gè)Environment對(duì)象,如果這些yml文件中有了指定的spring profiles,那么這些profiles將有較高優(yōu)先級(jí)
  • spring.profile.avtive是指定spring boot運(yùn)行的環(huán)境,而spring.cloud.config.profile是客戶端指定拉取資源庫(kù)的profile配置,如果有多個(gè)profiles,最后一個(gè)起作用

簡(jiǎn)易配置中心原理及流程說明

以下將詳細(xì)說明簡(jiǎn)易配置中心原理及流程說明。

原理

在啟動(dòng)后優(yōu)先于spring默認(rèn)配置掃描器增加所需配置項(xiàng),spring將讀取第一個(gè)值使之生效。

源碼詳解

對(duì)spring有了解的朋友都知道,spring對(duì)于默認(rèn)組件或一些配置都是寫在META-INF文件夾下的spring.factories文件中,spring默認(rèn)配置項(xiàng)也是配置在此。

在spring-boot-1.5.9.RELEASE.jar!/META-INF/spring.factories文件中,有一項(xiàng)與配置相關(guān)的配置

# Application Listeners 監(jiān)聽
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.config.ConfigFileApplicationListener
# Environment Post Processors 環(huán)境處理
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor

此監(jiān)聽類為配置文件關(guān)鍵類。

public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {}

可以看到,此監(jiān)聽實(shí)現(xiàn)了EnvironmentPostProcessor,SmartApplicationListener與Ordered。

// 監(jiān)聽回調(diào)函數(shù)
@Override
public void onApplicationEvent(ApplicationEvent event) {
?? ?if (event instanceof ApplicationEnvironmentPreparedEvent) {
?? ? ? ?//在此初始化配置項(xiàng)
?? ??? ?onApplicationEnvironmentPreparedEvent(
?? ??? ??? ??? ?(ApplicationEnvironmentPreparedEvent) event);
?? ?}
?? ?if (event instanceof ApplicationPreparedEvent) {
?? ??? ?onApplicationPreparedEvent(event);
?? ?}
}
//配置文件初始化讀取
private void onApplicationEnvironmentPreparedEvent(
?? ?ApplicationEnvironmentPreparedEvent event) {
?? ?//加載所有EnvironmentPostProcessor類型bean,此時(shí)掃描還未開始,獲取到的都必須是上面說的spring.factories中配置的Environment Post Processors。
?? ?List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
?? ?//增加自己
?? ?postProcessors.add(this);
?? ?//排序
?? ?AnnotationAwareOrderComparator.sort(postProcessors);
?? ?for (EnvironmentPostProcessor postProcessor : postProcessors) {
?? ? ? ?//執(zhí)行
?? ??? ?postProcessor.postProcessEnvironment(event.getEnvironment(),
?? ??? ??? ??? ?event.getSpringApplication());
?? ?}
}
//加載application.yml及指定profile文件
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
?? ??? ?SpringApplication application) {
?? ??? ?//加載
?? ?addPropertySources(environment, application.getResourceLoader());
?? ?configureIgnoreBeanInfo(environment);
?? ?bindToSpringApplication(environment, application);
}

配置中心思路

加載流程及優(yōu)先級(jí)清楚了,那么可以開始動(dòng)手了~

由于執(zhí)行到這步的時(shí)候,還未開始掃描,所以使用注解是無效的。那么新建一個(gè)/META-INF/spring.factories文件,并增加相應(yīng)配置,在ConfigFileApplicationListener之前執(zhí)行即可。

簡(jiǎn)易搭建例子

老規(guī)矩,不會(huì)寫詳細(xì)的步驟,更希望大家能明白原理,舉一反三喲

以修改tomcat端口為例子,由于時(shí)間關(guān)系,只在代碼中模擬獲得配置項(xiàng)。

新建配置類PropertiesConfigPostProcessor

public class PropertiesConfigPostProcessor implements EnvironmentPostProcessor, Ordered {
? ? //優(yōu)先于ConfigFileApplicationListener
? ? public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 9;
? ? @Override
? ? public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
? ? ? ?//此時(shí)PropertySources中無application配置項(xiàng),我們新增一個(gè)server.port為8999的配置。此步驟如為真實(shí)配置中心則可自行獲得。
? ? ? ? environment.getPropertySources().addLast(new MapPropertySource("configServerInitProperties", new QuickHashMap<String, Object>().quickPut("server.port", 8999)));
? ? }
? ? @Override
? ? public int getOrder() {
? ? ? ? return DEFAULT_ORDER;
? ? }
}

新建一個(gè)/META-INF/spring.factories文件,內(nèi)容為

org.springframework.boot.env.EnvironmentPostProcessor=\
top.wboost.example.PropertiesConfigPostProcessor

-application.yml配置

server:
? port: 8000

啟動(dòng)后,日志顯示端口為8999.

我們查看系統(tǒng)中存在的配置項(xiàng)為如下所示(省略其他配置項(xiàng))

{
? ? "data": {
? ? ? ? "applicationConfigurationProperties": {
? ? ? ? ? ? "server.port,class org.springframework.boot.context.config.ConfigFileApplicationListener$ConfigurationPropertySources": 8000
? ? ? ? },
? ? ? ? "configServerInitProperties,class org.springframework.core.env.MapPropertySource": {
? ? ? ? ? ? "server.port": 8999
? ? ? ? }
? ? },
? ? "info": {
? ? ? ? "code": 10906,
? ? ? ? "message": "執(zhí)行成功",
? ? ? ? "systemCode": "DO_OK"
? ? },
? ? "status": 0
}

以上就是簡(jiǎn)易流程,懂得原理,可自行擴(kuò)展!希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

    SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)

    本文主要介紹了SpringBoot配置連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Java實(shí)現(xiàn)下載文件的6種方式

    Java實(shí)現(xiàn)下載文件的6種方式

    本文主要介紹了Java實(shí)現(xiàn)下載文件的6種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的比較

    java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的比較

    本篇文章主要是對(duì)java和c/c++ 數(shù)據(jù)類型長(zhǎng)度的進(jìn)行了詳細(xì)的比較。需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2014-01-01
  • JVM系列之String.intern的性能解析

    JVM系列之String.intern的性能解析

    這篇文章主要介紹了JVM系列之String.intern的性能解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • springboot整合多數(shù)據(jù)源配置方式

    springboot整合多數(shù)據(jù)源配置方式

    這篇文章主要介紹了springboot整合多數(shù)據(jù)源配置,多數(shù)據(jù)源整合springboot+mybatis使用分包方式整合,springboot+druid+mybatisplus使用注解整合,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • 在springboot中如何給mybatis加攔截器

    在springboot中如何給mybatis加攔截器

    這篇文章主要介紹了在springboot中如何給mybatis加攔截器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java 導(dǎo)出excel進(jìn)行換行的案例

    Java 導(dǎo)出excel進(jìn)行換行的案例

    這篇文章主要介紹了Java 導(dǎo)出excel進(jìn)行換行的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • springboot加載復(fù)雜的yml文件獲取不到值的解決方案

    springboot加載復(fù)雜的yml文件獲取不到值的解決方案

    這篇文章主要介紹了springboot加載復(fù)雜的yml文件獲取不到值的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot自動(dòng)配置原理分析

    SpringBoot自動(dòng)配置原理分析

    這篇文章主要介紹了SpringBoot自動(dòng)配置原理分析,SpringBoot是我們經(jīng)常使用的框架,那么你能不能針對(duì)SpringBoot實(shí)現(xiàn)自動(dòng)配置做一個(gè)詳細(xì)的介紹。如果可以的話,能不能畫一下實(shí)現(xiàn)自動(dòng)配置的流程圖。牽扯到哪些關(guān)鍵類,以及哪些關(guān)鍵點(diǎn)
    2022-08-08
  • java反射超詳細(xì)講解

    java反射超詳細(xì)講解

    本文非常詳細(xì)的講解了java反射具體的內(nèi)容以及使用,java反射在現(xiàn)今的使用中很頻繁,希望此文可以幫大家解答疑惑,可以幫助大家理解
    2021-08-08

最新評(píng)論