dapr 應用開發 - 環境配置

1 $ Dapr 簡單介紹

Dapr CLI 允許您在上本地開發環境中運行 Dapr 框架,並支持 MacOS、Linux 和 Windows。在幾秒鐘內安裝 Dapr,並快速開始編碼。

The Dapr CLI allows you to get started with Dapr running locally on your dev box and support MacOS, Linux and Windows. Install Dapr in seconds and start coding quickly.

Dapr 框架的語言支持:任何語言,任何地方(雲 / Cloud 《=》端 / Edge)

使用 Dapr 的公司:

2 $ 環境搭建

2.1 安裝 Dockers /Prerequisites 先決條件 (可選項,不是必須)

默認情況下,在初始化期間 Dapr CLI 將安裝 Dapr 二進制文件以及設置一個開發人員環境,以幫助您輕鬆地啓動 Dapr。這個環境使用 Docker 容器,因此需要安裝 Docker。如果您希望在沒有這個環境和對 Docker 沒有依賴性的情況下運行 Dapr,那麼在安裝 CLI 之後,請確保按照說明使用 slim init 初始化 Dapr。

Note, if you are a new user, it is strongly recommended to intall Docker and use the regular init command.

注意,如果您是一個新用戶,強烈建議使用所有的 Docker 並使用常規 init 命令。

安裝 docker 注意事項:

Note: On Windows, Docker must be running in Linux Containers mode

注意: 在 Windows 上,Docker 必須在 Linux 容器模式下運行(建議使用 WSL2)

2.2 安裝 Dapr CLI(必須)

OS 平臺 Dapr CLI 安裝說明

Windows

#將最新的 windows Dapr CLI 安裝到 c: Dapr (默認安裝 C 盤根路徑),然後將這個目錄添加到 User PATH 環境變量。使用 -DaprRoot [path] 更改默認的安裝目錄

powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"

Linux

#將最新的 linux Dapr CLI 安裝到 /usr/local/bin

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

MacOS

#將最新的 darwin Dapr CLI 安裝到 /usr/local/bin

curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash

From the Binary Releases (二進制方式安裝)

Dapr CLI 的每個版本都包含各種操作系統和體系結構,這些二進制版本可以手動下載和安裝。

下載 Dapr CLI;

解壓包 (e.g. dapr_linux_amd64.tar.gz, dapr_windows_amd64.zip)

把它移動到你想要的位置

適用於 Linux/MacOS -/usr/local/bin

對於 Windows,創建一個目錄並將其添加到系統 [PATH] 中並通過編輯系統環境變量將此目錄添加到 [PATH] 中

2.3 安裝 Runtime 

Dapr 支持本地主機自託管  (self-hosted) 和 on Kubenetes 模式託管;

2.3.1 本地主機自託管模式 (self-hosted)

在本機安裝 Dapr 運行時分爲兩種模式,一種是完整模式,一種是簡易模式:

【完整模式】:需要 Docker 環境,運行 dapr init ,會自動下載 Dapr 的運行時 daprd ,並在 Docker 中啓動 3 個容器:

【dapr_placement】:Image 爲 “daprio/dapr”,用於 Dapr 的 Actor 應用的註冊

【dapr_zipkin】:Image 爲 “openzipkin/zipkin”,用於分佈式跟蹤的處理

【dapr_redis】:Image 爲 “redis”,用於狀態存儲和發佈訂閱處理

對於 Linux 用戶,如果使用 sudo 運行 docker cmds,則需要使用 sudo dapr init 

注意: 要查看 Dapr 是否已經成功安裝,請從命令提示符運行 docker ps 命令,並檢查 daprio/Dapr:latest、 dapr_redis 和 dapr_zipkin 容器映像是否都在運行。

這一步創建了以下缺省值:

組件文件夾,稍後在 除非對於 Linux/MacOS,默認組件文件夾路徑爲 對於 Windows 系統來說就是這樣 %USERPROFILE%.dapr\components.

