spring cloud連載第一篇之bootstrap context

1. Spring Cloud Context: Application Context Services(應用上下文服務)

1.1 The Bootstrap Application Context(引導上下文)

一個spring cloud應用會創建一個“bootstrap”context,它是主應用的parent context。它負責加載外部資源的配置屬性并且解釋本地外部配置文件中的屬性。

這里有兩個context,一個是spring boot的main context,另一個是spring cloud的bootstrap context,這兩個context共享同一個環境,也就是說他們共享spring項目的外部配置屬性。

默認情況下,bootstrap屬性(并非是bootstrap.properties而是那些在bootstrap階段加載的屬性)是以高優先級的方式添加的,所以無法被本地配置覆蓋。

bootstrap context和main context使用不同的方法定位外部配置,你可以使用bootstrap.yml來替代application.yml來為bootstrap context添加配置,這樣就可以區分開bootstrap context和main context。

可以通過在系統屬性中設置spring.cloud.bootstrap.enabled=false來禁用bootstrap程序。

1.2 Application Context Hierarchies(應用上下文層級)

如果你通過SpringApplication或者SpringApplicationBuilder構建了一個application context,那么bootstrap context將會作為它的parent context被添加。

spring的一個特性是child context會從它的parent context中繼承屬性資源和配置文件,因此main application context有一些額外的屬性資源:

“bootstrap”:如果在bootstrap context中發現PropertySourceLocators并且含有非空屬性,那么一個CompositePropertySource將會以高優先級出現。

“applicationConfig”:如果你有一個bootstrap.yml,并且設置了配置bootstrap context的屬性,那么它們將會被添加到child context中。但是它們具有比application.yml或者其他配置更低的優先級。

由于資源屬性的排序規則,“bootstrap”入口具有高優先級。注意這不包括bootstrap.yml中的數據(具有較低優先級,可以用來設置默認屬性)。

1.3 Changing the Location of Bootstrap Properties

bootstrap.yml可以通過在系統屬性中設置spring.cloud.bootstrap.name或者spring.cloud.bootstrap.location來指定。

如果有一個激活的配置文件(通過spring.profiles.active或者Environment API設置),那么這些文件中的屬性都會被加載。

1.4 Overriding the Values of Remote Properties(覆蓋遠程屬性的值)

通過bootstrap context添加到應用中的屬性資源可能經常是“遠程”的,例如從Spring Cloud Config Server讀取的屬性。默認情況下,他們不能被本地覆蓋。

如果你希望讓你的應用通過系統屬性或者本地配置來重寫那些遠程配置,可以通過設置遠程屬性資源spring.cloud.config.allowOverride=true(在本地設置無效)。

一旦設置了上面的標志,就可以通過下面兩個遠程屬性來控制遠程屬性和系統屬性跟本地配置的關系:

spring.cloud.config.overrideNone=true:遠程屬性可以被本地任意屬性資源覆蓋

spring.cloud.config.overrideSystemProperties=false:僅僅系統屬性,命令行參數和環境變量(不包括配置文件)可以覆蓋遠程設置。

1.5 Customizing the Bootstrap Configuration(自定義bootstrap配置)

bootstrap context可以被設置來做任何你想要做的事,只要在/META-INF/spring.factories文件中配置org.springframework.cloud.bootstrap.BootstrapConfiguration的值即可。

它的值是以逗號分隔的@Configuration類的全限定名。所以任何你想要在main application context中注入的bean都可以在這里配置。如果你希望控制啟動順序,在類上添加@Order注解(默認順序為最后)。

注意:不要bootstrap配置被main context加載到,即不能被@ComponentScan和@SpringBootApplication注解的配置類覆蓋到。

bootstrap程序最后將初始化器注入到main SpringApplication實例中。首先,通過spring.factories中配置的類來創建bootstrap context,然后所有ApplicationContextInitializer的bean將會被添加到main SpringApplication中,在其啟動前。

1.6 Customizing the Bootstrap Property Sources(定制化bootstrap屬性資源)

通過bootstrap程序添加的外部配置的默認屬性資源是Spring Cloud Config Server。但是可以通過添加PropertySourceLocator類型的bean到bootstrap context中(通過spring.factories)來添加額外的資源。

舉個?:

 1 @Configuration
 2 public class CustomPropertySourceLocator implements PropertySourceLocator {
 3 
 4     @Override
 5     public PropertySource<?> locate(Environment environment) {
 6         return new MapPropertySource("customProperty",
 7                 Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));
 8     }
 9 
10 }

然后在META-INF/spring.factories?文件中添加

1 org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator

1.7?Refresh Scope(刷新域)

當配置改變時,被標記為@RefreshScope的bean會得到一些特殊對待。這個特性將會解決一些beans在僅僅在初始化時注入配置的問題。

