日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

flask与js交互的示例代码_dapr实战(1):dapr locally环境的搭建和部署官方的Hello world示例...

發布時間:2024/10/6 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 flask与js交互的示例代码_dapr实战(1):dapr locally环境的搭建和部署官方的Hello world示例... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引言

dapr是多運行時微服務架構(及機甲架構)的參考實現,關于多運行時微服務架構的介紹,可以參考敖小劍老師的兩篇文章:

[譯]多運行時微服務架構

Mecha:將Mesh進行到底

為了探索dapr的使用,我們必須有一個環境,為了能快速體驗dapr的使用,在本文中,我們將闡述如何搭建一個dapr的locally環境,并在其上運行dapr的hello world示例代碼進行驗證

dapr的安裝

這次搭建采用的操作環境信息如下:

cat /proc/versionLinux version 4.4.0-177-generic (buildd@lgw01-amd64-035) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ) #207-Ubuntu SMP Mon Mar 16 01:16:10 UTC 2020# cat /etc/os-releaseNAME="Ubuntu"VERSION="16.04 LTS (Xenial Xerus)"ID=ubuntuID_LIKE=debianPRETTY_NAME="Ubuntu 16.04 LTS"VERSION_ID="16.04"HOME_URL="http://www.ubuntu.com/"SUPPORT_URL="http://help.ubuntu.com/"BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"UBUNTU_CODENAME=xenial

安裝docker

安裝方式,在這里采用了阿里提供一鍵安裝腳本

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

安裝完成后,我們查看一下docker的信息,如下:

root@ubuntu:/home/dapr# docker versionClient: Docker Engine - Community Version: 19.03.9 API version: 1.40 Go version: go1.13.10 Git commit: 9d988398e7 Built: Fri May 15 00:25:34 2020 OS/Arch: linux/amd64 Experimental: falseServer: Docker Engine - Community Engine: Version: 19.03.9 API version: 1.40 (minimum version 1.12) Go version: go1.13.10 Git commit: 9d988398e7 Built: Fri May 15 00:24:07 2020 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.13 GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc: Version: 1.0.0-rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: Version: 0.18.0 GitCommit: fec3683root@ubuntu:/home/dapr#

安裝dapr

dapr提供了其cli工具,通過 dapr 的cli就可以在本地主機上完成dapr組件的安裝,如下:

wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash

dapr的cli工具安裝完成后,就可以安裝dapr的相關組件了,如下:

dapr init? Making the jump to hyperspace...Downloading binaries and setting up components? Success! Dapr is up and running

在這個初始化的過程中,下載了相關dapr鏡像和daprd這個二進制包,如下:

find / -name *dapr*/usr/local/bin/daprd# docker ps82cfe68fe555 daprio/dapr "./placement" 2 days ago Up 2 hours 0.0.0.0:50005->50005/tcp dapr_placement

其中,daprd就是我們業務程序與之交互的sidecar(dapr runtime),而placement是負責 dapr actor機制處理的容器,其相互關系如下:

部署官方的Hello world示例