組件文件夾中的組件文件及 statestore.yaml.

默認配置文件適用於 Linux/MacOS 或 Windows 啓用追蹤 調用。可以用標誌 dapr run.

【簡易模式】:不需要 Docker 環境,運行 dapr init --slim,同樣會自動下載 Dapr 的運行時 daprd,並下載 placement 服務的二進制文件。

注意: 當使用 -- slim 標誌初始化 Dapr 時,只安裝 Dapr 運行時二進制文件和 placement service 二進制文件。創建一個沒有默認配置文件的空默認組件文件夾。在 dapr run 期間,用戶應該使用 -- components-path 指向包含自定義配置文件的組件目錄,或者將這些文件放在默認目錄中。對於 Linux/MacOS,默認組件目錄路徑是【$HOME/.dapr/components】,對於 Windows 它是【%USERPROFILE%.dapr\components】。

兩種模式下,有些配置也有所不同:

完整模式:會在 Linux/MacOS 的 【$HOME/.dapr/components】目錄或 Windows 的【%USERPROFILE%.dapr\components】目錄下面創建 3 個默認組件配置文件:pubsub.yaml 使用 redis 容器作爲發佈訂閱組件、statestore.yaml 使用 redis 容器作爲狀態存儲組件和 zipkin.yaml 把分佈式跟蹤的信息導出到 zipkin 容器;並添加 Linux/MacOS 的【$HOME/.dapr/config.yaml】或 Windows 的【%USERPROFILE%.dapr\config.yaml】的默認配置文件,重點配置了分佈式跟蹤的採樣率爲 1。

簡易模式:雖然會創建默認 components 目錄,但是不會創建默認組件配置文件。後續運行的時候,你需要自行創建默認組件配置文件或者指定服務的自定義組件配置文件。

=》安裝特定的運行時版本 | Install a specific runtime version 

您可以使用 Dapr init --runtime-version 安裝或升級到 Dapr 運行時的特定版本。您可以在 Dapr Release 中找到版本列表。

Install v1.0.0 runtime

$ dapr init --runtime-version 1.0.0

Check the versions of CLI and runtime

$ dapr --version

CLI version: v1.0.0

Runtime version: v1.0.0

 =》安裝到特定的 Docker 網絡 | Install to a specific Docker network

您可以將 Dapr 運行時安裝到特定的 Docker 網絡中,以便將其與本地機器隔離開來 (例如,從 Docker 容器中使用 Dapr)。

Create Docker network

$ docker network create dapr-network

Install Dapr to the network

$ dapr init --network dapr-network

 注意: 當安裝到特定的 Docker 網絡時,您需要將 --placement-host-address 參數添加到 dapr,以便在該網絡內的任何容器中運行命令。

2.3.2 託管 on Kubenetes 模式

The init command will install Dapr to a Kubernetes cluster. For more advanced use cases, use our Helm Chart.

Init 命令將把 Dapr 安裝到 Kubernetes(簡寫 -k)集羣。

Note: The default namespace is dapr-system. The installation will appear under the name dapr for Helm

注意: 默認名稱空間是 dapr-system。安裝將以 Helm 的名稱 dapr 出現

dapr init -k

提供 Helm values | Supplying Helm values 

可以使用 --set 標誌設置所有可用的 Helm values | All available Helm Chart values can be set by using the --set flag:

$ dapr init -k --set global.tag=1.0.0 --set dapr_operator.logLevel=error  

安裝到自定義命名空間 | Installing to a custom namespace

$ dapr init -k -n my-namespace

安裝時使用高可用的控制平面配置 | Installing with a highly avaialable control plane config 

$ dapr init -k --enable-ha=true

安裝時禁用 mTLS | Installing with mTLS disabled 

$ dapr init -k --enable-mtls=false 

Dapr 在 k8s 上面的升級 | Upgrade Dapr on Kubernetes 

