日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Dapr入門(mén)教程之發(fā)布訂閱

Dapr 入門(mén)教程之發(fā)布訂閱

作者:k8s技術(shù)圈 2022-09-19 16:08:31

云計(jì)算

云原生 Dapr 使用可插拔的消息總線來(lái)支持發(fā)布-訂閱,并將消息傳遞給 CloudEvents(一個(gè) CNCF 項(xiàng)目) 作為通用的事件信封格式,以提高連接服務(wù)的互操作性。

創(chuàng)新互聯(lián)專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、渾南網(wǎng)絡(luò)推廣、微信小程序定制開(kāi)發(fā)、渾南網(wǎng)絡(luò)營(yíng)銷(xiāo)、渾南企業(yè)策劃、渾南品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供渾南建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com

前面我們了解了如果在 Dapr 下面進(jìn)行服務(wù)調(diào)用,以及最簡(jiǎn)單的狀態(tài)管理,本節(jié)我們來(lái)了解如何啟用 Dapr 的發(fā)布/訂閱模式,發(fā)布者將生成特定主題的消息,而訂閱者將監(jiān)聽(tīng)特定主題的信息。

  • 使用發(fā)布服務(wù),開(kāi)發(fā)人員可以重復(fù)發(fā)布消息到一個(gè)主題上。
  • Pub/sub 組件對(duì)這些消息進(jìn)行排隊(duì)處理。
  • 該主題訂閱者將從隊(duì)列中獲取到消息并處理他們。

接下來(lái)我們使用的這個(gè)示例包含一個(gè)發(fā)布者:

  • React 前端消息生成器

包含另外 3 個(gè)消息訂閱者:

  • Node.js 訂閱者
  • Python 訂閱者
  • C# 訂閱者

Dapr 使用可插拔的消息總線來(lái)支持發(fā)布-訂閱,并將消息傳遞給 CloudEvents(一個(gè) CNCF 項(xiàng)目) 作為通用的事件信封格式,以提高連接服務(wù)的互操作性。

我們這里將使用 Redis Streams(在 Redis version = > 5 中啟用),當(dāng)然也可以使用 RabbitMQ、Kafka 等中間件。下圖是用來(lái)說(shuō)明組件之間是如何在本地模式下互相連接的。

dapr pub/sub

本地初始化

Dapr 允許你將相同的微服務(wù)從本地機(jī)器部署到云環(huán)境中去,這里為了和大家說(shuō)明這種便利性,我們先在本地部署這個(gè)實(shí)例項(xiàng)目,然后再將其部署到 Kubernetes 環(huán)境中去。

要在本地使用 Dapr 服務(wù),需要先在本地初始化 Dapr:

$ dapr init

由于某些網(wǎng)絡(luò)原因使用上面的命令可能并不能初始化成功,我們可以使用離線的方式進(jìn)行安裝,前往 https://github.com/dapr/installer-bundle/releases 下載對(duì)應(yīng)系統(tǒng)的 Bundle 包,然后解壓,比如我這里是 Mac M1,使用下面的命令下載:

$ wget https://github.91chi.fun/https://github.com/dapr/installer-bundle/releases/download/v1.8.4/daprbundle_darwin_arm64.tar.gz
$ tar -xvf daprbundle_darwin_arm64.tar.gz
x daprbundle/
x daprbundle/README.md
x daprbundle/dapr
x daprbundle/details.json
x daprbundle/dist/
x daprbundle/dist/daprd_darwin_arm64.tar.gz
x daprbundle/dist/dashboard_darwin_arm64.tar.gz
x daprbundle/dist/placement_darwin_arm64.tar.gz
x daprbundle/docker/
x daprbundle/docker/daprio-dapr-1.8.4.tar.gz

然后我們可以重新使用下面的命令進(jìn)行初始化:

$ dapr init --from-dir daprbundle/
? Making the jump to hyperspace...
Local bundle installation using --from-dir flag is currently a preview feature and is subject to change. It is only available from CLI version 1.7 onwards.
?? Installing runtime version 1.8.4
↙ Extracting binaries and setting up components...
Dapr runtime installed to /Users/cnych/.dapr/bin, you may run the following to add it to your path if you want to run daprd directly:
export PATH=$PATH:/Users/cnych/.dapr/bin
8d7366c22fd8: Loading layer [==================================================>] 3.697MB/3.697MB
61f7f94319f6: Loading layer [==================================================>] 238.6MB/238.6MB
← Extracting binaries and setting up components... Loaded image: daprio/dapr:1.8.4
Extracting binaries and setting up components...
Extracted binaries and completed components set up.
?? daprd binary has been installed to /Users/cnych/.dapr/bin.
?? dapr_placement container is running.
?? Use `docker ps` to check running containers.
$ dapr version
CLI version: 1.8.0
Runtime version: 1.8.4