例如,數據庫url改變時,數據源已經打開了一些數據庫連接,你可能希望那些已經打開的連接能夠將他們的工作完成,但是當有新的請求來獲取連接時,返回給他們新的url連接。

有時可能需要強制性的在一些只能初始化一次的bean上添加@RefreshScope注解。如果一個bean是“不可變的”,你將不得不為它添加@RefreshScope注解或者在spring.cloud.refresh.extra-refreshable鍵上指定class name。

處在refresh scope中的bean是延遲代理,只有當他們被使用時才初始化,并且這個scope像是一個已初始化值的緩存。為了能使bean在下次使用時重新初始化,必須將它的緩存入口置為無效。

RefreshScope是一個bean,并且有一個公共方法refreshAll(),這個方法通過清理目標緩存的手段來達到刷新在scope中的所有bean。

端點/refresh向外部提供了這個功能(通過HTTP或者JMX)。如果想通過bean的名稱來刷新bean可以使用refresh(String)方法。

要向外暴露/refresh端點,需要在配置文件中寫入以下配置:

1 management:
2   endpoints:
3     web:
4       exposure:
5         include: refresh

注意:@RefreshScope在@Configuration的類上起作用,但可能有一些特殊的行為。比如說,并不意味著所有在@Configuration類中定義的bean都是在@RefreshScope中的。所以任何依賴那些bean的東西都不能在刷新時得到

更新。

1.8?Endpoints(端點)

如果你的應用是一個Spring Boot Actuator 那么會有一些額外的管理端點:

發送一個POST請求到/actuator/env可以更新Environment,重新綁定@ConfigurationProperties?和日志級別;

/actuator/refresh重新加載bootstrap context并且刷新@RefreshScope中的bean;

/actuator/restart重新啟動ApplicationContext(默認此功能是關閉的);

/actuator/pause和/actuator/resume是在ApplicationContext上調用生命周期方法stop()和start()

注意:如果禁止了/actuator/restart端點那么/actuator/pause和/actuator/resume也會被禁止。

?

轉載于:https://www.cnblogs.com/CLAYJJ/p/9632629.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279393.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279393.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279393.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

過Postfix構建Exchange Server 2010郵件網關部署系列三:安裝Exchange 2010先決性條件

1.將Exchange Server 2010服務器加入域。 2.在“開始”菜單上&#xff0c;依次導航到“所有程序”>“附件”>“Windows PowerShell”。打開提升的 Windows PowerShell 控制臺并運行以下命令&#xff1a; Import-Module ServerManager 3.使用 Add-WindowsFeature cmdlet 安…

gmail收件箱標簽設置_通過多個收件箱實驗室有效管理您的Gmail

gmail收件箱標簽設置Most people have more than one email account and if you are using Gmail it’s easy to get things set up so that all of your messages can be accessed in the same place. But if you would prefer to keep things ‘together yet separate’ the …

清華生命學院 2017 就業報告:就業率僅 51%

時間&#xff1a;20170406 一、截至目前生命學院整體就業情況 1.1 系統就業率 1.2 實際排查就業率 (6092)/(68230)51.06%二、本科生就業排查 2017 屆本科生 68 人&#xff0c;已確定去向 60 人&#xff08;已登記去向 32 人&#xff09; 2.1 確定去向的 60 人中 國內深造 35 人…

程序改變了命運,程序生活一天比一天好,對未來也充滿了希望

為什么80%的碼農都做不了架構師&#xff1f;>>> 我出生在內蒙古自治區興安盟扎賚特旗寶力根花蘇木&#xff0c;那里是少數民族蒙古族聚居區&#xff0c;20-30年前與現代城市文明有些差距。當還在讀小學的時在中學當數學老師的爸爸去深圳出差學習&#xff0c;順路在…

powershell 變量_極客學院:學習PowerShell變量,輸入和輸出

powershell 變量As we move away from simply running commands and move into writing full blown scripts, you will need a temporary place to store data. This is where variables come in. 隨著我們不再只是運行命令而轉而編寫完整的腳本&#xff0c;您將需要一個臨時位…

offsetTop、offsetLeft、offsetWidth、offsetHeight、style中的樣式

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd> 假設 obj 為某個 HTML 控件。 obj.offsetTop 指 obj 距離上方或上層控件的位置&#xff0c;整型&#xff0c;單位像素。 obj.offsetLeft 指 obj 距離左方或上層控件的位置&#xff0…

Mock2 moco框架的http協議get方法Mock的實現

首先在Chapter7文件夾下再新建一個startGet.json startget.json代碼如下&#xff0c;因為是get請求&#xff0c;所以要寫method關鍵字&#xff0c;有兩個&#xff0c;一個是有參數&#xff0c;一個是無參數的請求。 [{"description":"模擬一個沒有參數的get請求…

Android 干貨,強烈推薦