dapr的Hello world示例中,有兩個示例場景,如下:

  • 場景一:服務端(node開發)是一個REST服務,使用者采用rest工具,與其交互,將相關信息存入到redis中,并支持從redis中讀取已經存放的數據
  • 場景二:服務端(node開發)是一個REST服務,客戶端(python程序)也使用dapr來部署,二者交互,完成將相關信息存入到redis中,并從redis中讀取已經存放的數據
  • 在開始部署這兩個場景前,我們需要先將dapr的官方實例代碼下載到本地,進行解壓,如下:

    # wget https://github.com/dapr/samples/archive/v0.7.0.tar.gz # tar -xvf v0.7.0.tar.gz

    注:由于我當前使用的dapr的0.7.0 版本,所以下載的實例也是 0.7.0的,大家在試驗時,一定要注意版本匹配

    場景一

    這個場景的示意圖如下:

    步驟一:通過dapr來運行我們的node的程序

  • 進入示例的代碼目錄
  • # cd samples-0.7.0/# ls1.hello-world 5.bindings CONTRIBUTING.md2.hello-kubernetes 6.functions-and-keda LICENSE3.distributed-calculator 7.middleware README.md4.pub-sub 8.observability samples-diagrams.pptx# cd 1.hello-world/
  • 安裝node的相關依賴
  • root@ubuntu:/home/dapr/samples-0.7.0/1.hello-world# npm installnode_server@1.0.0 /home/dapr/samples-0.7.0/1.hello-world├─┬ body-parser@1.19.0│ ├── bytes@3.1.0│ ├── content-type@1.0.4│ ├─┬ debug@2.6.9......npm WARN node_server@1.0.0 No repository field.
  • 通過dapr來運行node進程
  • dapr run --app-id nodeapp --app-port 3000 --port 3500 node app.jsWARNING: Redis State Store file is being overwritten: /home/dapr/samples-0.7.0/1.hello-world/components/statestore.yamlWARNING: Redis PubSub file is being overwritten: /home/dapr/samples-0.7.0/1.hello-world/components/pubsub.yaml?? Starting Dapr with id nodeapp. HTTP Port: 3500. gRPC Port: 35158......?? Updating metadata for app command: node app.js? You're up and running! Both Dapr and your app logs will appear here.

    這里對啟動的參數做一個簡單的說明,--app-id為這個應用在dapr中指定一個id,--app-port是node對外的監聽端口,而port是dapr runtime為這個服務而提供的一個對外端口,我們通過Linux命令來驗證一下,如下:

    # lsof -i:3000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnode 2969 root 10u IPv6 12038090 0t0 TCP *:3000 (LISTEN)# lsof -i:3500COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEdaprd 2968 root 15u IPv4 12038265 0t0 TCP *:3500 (LISTEN)root@ubuntu:~#

    步驟二:通過Postman來訪問我們的node程序

  • 調用rest接口,插入數據
  • 如下圖:

    在這里我們可以看到,訪問的端口是 3500,就是 由 daprd監聽的端口 2. 通過redis工具,查看redis中的數據

    redis中的數據
  • 調用rest接口,獲取已經插入的數據 如下圖:
  • 小結

    通過場景一的演示,我們看到postman與drapd交互,然后drapd再與node程序交互,最終將數據插入到了redis中

    場景二

    場景二將在場景一的基礎上,將postman也換成一個python程序,而python也是用dapr進行部署,如圖所示:

    步驟一:通過pip來安裝python程序的依賴包

    # pip3 install requestsWARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.Collecting requests Downloading https://files.pythonhosted.org/packages/1a/70/1935c770cb3be6e3a8b78ced23d7e0f3b187f5cbfab4749523ed65d7c9b1/requests-2.23.0-py2.py3-none-any.whl (58kB) 100% |████████████████████████████████| 61kB 429kB/s.........

    步驟二:通過dapr將python程序運行起來

    新啟動一個終端,在這個終端用dapr啟動這個python程序(這個程序也是在官方的1.hello-world中)

    # dapr run --app-id pythonapp python3 app.py?? Starting Dapr with id pythonapp. HTTP Port: 40182. gRPC Port: 41868.....

    使用dapr運行這個python程序后,在node的終端中就可以看到如下打印輸出:

    == APP == Got a new order! Order ID: 1== APP == Successfully persisted state.== APP == Got a new order! Order ID: 2== APP == Successfully persisted state.== APP == Got a new order! Order ID: 3

    總結

    在這篇文章我們搭建了dapr的單機環境,并通過官方示例代碼,體驗了dapr的使用方式,但是我們對Dapr還是存在很多不明白的地方,在下一篇中,我將用go和java來重寫一下這個hello world,來進一步深入Dapr

    總結

    以上是生活随笔為你收集整理的flask与js交互的示例代码_dapr实战(1):dapr locally环境的搭建和部署官方的Hello world示例...的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。