默認(rèn)會(huì)啟用 ??zipkin??? 這個(gè) tracing 服務(wù),使用上面的命令初始化如果沒(méi)有對(duì)應(yīng)的容器,則可以使用 ??docker run --name dapr_zipkin -d -p 9411:9411 dockerproxy.com/openzipkin/zipkin??? 啟動(dòng)該服務(wù)。同樣也需要運(yùn)行一個(gè) Redis 服務(wù):??docker run --name dapr_redis -d -p 6379:6379 dockerproxy.com/redislabs/rejson??。

dapr containers

消息訂閱服務(wù)

這里我們還是使用前面使用的 quickstarts 這個(gè)項(xiàng)目,克隆項(xiàng)目到本地:

git clone [-b ] https://github.com/dapr/quickstarts.git

進(jìn)入 tutorials/pub_sub 目錄下面:

  pub-sub git:(622b7d9) ls
README.md deploy makefile message_b.json node-subscriber react-form
csharp-subscriber img message_a.json message_c.json python-subscriber

運(yùn)行 Node 消息訂閱服務(wù)

首先我們使用 Dapr 運(yùn)行 node 消息訂閱服務(wù),導(dǎo)航到 node-subscriber 目錄,安裝依賴:

$ cd node-subscriber
$ npm install # 或者 yarn

執(zhí)行如下所示命令運(yùn)行 node 消息訂閱服務(wù):

$ dapr run --app-id node-subscriber --app-port 3000 node app.js
?? Starting Dapr with id node-subscriber. HTTP Port: 50728. gRPC Port: 50729
INFO[0000] starting Dapr Runtime -- version 1.8.4 -- commit 18575823c74318c811d6cd6f57ffac76d5debe93 app_id=node-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
# ......
INFO[0000] app is subscribed to the following topics: [A B] through pubsub=pubsub app_id=node-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] dapr initialized. Status: Running. Init Elapsed 312.69599999999997ms app_id=node-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] placement tables updated, version: 0 app_id=node-subscriber instance=MBP2022.local scope=dapr.runtime.actor.internal.placement type=log ver=1.8.4
?? Updating metadata for app command: node app.js
You're up and running! Both Dapr and your app logs will appear here.

上面命令中的 app-id 是微服務(wù)的唯一標(biāo)識(shí)符,--app-port 是 Node 應(yīng)用程序運(yùn)行的端口,最后,運(yùn)行應(yīng)用程序的命令是 node app.js。

運(yùn)行 Python 消息訂閱服務(wù)

接下來(lái)使用 Dapr 運(yùn)行 Python 消息訂閱服務(wù),導(dǎo)航到 python-subscriber 目錄:

$ cd python-subscriber

安裝應(yīng)用依賴:

$ pip3 install -r requirements.txt

同樣再次使用 dapr run 來(lái)運(yùn)行該訂閱服務(wù):

$ dapr run --app-id python-subscriber --app-port 5001 python3 app.py
?? Starting Dapr with id python-subscriber. HTTP Port: 55508. gRPC Port: 55509
INFO[0000] starting Dapr Runtime -- version 1.8.4 -- commit 18575823c74318c811d6cd6f57ffac76d5debe93 app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] log level set to: info app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] enabled gRPC metrics middleware app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime.grpc.internal type=log ver=1.8.4
INFO[0000] internal gRPC server is running on port 55514 app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] application protocol: http. waiting on port 5001. This will block until the app is listening on that port. app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
INFO[0000] application discovered on port 5001 app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
WARN[0000] [DEPRECATION NOTICE] Adding a default content type to incoming service invocation requests is deprecated and will be removed in the future. See https://docs.dapr.io/operations/support/support-preview-features/ for more details. You can opt into the new behavior today by setting the configuration option `ServiceInvocation.NoDefaultContentType` to true. app_id=python-subscriber instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
== APP == * Serving Flask app "app" (lazy loading)
== APP == * Environment: production
== APP == WARNING: This is a development server. Do not use it in a production deployment.
== APP == Use a production WSGI server instead.
== APP == * Debug mode: off
== APP == * Running on http://127.0.0.1:5001/ (Press CTRL+C to quit)
?? Updating metadata for app command: python3 app.py
You're up and running! Both Dapr and your app logs will appear here.

由于我們這里沒(méi)有 C# 環(huán)境,所以只運(yùn)行 Node 和 Python 這兩個(gè)消息訂閱服務(wù)了。

消息發(fā)布服務(wù)

接下來(lái)我們來(lái)運(yùn)行 React 這個(gè)前端消息發(fā)布服務(wù),同樣先導(dǎo)航到 react-form 項(xiàng)目目錄下面:

$ cd react-form

然后執(zhí)行下面的命令安裝依賴并構(gòu)建服務(wù):

$ npm run buildclient
$ npm install

構(gòu)建完成后可以使用下面的 dapr 命令來(lái)啟動(dòng)該前端服務(wù):

