gpt4 book ai didi

.net - 如何使用 Dapr 订阅 Azure 应用程序配置中的更改

转载 作者:行者123 更新时间:2023-12-02 07:51:29 25 4
gpt4 key购买 nike

我想使用 Dapr 订阅 Azure 应用程序配置存储中的更改,如本页所述:https://docs.dapr.io/reference/components-reference/supported-configuration-stores/azure-appconfig-configuration-store/

当我运行应用程序时,我成功地从 Azure 应用程序配置存储中读取配置,但是当我更新 SentinelKey 时,我没有收到任何更改。

我有一个 .NET 应用程序,它使用 Dapr SDK 进行订阅,代码如下:

/// <summary>
/// Subscribes to configuration changes using the DaprClient
/// and logs each change to the console in real time.
/// </summary>
/// <param name="stoppingToken"></param>
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine("Subscribing to configuration changes on the following keys:");

foreach (var key in CONFIGURATION_KEYS)
{
Console.WriteLine(key);
}

// Initialize the gRPC Stream that will provide configuration updates.
Dapr.Client.SubscribeConfigurationResponse subscribe = await _daprClient.SubscribeConfiguration(
DAPR_CONFIGURATION_STORE,
CONFIGURATION_KEYS,
new Dictionary<string, string>
{
{ "sentinelKey", "TestApp:Settings:Sentinel"}
}, stoppingToken);

// The response contains a data source which is an IAsyncEnumerable, so it can be iterated through via an awaited foreach.
await foreach (var configItems in subscribe.Source.WithCancellation(stoppingToken))
{
// First invocation when app subscribes to config changes only returns subscription id
if (configItems.Keys.Count == 0)
{
Console.WriteLine("Subscribed to config changes with subscription id: " + subscribe.Id);
continue;
}

var cfg = System.Text.Json.JsonSerializer.Serialize(configItems);
Console.WriteLine("Configuration update: " + cfg);
}
}

我的 Dapr 配置存储组件如下所示:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: configstoreazureappconfig
spec:
type: configuration.azure.appconfig
version: v1
metadata:
- name: connectionString
value: <CONNECTION-STRING-HERE>

当我将此代码与 Redis 一起用作配置存储时,它按预期工作,配置更新将发送到我的应用程序并记录到控制台。即使我更新 Azure 中的 sentinelKey 值,我也没有收到任何 Azure 应用程序配置更新。

当我运行应用程序时,我得到以下输出:

| Subscribing to configuration changes on the following keys:
| orderId1
| orderId2
| TestApp:Settings:Sentinel
| info: Microsoft.Hosting.Lifetime[14]
| Now listening on: http://[::]:80
| info: Microsoft.Hosting.Lifetime[0]
| Application started. Press Ctrl+C to shut down.
| info: Microsoft.Hosting.Lifetime[0]
pocnativevoicesessions-poc.nativevoice.sessions-1 | Hosting environment: Development
| info: Microsoft.Hosting.Lifetime[0]
| Content root path: /app
| Subscribed to config changes with subscription id: 22b7dce1-7a89-4de1-bc57-87145937cc1f
| Configuration update: {"TestApp:Settings:Sentinel":{"Value":"8","Version":"","Metadata":{}},"orderId1":{"Value":"102","Version":"","Metadata":{}},"orderId2":{"Value":"10","Version":"","Metadata":{}}}

所以当应用程序启动时我确实得到了配置,但我只是没有得到任何更新。 Dapr sidecar 也没有记录任何错误。

我发现有关使用 Dapr 订阅 Azure 应用程序配置更改的实现的讨论: https://github.com/dapr/components-contrib/issues/2060

有谁知道如何使用 Dapr 配置存储组件订阅 Azure 应用程序配置更改?提前致谢!

最佳答案

从源代码来看,好像可以subscribe for changes ,但您必须提供哨兵 key 作为元数据的一部分。

另请注意 defaultSubscribePollInterval是24小时。您可能希望将其更改为更短的内容,至少在测试期间是这样。

关于.net - 如何使用 Dapr 订阅 Azure 应用程序配置中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76414333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com