【C#】在.NET Aspire 中使用 Dapr 配置(Configuration)

【C#】在.NET Aspire 中使用 Dapr 配置(Configuration)

一、AppHost项目

configstore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: configstore
spec:
  type: configuration.redis
  version: v1
  metadata:
  - name: redisHost
    value: localhost:6379
  - name: redisPassword
    value: ""

在AppHost项目添加组件:

// 添加配置组件
var configStore = builder.AddDaprComponent("configstore", "configuration.redis",
    new DaprComponentOptions
    {
        LocalPath = "Resources\\configstore.yaml"
    });

在其他项目中添加引用:

builder.AddProject<Projects.Aspire_WPF>("wpfclient")
	// ...
	.WithReference(configStore)  // 添加引用
	// ...

二、使用方法

private const string storeName = "statestore";
private const string DAPR_CONFIGURATION_STORE = "configstore";
private static List<string> CONFIGURATION_ITEMS = new List<string> { "key1", "key2" };

1、读取配置

GetConfigurationResponse config = await _client.GetConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);

foreach (var item in config.Items)
{
	var cfg = System.Text.Json.JsonSerializer.Serialize(item.Value);
	Console.WriteLine("Configuration for " + item.Key + ": " + cfg);
}

2、订阅更新

SubscribeConfigurationResponse subscribe = await _client.SubscribeConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);

await foreach (var items in subscribe.Source)  // 这里阻塞:第一次调用、配置更新时通过,取消订阅后结束
{
	// 首次调用时只返回订阅Id
	if (items.Keys.Count == 0)
	{
		Console.WriteLine("App subscribed to config changes with subscription id: " + subscribe.Id);
		subscriptionId = subscribe.Id;  // 暂存Id用于取消订阅时使用
		continue;
	}
	
	// 配置更新
	var cfg = System.Text.Json.JsonSerializer.Serialize(items);
	Console.WriteLine("Configuration update " + cfg);
}

3、取消订阅

try
{
	await _client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, subscriptionId);
	Console.WriteLine("App unsubscribed from config changes");
}
catch (Exception ex)
{
	Console.WriteLine("Error unsubscribing from config updates: " + ex.Message);
}

4、设置/修改配置

> redis-cli -a 密码 MSET key1 "value1" key2 "value2"
### 配置 Dapr配置文件 在 Docker 环境中使用 Dapr 时,可以通过配置文件定义组件、服务行为以及集成外部服务(如 Consul)。Dapr配置文件通常以 YAML 格式编写,并通过 `--components-path` 参数指定其所在目录。 Dapr 的核心配置文件包括组件定义(如服务发现、状态存储等)以及应用级别的配置。例如,可以创建一个 `components` 目录,并在其中放置 `dapr-consul-component.yaml` 文件,用于定义 Consul 作为服务发现组件: ```yaml apiVersion: dapr.io/v1alpha1 kind: Component metadata: name: consul-service-discovery spec: type: serviceDiscovery version: v1 metadata: - name: consulAddress value: "http://consul:8500" ``` 此配置文件将 Dapr 配置使用 Consul 服务发现,并指定 Consul 的 HTTP 地址为 `http://consul:8500` [^3]。 ### 在 Docker 中使用 Dapr 配置文件 在 Docker 容器中使用 Dapr 时,需要确保容器能够访问配置文件,并正确加载组件。可以通过 `docker-compose.yml` 文件定义服务及其依赖项,并挂载配置文件目录。 例如,以下是一个包含 Dapr 配置的 `docker-compose.yml` 示例: ```yaml version: '3.4' services: myfrontend: image: ${DOCKER_REGISTRY-}myfrontend build: context: . dockerfile: MyFrontEnd/Dockerfile ports: - "51000:50001" myfrontend-dapr: image: "daprio/daprd:latest" command: [ "./daprd", "-app-id", "MyFrontEnd", "-app-port", "80", "-components-path", "/components" ] volumes: - ./components:/components depends_on: - myfrontend network_mode: "service:myfrontend" ``` 在此配置中,`-components-path` 参数指定 Dapr 从 `/components` 目录加载组件配置,`volumes` 挂载了本地的 `components` 目录到容器中,确保 Dapr 能够读取配置文件 [^5]。 ### 应用配置文件并运行 Dapr 边车 在 Docker 容器中运行 Dapr 边车时,需要确保 Dapr 服务能够访问 Consul 或其他服务发现组件。通常,Dapr 边车与应用容器共享网络命名空间,以便通过服务名称直接访问其他服务。 例如,以下命令可用于启动 Dapr 边车并加载配置文件: ```bash dapr run --app-id myapp --app-port 8080 --dapr-http-port 3500 --components-path ./components ``` 该命令指定 Dapr 边车使用 `./components` 目录中的配置文件,并加载其中定义的组件 。 ### 网络配置与服务访问 Docker 容器之间的网络配置至关重要。为了确保 Dapr 能够正确访问 Consul 或其他服务,通常需要将容器加入到共享网络中。例如,在 `docker-compose.yml` 文件中,可以使用 `network_mode: "service:<service-name>"` 来让 Dapr 边车与应用容器共享网络命名空间 [^5]。 此外,还可以通过 Docker 的 `--network` 参数指定容器使用的网络,例如: ```bash docker run --network mynetwork -d --name myapp myapp ``` 这将确保容器可以访问 Consul 服务,并通过服务名称进行通信 [^5]。 ### 示例:Dapr 与 Consul 集成 假设 Consul 服务运行在 `http://consul:8500`,Dapr 配置文件已定义服务发现组件。在 Dapr 应用中,可以通过服务调用 API 与其他服务通信: ```bash curl http://localhost:3500/v1.0/invoke/orderservice/method/getOrder ``` Dapr使用 Consul 解析 `orderservice` 并将请求路由到正确的实例 。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值