The documentation you are viewing is for Dapr v1.10 which is an older version of Dapr. For up-to-date documentation, see the latest version.

操作:配置具有多个命名空间的 Pub/Sub 组件

多个命名空间下使用Dapr Pub/Sub

在某些场景下,应用程序分布在不同的命名空间,并通过PubSub共享一个队列或主题。 在这种情况下,必须在每个命名空间上都提供PubSub组件。

这个例子使用了PubSub示例。 Redis安装和其订阅者在namespace-a中,而发布者UI在namespace-b中。 如果Redis安装在另一个命名空间上,或者使用Azure ServiceBus、AWS SNS/SQS或GCP PubSub等云服务,该解决方案也同样奏效。

这是一个使用命名空间的示例图片。



下表描述了部署的资源和所在命名空间的对应关系:

资源 namespace-a namespace-b
Redis master X
Redis slave X
Dapr’s PubSub component X X
Node subscriber X
Python subscriber X
React UI publisher X

前提

设置namespace-a

创建命名空间并用kubectl切入。

kubectl create namespace namespace-a
kubectl config set-context --current --namespace=namespace-a

Install Redis (master and slave) on namespace-a, following these instructions.

现在,配置deploy/redis.yaml,注意包含namespace-a的主机名。

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: pubsub
spec:
  type: pubsub.redis
  version: v1
  metadata:
  - name: "redisHost"
    value: "redis-master.namespace-a.svc:6379"
  - name: "redisPassword"
    value: "YOUR_PASSWORD"

将资源部署到namespace-a

kubectl apply -f deploy/redis.yaml
kubectl apply -f deploy/node-subscriber.yaml
kubectl apply -f deploy/python-subscriber.yaml

设置namespace-b

创建命名空间并用kubectl切入。

kubectl create namespace namespace-b
kubectl config set-context --current --namespace=namespace-b

将资源部署到namespace-b,包括Redis组件:

kubectl apply -f deploy/redis.yaml
kubectl apply -f deploy/react-form.yaml

现在,找到react-form的IP地址,在浏览器上打开它,并将消息发布到每个主题(A、B、C)。

kubectl get service -A

确认订阅者收到信息

切换回 namespace-a:

kubectl config set-context --current --namespace=namespace-a

查找POD名称:

kubectl get pod # 复制POD名称并在接下来的命令中使用。

显示日志:

kubectl logs node-subscriber-XYZ node-subscriber
kubectl logs python-subscriber-XYZ python-subscriber

浏览器上发布的消息应该会显示在相应用户的日志中。 Node.js订阅者接收的消息类型为 “A “和 “B”,而Python订阅者接收的消息类型为 “A “和 “C”。

清理

kubectl delete -f deploy/redis.yaml  --namespace namespace-a
kubectl delete -f deploy/node-subscriber.yaml  --namespace namespace-a
kubectl delete -f deploy/python-subscriber.yaml  --namespace namespace-a
kubectl delete -f deploy/react-form.yaml  --namespace namespace-b
kubectl delete -f deploy/redis.yaml  --namespace namespace-b
kubectl config set-context --current --namespace=default
kubectl delete namespace namespace-a
kubectl delete namespace namespace-b

相关链接