configuration service
1.0.2
| 包裹 | 最新版本 |
|---|---|
| configurationservice.hosting | |
| configurationservice.client |
配置服務是.NET的分佈式配置服務。可以立即更新應用程序,服務和容器化的微服務的配置,而無需重新部署或重新啟動。配置服務使用客戶端/服務器/子體系結構在發生時將配置更改通知訂閱客戶端。可以使用標準選項模式與IOptions , IOptionsMonitor或IOptionsSnapshot注入配置。
配置服務當前支持使用GIT,文件系統或金庫後端託管配置,並支持使用REDIS,NATS或RABBITMQ PUBLISS/subscribe的發布更改。支持的文件類型為.json,.yaml,.xml和.ini。
ConfigurationBuilder模式輕鬆地集成到任何.NET標準2.0應用程序中。IOptionsMonitor或IOptionsSnapshot注入配置,以訪問配置更改。 安裝配置服務的最簡單方法是使用Nuget。
在Visual Studio的Package Manager控制台中,輸入以下命令:
伺服器:
Install-Package ConfigurationService.Hosting
客戶:
Install-Package ConfigurationService.Client
配置服務主機中間件可以添加到現有ASP.NET應用程序的服務集合中。以下示例將配置帶有Redis Publisher的GIT存儲提供商。
builder . Services . AddConfigurationService ( )
. AddGitProvider ( c =>
{
c . RepositoryUrl = "https://github.com/jamespratt/configuration-test.git" ;
c . LocalPath = "C:/local-repo" ;
} )
. AddRedisPublisher ( "localhost:6379" ) ;在startup.configure中,在端點構建器上調用MapConfigurationService 。默認模式為“/配置”。
app . UseEndpoints ( endpoints =>
{
endpoints . MapConfigurationService ( ) ;
} ) ;配置的主機將公開兩個API端點:
configuration/ - 在配置的提供商處列出所有文件。configuration/{filename} - 檢索指定文件的內容。 | 財產 | 描述 |
|---|---|
| RepositoryUrl | 遠程存儲庫的URI。 |
| 使用者名稱 | 身份驗證的用戶名。 |
| 密碼 | 身份驗證的密碼。 |
| 分支 | 結帳分支的名稱。未指定後,將使用遙控器的默認分支。 |
| Local Path | 局部路徑克隆進入。 |
| 搜索頁 | 搜索字符串用作文件名稱的過濾器。默認為沒有過濾器(*)。 |
| 民意測驗Interval | 檢查遠程更改的間隔。默認為60秒。 |
services . AddConfigurationService ( )
. AddGitProvider ( c =>
{
c . RepositoryUrl = "https://example.com/my-repo/my-repo.git" ;
c . Username = "username" ;
c . Password = "password" ;
c . Branch = "main" ;
c . LocalPath = "C:/config" ;
c . SearchPattern = ".*json" ;
c . PollingInterval = TimeSpan . FromSeconds ( 60 ) ;
}
.. . | 財產 | 描述 |
|---|---|
| 小路 | 配置文件的路徑。 |
| 搜索頁 | 搜索字符串用作文件名稱的過濾器。默認為沒有過濾器(*)。 |
| 包括求職者 | 包括當前目錄及其所有子目錄。默認為false 。 |
| 使用者名稱 | 身份驗證的用戶名。 |
| 密碼 | 身份驗證的密碼。 |
| 領域 | 身份驗證的域。 |
services . AddConfigurationService ( )
. AddFileSystemProvider ( c =>
{
c . Path = "C:/config" ;
c . SearchPattern = "*.json" ;
c . IncludeSubdirectories = true ;
} )
.. . | 財產 | 描述 |
|---|---|
| Serveruri | 帶端口的Vault Server URI。 |
| 小路 | 啟用KV Secrets引擎的路徑。 |
| Authmethodinfo | 用於獲取保庫令牌的驗證方法。 |
| 民意測驗Interval | 檢查遠程更改的間隔。默認為60秒。 |
services . AddConfigurationService ( )
. AddVaultProvider ( c =>
{
c . ServerUri = "http://localhost:8200/" ;
c . Path = "secret/" ;
c . AuthMethodInfo = new TokenAuthMethodInfo ( "myToken" ) ;
} )
.. . 可以通過實現IProvider和IPublisher接口並調用AddConfigurationservice上的適當擴展方法來添加存儲提供商和發布者的自定義實現:
services . AddConfigurationService ( )
. AddProvider ( new CustomStorageProvider ( ) )
. AddPublisher ( new CustomPublisher ( ) ) ; 可以通過將AddRemoteConfiguration添加到標準配置構建器中來配置配置服務客戶端。在下面的示例中,添加了遠程JSON配置,並指定了redis端點以進行配置更改訂閱。可以使用多個配置構建器實例讀取本地配置的遠程源設置。
var loggerFactory = LoggerFactory . Create ( builder =>
{
builder . AddConsole ( ) ;
} ) ;
IConfiguration configuration = new ConfigurationBuilder ( )
. AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
. Build ( ) ;
configuration = new ConfigurationBuilder ( )
. AddConfiguration ( configuration )
. AddRemoteConfiguration ( o =>
{
o . ServiceUri = "http://localhost:5000/configuration/" ;
o . AddConfiguration ( c =>
{
c . ConfigurationName = "test.json" ;
c . ReloadOnChange = true ;
c . Optional = false ;
} ) ;
o . AddConfiguration ( c =>
{
c . ConfigurationName = "test.yaml" ;
c . ReloadOnChange = true ;
c . Optional = false ;
c . Parser = new YamlConfigurationFileParser ( ) ;
} ) ;
o . AddRedisSubscriber ( "localhost:6379" ) ;
o . AddLoggerFactory ( loggerFactory ) ;
} )
. Build ( ) ; | 財產 | 描述 |
|---|---|
| Serviceuri | 配置服務端點。 |
| httpmessagehandler | HttpClient的可選HttpMessageHandler 。 |
| requestTimeOut | HttpClient請求到配置服務器的超時。默認為60秒。 |
| loggerFactory | 用於配置記錄系統並創建ILogger實例的類型。默認為NullLoggerFactory 。 |
| 添加配置 | 添加一個單獨的配置文件。 |
| ConfigurationName | 相對於配置提供商,配置文件的路徑或名稱。該值應匹配configuration/端點返回的列表中指定的值。 |
| 選修的 | 確定加載文件是否是可選的。 |
| realoadonChange | 確定如果基礎文件更改,是否將加載源。 |
| 解析器 | 用於解析遠程配置文件的類型。如果未指定,客戶端將嘗試從ConfigurationName的文件擴展程序中解決此問題。支持類型:
|
| 添加了criber | 將Redis添加為配置訂戶。 |
| addnatssubscriber | 將NAT添加為配置訂戶。 |
| addrabbitmqsubscriber | 將RabbitMQ添加為配置訂戶。 |
| 添加了bubscriber | 添加自定義配置訂閱者Insterments ISubscriber 。 |
| AddLoggerFactory | 添加用於配置記錄系統並創建ILogger實例的類型。 |
可以在樣本中查看主機和客戶端實現的樣本。