From 919e592d2882d8f544f580b4aa8f18ea88ddcfef Mon Sep 17 00:00:00 2001 From: hello Date: Fri, 16 Aug 2024 14:18:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=86=E5=B8=83=E5=BC=8F?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/helloshop/distributed-event-dapr.md | 129 ++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 notes/helloshop/distributed-event-dapr.md diff --git a/notes/helloshop/distributed-event-dapr.md b/notes/helloshop/distributed-event-dapr.md new file mode 100644 index 0000000..4718a80 --- /dev/null +++ b/notes/helloshop/distributed-event-dapr.md @@ -0,0 +1,129 @@ +# 分布式事件最佳实践 + +## 安装 Dapr CLI 脚手架工具 + +https://docs.dapr.io/zh-hans/getting-started/install-dapr-cli + +## 基于容器初始化 Dapr 运行时 + +https://docs.dapr.io/zh-hans/getting-started/install-dapr-selfhost + +```shell +dapr init +``` + +## 不用容器离线初始化 Dapr 运行时 + +```shell +dapr init -s +``` + + +## 使用离线包初始化 Dapr 运行时 + +https://docs.dapr.io/zh-hans/operations/hosting/self-hosted/self-hosted-airgap/ + +```shell +dapr init --from-dir C:\daprbundle +``` + +## 打开默认初始化目录 + +```shell +explorer "$env:USERPROFILE\.dapr" +explorer "%USERPROFILE%\.dapr" +``` + +## 使用 Dashboard 查看 Dapr 运行状态 + +```shell +dapr dashboard +``` + +## 发布订阅配置分布式事件 + +```shell +/components/redis-pubsub.yaml +``` + +### 基于 Redis 发布订阅配置 + +```yaml +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: pubsub +spec: + type: pubsub.redis + version: v1 + metadata: + - name: redisHost + value: localhost:6379 + - name: redisPassword + value: "" +``` + +### 基于 RabbitMQ 发布订阅配置 + +```shell +/components/rabbitmq-pubsub.yaml +``` + +```yaml +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: pubsub +spec: + type: pubsub.rabbitmq + version: v1 + metadata: + - name: host + value: "amqp://guest:guestpwd@localhost:5672" + - name: durable + value: "false" + - name: deletedWhenUnused + value: "false" + - name: autoAck + value: "false" + - name: reconnectWait + value: "0" + - name: concurrency + value: parallel +``` + +### 使用 Dapr 命令启动 Sidecar 和应用程序 + +```shell +dapr run --app-id myapp --dapr-http-port 3500 --dapr-grpc-port 50001 --app-port 5000 --log-level debug --config ./configuration/config.yaml --components-path ./components dotnet run +``` + +## 使用 Aspire 命令启动 Sidecar 和应用程序 + +```shell +dotnet add package Aspire.Hosting.Dapr +``` + +```csharp +var pubsub = builder.AddDaprPubSub("pubsub", new DaprComponentOptions { LocalPath = "./DaprComponents/" }); + +var productService = builder.AddProject("productservice") + .WithReference(identityService) + .WithDaprSidecar() + .WithReference(pubsub); + +``` + + +## 使用 RabbitMQ 发布订阅配置 + +```shell +var username = builder.AddParameter("username", secret: true); +var password = builder.AddParameter("password", secret: true); + +var messaging = builder.AddRabbitMQ("messaging", username, password); + +// Service consumption +builder.AddProject() + .WithReference(messaging); +``` \ No newline at end of file