要執行 Dapr 控制平面的零宕機時間升級 | To perform a zero downtime upgrade of the Dapr control plane:

dapr 升級指定 runtime 版本

$ dapr upgrade -k --runtime-version=1.0.0

Note: do not use the dapr upgrade command if you're upgrading from 0.x versions of Dapr

注意: 如果您從 Dapr 的 0.x 版本升級,請不要使用 Dapr 升級命令

可以使用 --set 標誌設置所有可用的 Helm values | All available Helm Chart values can be set by using the --set flag:

$ dapr upgrade -k --runtime-version=1.0.0 --set global.tag=my-tag --set dapr_operator.logLevel=error 

3 $ 卸載 Dapr 環境

3.1 本地主機自託管 (self-hosted) 模式卸載

在獨立模式下卸載 Dapr | Uninstall Dapr in a standalone mode

Uninstalling will remove daprd binary and the placement container (if installed with Docker or the placement binary if not).

卸載將刪除 daprd 二進制文件和佈局容器 (如果安裝了 Docker 或佈局二進制文件,如果沒有)。

$ dapr uninstall

For Linux users, if you run your docker cmds with sudo, you need to use "sudo dapr uninstall" to remove the containers.

對於 Linux 用戶,如果使用 sudo 運行 docker cmds,則需要使用 “sudo dapr uninstall” 來刪除容器。

The command above won't remove the redis or zipkin containers by default in case you were using it for other purposes. It will also not remove the default dapr folder that was created on dapr init. To remove all the containers (placement, redis, zipkin) and also the default dapr folder created on init run:

上面的命令默認情況下不會刪除 redis 或 zipkin 容器,以防您將其用於其他目的。它也不會移除在 dapr init 上創建的默認 dapr 文件夾。要刪除所有的容器 (placement、 redis、 zipkin) 以及在 init 上創建的默認 dapr 文件夾,請運行:

$ dapr uninstall --all

The above command can also be run when Dapr has been installed in a non-docker environment, it will only remove the installed binaries and the default dapr folder in that case.

當 Dapr 安裝在非 docker 環境中時,也可以運行上面的命令,在這種情況下,它只會刪除已安裝的二進制文件和默認的 Dapr 文件夾。

NB: The dapr uninstall command will always try to remove the placement binary/service and will throw an error is not able to.

注意: dapr uninstall 命令將總是嘗試刪除 placement binary/service,並將拋出一個無法執行的錯誤。

You should always run a dapr uninstall before running another dapr init.

在運行另一個 dapr init 之前,應該始終運行 dapr 卸載。

從特定的 Docker 網絡卸載 Dapr | Uninstall Dapr from a specific Docker network 

If previously installed to a specific Docker network, Dapr can be uninstalled with the --network argument:

如果以前安裝到特定的 Docker 網絡,Dapr 可以通過 - network 參數卸載:

$ dapr uninstall --network dapr-network

3.2 託管 on Kubenetes 模式卸載

卸載 Kubernetes 上的 Dapr | Uninstall Dapr on Kubernetes

To remove Dapr from your Kubernetes cluster, use the uninstall command with --kubernetes flag or the -k shorthand.

要從您的 Kubernetes 集羣中刪除 Dapr,可以使用 uninstall 命令 -- Kubernetes 標誌或 -k 簡寫。

dapr uninstall --kubernetes // 卸載 Kubernetes 中的測試環境

helm uninstall dapr -n dapr-system // 卸載 Kubernetes 中的生產環境

至此,Dapr 的開發、測試和生產環境就配置完成了,接下來就是進入到開發階段了。

參考資料

【dapr 官網】https://dapr.io/

【dapr cli】https://github.com/dapr/cli

【Dapr 微服務應用開發系列 1:環境配置】https://www.cnblogs.com/redmoon/p/13576552.html

dapr-cli 國內加速下載:https://www.newbe.pro/Mirrors/Mirrors-Dapr-CLI/#more

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/W7UZ1vKaZzKCzmYca42doA