日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

你可能不知道的.Net Core Configuration

發布時間:2023/12/4 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 你可能不知道的.Net Core Configuration 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

執行原理

  1. 配置讀取順序:與代碼先后順序一致。

public Startup(IHostingEnvironment env)

? ? {

? ? ? ? var builder = new ConfigurationBuilder()

? ? ? ? ? ? .SetBasePath(env.ContentRootPath)

? ? ? ? ? ? .AddJsonFile("appsettings.json", false, true)

? ? ? ? ? ? .AddJsonFile("cussettings.json", false, true);

? ? ? ? Configuration = builder.Build();

? ? }

以上代碼會先讀取appsettings.json,再讀取cussettings.json,cussettings.json的內容會覆蓋appsettings.json的內容。

?

  2. 覆蓋邏輯:原有的配置繼承,相同的配置覆寫,新增的配置添加。

  appsettings.json:

? ?{ ? ? ?"settings": { ? ?
? ?
"name": "AppSetting", ? ?
? ?
"age": 20 } }

  cussettings.json

? ?{ ? ? ?"settings": { ? ? ?
?
"name": "CusSetting", ? ?
? ?
"gender": "Male"} }

  結果:

  

?  

  3. 可以設置配置文件不存在或失效時,程序不會被中止,該配置會被忽略。

  如cussettings.json不存在或失效時:

  

?

環境變量

  1.?appsettings.{env.EnvironmentName}.json

  可以根據當前的環境變量設置讀取對應的配置,來覆蓋之前的配置,有點像Asp.Net的Web Transform,其中環境變量的key為:ASPNETCORE_ENVIRONMENT。

  可以在四個地方設置該環境變量:

  a) Visual Studio 2017中的launchSettings.json

  

  b) 操作系統的環境變量

  Windows:

  

?

  Linux:

  

?

  c) Dockerfile

  FROM microsoft/aspnetcore:1.1
  COPY . /app
  WORKDIR /app
  EXPOSE 5000/tcp
  ENV ASPNETCORE_URLS http://*:5000/
  ENV?ASPNETCORE_ENVIRONMENT Production
  ENTRYPOINT ["dotnet", "EnvironmentVariable.dll"]

?

  d) Docker啟動指令

  docker run --name {name} -e?ASPNETCORE_ENVIRONMENT=Production ...

  如:

  

?

  2.?AddEnvironmentVariables()的作用

var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json", false, true).AddJsonFile($"appsettings.{env.EnvironmentName}.json", true).AddEnvironmentVariables();//作用?Configuration = builder.Build();

  a) 讀取系統的環境變量信息,這個應該大部分人都知道。

  b) 覆蓋之前的配置信息。

  appsettings.json:

{
?
"settings": { ?
?
"name": "AppSetting"} }

  通過環境變量來覆蓋settings:name:

  

  

  說明:這里是用控制臺運行,因為操作系統的環境變量要對IIS Express生效,要重啟vs2017,我懶!

  另外環境變量使用兩下劃線(__)作為層次的分隔符,具體可參考EnvironmentVariablesConfigurationProvider的源碼。

  

?  

Spring Cloud Config Server

  Config Server是Spring Cloud 配置管理的重要中間件,接下來,我們看一下.Net Core如何跟Config Server進行交互。

  1. 準備配置文件,這里使用Git作為配置文件的Source,地址:https://github.com/ErikXu/.NetCore-Configuration

  2. 獲取安裝包,這里使用Docker啟動,因此,是拉取官方鏡像:docker pull hyness/spring-cloud-config-server

  

  3.??準備啟動資源文件application.yml


info:??component: config service??server:??port: 8888??spring:??application:????name: git-config??profiles:????active: dev??cloud:????config:??????server:????????git:??????????uri: https://github.com/ErikXu/.NetCore-Configuration??????????searchPaths: Configs

  4. 執行指令啟動Config Server

  docker run --name configsvr -it -d -p 8888:8888 -v /root/config-server/application.yml:/config/application.yml hyness/spring-cloud-config-server

?

?  5. 引入Nuget包

  Install-Package Steeltoe.Extensions.Configuration.ConfigServer -Version 1.1.1

?

  6. 準備appsetting.json

{

? "spring": {

? ? "application": {

? ? ? "name": "foo"

? ? },

? ? "cloud": {

? ? ? "config": {

? ? ? ? "uri": "http://192.168.52.142:8888",

? ? ? ? "validate_certificates": false,

? ? ? ? "env": "dev"?

? ? ? }

? ? }

? },

? "settings": {

? ? "name": "AppSetting"//會被Config Server的內容覆蓋

? }

}

7. 引入Config Server

  

  結果:

  

?

掛卷Volume

  掛卷是docker的一種機制,可以把特定的目錄或者文件掛載到docker容器的指定位置。配合.Net Core Configuration的機制,可以在appsetting.json中記錄開發環境的配置,然后再指定一個待掛載的文件用于記錄不同環境的配置,從而覆蓋開發環境的配置,達到多環境適配的能力。

  1. appsetting.json


{??"settings": {????"name":?"AppSetting"??}}

  2. 指定一個待掛載的目錄,但是在開發環境不存在此文件。

public Startup(IHostingEnvironment env)

? ? {

? ? ? ? var builder = new ConfigurationBuilder()

? ? ? ? ? ? .SetBasePath(env.ContentRootPath)

? ? ? ? ? ? .AddJsonFile("appsettings.json", false, true)

? ? ? ? ? ? .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)

? ? ? ? ? ? .AddEnvironmentVariables()

? ? ? ? ? ? .AddJsonFile("/configs/volume.json", true, true); //待掛載


? ? ? ? Configuration = builder.Build();

? ? }

3. 開發環境

  

?

  4. docker啟動指令

  docker run?--name {name} -v {source}:{target} ...

  

  注:如果有權限問題,需要先執行setenforce 0

?

  5. 效果

  

?

?  掛卷感覺跟appsettings.{env.EnvironmentName}.json很像,既然如此,為什么還要有掛卷呢?那是因為appsettings.{env.EnvironmentName}.json是.net core的機制,其它語言不一定會有,或者是另外的機制,docker的掛卷是服務各種語言的。.Net Core是很優秀的語言,它接收了很多業界的需求,也響應了docker掛卷的機制,在這一點上,.net的能力就相對較弱了。

  

Config Server vs Volume

  Config Server是Spring Cloud體系的配置管理中心,而Kubernetes更多是使用Volume的方式(HostPath,Config Map,Secret)。

  Config Server是運行時的配置管理,它會定期檢測(心跳,2s)remote config的內容,如果內容更新,就刷新容器的配置信息。Volume是發布(更新)時的配置管理,在發布(更新)期間,就把配置信息掛載到容器中。

  Config Server要自己保證其高可用,否則,Config Server掛了,會讀取容器內的原始配置,Volume是把文件掛載到容器中,因此無中間件高可用要求,但要利用Kubernetes等保證發布更新時,配置掛載到了各個容器中。

  Config Server具有一定的語言侵入性(必須要有驅動jdk或者sdk),Volume無語言侵入性。

  Config Server只支持Yaml等少量配置文件類型,Volume支持各種配置文件類型。

原文:http://www.cnblogs.com/Erik_Xu/p/7624323.html


.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結

以上是生活随笔為你收集整理的你可能不知道的.Net Core Configuration的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。