本文主要收集 Android開發中常用的干貨技術&#xff0c;現做出目錄&#xff0c;此文不斷更新中&#xff0c;歡迎關注、點贊、投稿。Android 四大組件與布局1. Activity 使用詳解2. Service 使用詳解3. Broadcast 使用詳解4. ContentProvider 使用詳解5. 四大布局 使用詳解6. Re…

imessage_如何在所有Apple設備上同步您的iMessage

imessageMessages in iCloud lets you sync your iMessages across all of your Apple devices using your iCloud account. Here’s how to set it up. 通過iCloud中的消息&#xff0c;您可以使用iCloud帳戶在所有Apple設備上同步iMessage。 設置方法如下。 Apple announced t…

“.Net 社區大會”(dotnetConf) 2018 Day 1 主題演講

Miguel de Icaza、Scott Hunter、Mads Torgersen三位大咖給大家帶來了 .NET Core ,C# 以及 Xamarin的精彩內容&#xff1a;6月份已經發布了.NET Core 2.1, 大會上Scott Hunter 一開始花了大量的篇幅回顧.NET Core 2.1的發布&#xff0c;社區的參與度已經非常高&#xff0c;.NET…

Windows 2003 NTP 時間服務器設置

需要在局域網中架設一臺時間同步服務器&#xff0c;統一各客戶端及服務器的系統時間&#xff0c;在網上查找大多是基于Linux下的 確&#xff2e;&#xff34;&#xff30;服務器&#xff0e;搜索&#xff0c;實驗及總結&#xff0c;寫一篇采用Windwos2003自帶的W32Time服務用于…

React 深入學習:React 更新隊列

path&#xff1a;packages/react-reconciler/src/ReactUpdateQueue.js 更新 export type Update<State> {expirationTime: ExpirationTime, // 到期時間tag: 0 | 1 | 2 | 3, // 更新類型payload: any, // 負載callback: (() > mixed) | null, // 回調函數next: Updat…

長時間曝光計算_如何拍攝好長時間曝光的照片

長時間曝光計算In long exposure photography, you take a picture with a slow shutter speed—generally somewhere between five and sixty seconds—so that any movement in the scene gets blurred. It’s a way to show the passage of time in a single image. Let’s …

思科設備snmp配置。

1、設置IOS設備在IOS的Enable狀態下&#xff0c;敲入 config terminal進入全局配置狀態 Cdp run啟用CDP snmp-server community gsunion ro \\配置本路由器的只讀字串為gsunion snmp-server community gsunion rw \\配置本路由器的讀寫字串為gsunion snmp-server enable trap…

Python——邏輯運算(or,and)

print(0 and 2 > 1) #結果0 print(0 and 2 < 1) #結果0 print(1 and 2 > 1) #結果True print(1 and 2 < 1) #結果False print(2 > 1 and 0) #結果0 print(2 < 1 and 0) #結果False print(2 > 1 and 1) #結果1 print(2 < 1 and 0) #結果False# and 前或…

深度學習入門3

CNN 第一周&#xff1a; title: edge detection example 卷積核在邊緣檢測中的應用&#xff0c;可解釋&#xff0c;卷積核的設計可以找到像素列突變的位置 把人為選擇的卷積核參數&#xff0c;改為學習參數&#xff0c;可以學到更多的特征 title: padding n * n圖片&#xff0c…

圖像大小調整_如何在Windows中調整圖像和照片的大小

圖像大小調整Most image viewing programs have a built-in feature to help you change the size of images. Here are our favorite image resizing tools for Windows. We’ve picked out a built-in option, a couple of third party apps, and even a browser-based tool.…

Spring Data JPA例子[基于Spring Boot、Mysql]

閱讀目錄 關于Spring Data關于Spring Data子項目關于Spring Data Jpa例子&#xff0c;Spring Boot Spring Data Jpa運行、測試程序程序源碼參考資料關于Spring Data Spring社區的一個頂級工程&#xff0c;主要用于簡化數據&#xff08;關系型&非關系型&#xff09;訪問&am…

The way of Webpack learning (IV.) -- Packaging CSS(打包css)

一&#xff1a;目錄結構 二&#xff1a;webpack.config.js的配置 const path require(path);module.exports {mode:development,entry:{app:./src/app.js},output:{path:path.resolve(__dirname,dist),publicPath:./dist/,//設置引入路徑在相對路徑filename:[name].bundle.js…

文本文檔TXT每行開頭結尾加內容批處理代碼

文本文檔TXT每行開頭結尾加內容批處理代碼 讀A.TXT ,每行開頭加&#xff1a;HTMLBodytxt HTMLBodytxt chr(10) aaaaaaaa結尾加&#xff1a;bbbbbbbb處理后的文檔寫入到B.TXT For /f "delims" %%i in (a.txt) do echo HTMLBodytxt HTMLBodytxt chr(10) aaaaaaaa%%…