$ dapr run --app-id react-form --app-port 8080 npm run start
?? Starting Dapr with id react-form. HTTP Port: 57303. gRPC Port: 57304
INFO[0000] starting Dapr Runtime -- version 1.8.4 -- commit 18575823c74318c811d6cd6f57ffac76d5debe93 app_id=react-form instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
# ......
== APP ==
== APP == > react-form@1.0.0 start
== APP == > node server.js
== APP ==
== APP == Listening on port 8080!
INFO[0000] application discovered on port 8080 app_id=react-form instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
# ......
INFO[0000] dapr initialized. Status: Running. Init Elapsed 760.39ms app_id=react-form instance=MBP2022.local scope=dapr.runtime type=log ver=1.8.4
?? Updating metadata for app command: npm run start
You're up and running! Both Dapr and your app logs will appear here.

INFO[0001] placement tables updated, version: 0 app_id=react-form instance=MBP2022.local scope=dapr.runtime.actor.internal.placement type=log ver=1.8.4

當(dāng)看到 == APP == Listening on port 8080! 這樣的日志時(shí),表示應(yīng)用啟動(dòng)成功了。然后我們就可以在瀏覽器中訪問(wèn) http://localhost:8080 訪問(wèn)前端應(yīng)用了。

前端頁(yè)面

比如現(xiàn)在我們選擇消息類(lèi)型 A,然后隨便輸入一些消息內(nèi)容,點(diǎn)擊 Submit 發(fā)送,然后觀察上面的 Node 和 Python 這兩個(gè)消息訂閱者服務(wù)的日志。

選擇一個(gè)主題,輸入一些文字,然后發(fā)送一條信息!觀察通過(guò)你們各自的 Dapr 的日志。

Dapr 消息訂閱發(fā)布服務(wù)

注意,Node 訂閱者接收類(lèi)型為 A 和 B 的消息,而 Python 訂閱者接收類(lèi)型為 A和 C 的消息,所以注意每個(gè)控制臺(tái)窗口的日志顯示。

此外 Dapr CLI 提供了一個(gè)機(jī)制來(lái)發(fā)布消息用于測(cè)試,比如我們可以使用如下命令來(lái)發(fā)布一條消息:

$ dapr publish --publish-app-id react-form --pubsub pubsub --topic A --data-file message_a.json

dapr cli publish

到這里我們就完成了使用 Dapr 來(lái)進(jìn)行消息訂閱發(fā)布的功能演示。

在 Kubernetes 中運(yùn)行

上面我們是將演示服務(wù)在本地部署的,我們知道使用 Dapr 開(kāi)發(fā)的服務(wù)是和平臺(tái)沒(méi)關(guān)系的,可以很輕松遷移到云環(huán)境,比如現(xiàn)在我們?cè)賹⑸厦娴氖纠龖?yīng)用部署到 Kubernetes 集群中。

要在 Kubernetes 中運(yùn)行相同的代碼,首先需要設(shè)置 Redis 存儲(chǔ),然后部署微服務(wù),將使用相同的微服務(wù),但最終架構(gòu)有所不同:

運(yùn)行在K8s

前面我們已經(jīng)使用 Helm 安裝了 bitnami 下面的 redis 應(yīng)用:

$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo update
$ helm install redis bitnami/redis

有了 Redis 服務(wù)過(guò)后,接著我們需要?jiǎng)?chuàng)建一個(gè)發(fā)布訂閱的 Component 組件,前文是創(chuàng)建的一個(gè)使用 Redis 的狀態(tài)管理組件,對(duì)應(yīng)的組件資源清單如下所示:

# deploy/redis.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.redis
version: v1
metadata:
# These settings will work out of the box if you use `helm install
# bitnami/redis`. If you have your own setup, replace
# `redis-master:6379` with your own Redis master address, and the
# Redis password with your own Secret's name. For more information,
# see https://docs.dapr.io/operations/components/component-secrets .
- name: redisHost
value: redis-master:6379
- name: redisPassword
secretKeyRef:
name: redis
key: redis-password
auth:
secretStore: kubernetes

直接應(yīng)用上面的資源清單即可:

$ kubectl apply -f deploy/redis.yaml
component.dapr.io/pubsub created
$ kubectl get components
NAME AGE
pubsub 26s
statestore 45h

現(xiàn)在我們就有了一個(gè)使用 Redis 為中間件的發(fā)布訂閱組件了,注意上面對(duì)象的類(lèi)型為 ??pubsub.redis??。

redis pubsub

接著我們就可以部署 Python、Node 和 React-form 這 3 個(gè)微服了:

$ kubectl apply -f deploy/node-subscriber.yaml
$ kubectl apply -f deploy/python-subscriber.yaml
$ kubectl apply -f deploy/react-form.yaml

部署后查看 Pod 的狀態(tài):

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
node-subscriber-5b5777c785-z8jzn 2/2 Running 0 30m
python-subscriber-76d9fc6c87-http://www.dlmjj.cn/article/cogcgig.html