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实例的类型。 |
可以在样本中查看主机和客户端实现的样本。