手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序
注意:
文章中提到的命令行工具即是Windows Terminal/PowerShell/cmd其中的一個,推薦使用Windows Terminal
運行命令行工具的時候建議以管理員身份,避免踩坑
為了保證操作順暢,建議使用PowerShell先執行一下set-ExecutionPolicy RemoteSigned
安裝Docker
因為Dapr CLI默認會在Docker內啟動 redis、zipkin、placement。
當然這些也不是必須要安裝的,只是推薦安裝可以體驗Dapr的完整能力,方便后續章節的學習。
下載并安裝Docker Desktop
https://www.docker.com/products/docker-desktop
安裝WSL2,使用命令行工具執行命令
wsl --instal如果不能使用wsl直接安裝的話可以手動安裝,運行PowerShell并執行下面兩句命令
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart如果你再次遇到錯誤提示:0x800f080c 功能名稱 VirtualMachinePlatform 未知。
請保證自己的操作系統版本在Windows 10 build 18917以上
重啟電腦后下載WSL2內核
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
運行命令行工具,設置默認使用WSL2
wsl --set-default-version 2下載Ubuntu 20.04
https://www.microsoft.com/store/apps/9n6svws3rx71
設置Docker使用WSL2
你“可能”需要一個小工具
不能說的秘密,看文件名猜功能
https://github.com.cnpmjs.org/dotnetcore/FastGithub/releases/download/2.0.4/fastgithub_win-x64.zip
安裝Dapr CLI
官方解釋:Dapr CLI 是您用于各種 Dapr 相關任務的主要工具。您可以使用它來運行一個帶有Dapr sidecar的應用程序, 以及查看sidecar日志、列出運行中的服務、運行 Dapr 儀表板。
CLI是必須要安裝嗎?其實也不是,但新手不建議去體驗這些騷操作。后續文章會講解脫離dapr cli工作。
運行Windows Terminal或PowerShell,執行命令,這里要耐心等待一下
iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex如果是cmd執行下面命令:
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"驗證安裝,重新打開命令行工具執行命令dapr,看到下面的提示即安裝正確
______/ /___ _____ _____/ __ / __ '/ __ \/ ___// /_/ / /_/ / /_/ / /\__,_/\__,_/ .___/_//_/=============================== Distributed Application RuntimeUsage:dapr [command]Available Commands:completion ? ? Generates shell completion scriptscomponents ? ? List all Dapr components. Supported platforms: Kubernetesconfigurations List all Dapr configurations. Supported platforms: Kubernetesdashboard ? ? Start Dapr dashboard. Supported platforms: Kubernetes and self-hostedhelp ? ? ? ? ? Help about any commandinit ? ? ? ? ? Install Dapr on supported hosting platforms. Supported platforms: Kubernetes and self-hostedinvoke ? ? ? ? Invoke a method on a given Dapr application. Supported platforms: Self-hostedlist ? ? ? ? ? List all Dapr instances. Supported platforms: Kubernetes and self-hostedlogs ? ? ? ? ? Get Dapr sidecar logs for an application. Supported platforms: Kubernetesmtls ? ? ? ? ? Check if mTLS is enabled. Supported platforms: Kubernetespublish ? ? ? Publish a pub-sub event. Supported platforms: Self-hostedrun ? ? ? ? ? Run Dapr and (optionally) your application side by side. Supported platforms: Self-hostedstatus ? ? ? ? Show the health status of Dapr services. Supported platforms: Kubernetesstop ? ? ? ? ? Stop Dapr instances and their associated apps. . Supported platforms: Self-hosteduninstall ? ? Uninstall Dapr runtime. Supported platforms: Kubernetes and self-hostedupgrade ? ? ? Upgrades a Dapr control plane installation in a cluster. Supported platforms: KubernetesFlags:-h, --help ? ? help for dapr-v, --version ? version for daprUse "dapr [command] --help" for more information about a command.初始化Dapr
使用命令行工具執行命令
dapr init如此之外還有一個精簡版的安裝方式
dapr init --slim看看官方解釋slim
在此模式下安裝了兩個不同的二進制文件 daprd 和 placement。placement是給actor用的,之前有提到過。
在此模式下,不會為狀態管理或發布/訂閱安裝任何默認組件(如 Redis),那就只能用服務調用。其他的需要用戶自己設置環境和自定義組件。
說白了,你只用服務調用,那slim是可以的,否則的話你需要手動配置來解決CLI代替你做的那部分工作。
驗證Dapr版本
dapr --version當前時間下,我的版本是
CLI version: 1.4.0 Runtime version: 1.4.3驗證容器
docker psdapr init的容器是包括dapr_placement, dapr_redis, dapr_zipkin
驗證組件目錄,打開目錄%USERPROFILE%\.dapr\,看到如下結構即代表安裝完成
運行一個示例代碼看看效果
運行環境準備
vs 2022/2019,建議直接上VS2022體驗64位VS
https://visualstudio.microsoft.com/zh-hans/vs/preview/#download-preview
.net 6,如果你用的不是vs2022,需要自行安裝.net 6
https://download.visualstudio.microsoft.com/download/pr/5bc3c525-d6d5-4370-8468-e44a1b948c03/fe758c9942966b88b52d92ce2823f6c0/dotnet-sdk-6.0.100-rc.2.21505.57-win-x64.exe
從Github下載示例代碼
使用命令行工具克隆代碼庫到本地
ssh(推薦)用下面的命令
git clone git@github.com:doddgu/dapr-study-room.githttps用下面的命令
git clone https://github.com/doddgu/dapr-study-room.git沒有git的需要先安裝一下git,具體教程可以自行百度
https://github.com/git-for-windows/git/releases/download/v2.33.1.windows.1/Git-2.33.1-64-bit.exe
使用Dapr運行.Net示例代碼
使用命令行工具,跳轉到源碼目錄dapr-study-room\Assignment03\Assignment.Server
Tips:在資源管理器找到源碼目錄
Win 11可以右鍵空白區域 -> 在Windows終端中打開即可
Win 11以下在資源管理器輸入cmd
執行命令dapr run --app-id assignment-server --app-port 5038 dotnet run
可以先不用了解命令行的意思,后續章節會講解
看到輸入內容如下即運行成功
== APP == info: Microsoft.Hosting.Lifetime[14] == APP == Now listening on: https://localhost:7038 == APP == info: Microsoft.Hosting.Lifetime[14] == APP == Now listening on: http://localhost:5038 == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Application started. Press Ctrl+C to shut down. == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Hosting environment: Development == APP == info: Microsoft.Hosting.Lifetime[0] == APP == Content root path: D:\Project\OpenSource\dapr-study-room\Assignment03\Assignment.Server time="2021-10-27T18:06:11.8422605+08:00" level=info msg="application discovered on port 5038" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime type=log ver=1.4.3 time="2021-10-27T18:06:11.8788949+08:00" level=info msg="application configuration loaded" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime type=log ver=1.4.3 time="2021-10-27T18:06:11.8803982+08:00" level=info msg="actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime.actor type=log ver=1.4.3 time="2021-10-27T18:06:11.8844485+08:00" level=info msg="dapr initialized. Status: Running. Init Elapsed 1830.3305ms" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime type=log ver=1.4.3 time="2021-10-27T18:06:11.8920835+08:00" level=info msg="placement tables updated, version: 0" app_id=assignment-server instance=SSHZ014 scope=dapr.runtime.actor.internal.placement type=log ver=1.4.3還有一個驗證方式,使用命令行工具執行命令dapr list
其中APP ID,APP PORT,COMMAND是不變的,其余都是變的
APP ID ? ? ? ? ? ? HTTP PORT ?GRPC PORT ?APP PORT ?COMMAND ? ? AGE ?CREATED ? ? ? ? ? ? ?PID
assignment-server ?49948 ? ? ?49949 ? ? ?5038 ? ? ?dotnet run ?13m ?2021-10-27 18:06.09 ?22596
調用Dapr API
重新打開一個命令行工具
測試Dapr API,再打開一個命令行工具
dapr invoke --app-id assignment-server --method hello驗證運行是否成功
執行dapr inovoke的命令行窗口輸出如下
App invoked successfully
執行dapr run的命令行窗口輸出如下
== APP == Hello World!
這個Hello World就是Assignment.Server的API /hello 打印出來的,此時你的環境已經ok,且第一個示例也運行成功了
給自己點個贊吧👍
在這個環境的搭建過程中,可以看到dapr還有點“稚嫩”,畢竟現在才1.4版本,還有很長的路要走
但是這并不妨礙他帶來的一些思想的進步,如果你錯過了微服務的第一波浪潮,也錯過了Service Mesh,那分布式應用運行時(Dapr)可以作為你新的起點
本章源碼
Assignment03
https://github.com/doddgu/dapr-study-room
我們正在行動,新的框架、新的生態
我們的目標是自由的、易用的、可塑性強的、功能豐富的、健壯的。
所以我們借鑒Building blocks的設計理念,正在做一個新的框架MASA Framework,它有哪些特點呢?
原生支持Dapr,且允許將Dapr替換成傳統通信方式
架構不限,單體應用、SOA、微服務都支持
支持.Net原生框架,降低學習負擔,除特定領域必須引入的概念,堅持不造新輪子
豐富的生態支持,除了框架以外還有組件庫、權限中心、配置中心、故障排查中心、報警中心等一系列產品
核心代碼庫的單元測試覆蓋率90%+
開源、免費、社區驅動
還有什么?我們在等你,一起來討論
經過幾個月的生產項目實踐,已完成POC,目前正在把之前的積累重構到新的開源項目中
目前源碼已開始同步到Github(文檔站點在規劃中,會慢慢完善起來):
MASA.BuildingBlocks
MASA.Contrib
MASA.Utils
MASA.EShop
BlazorComponent
MASA.Blazor
QQ群:7424099
微信群:加技術運營微信(MasaStackTechOps),備注來意,邀請進群
總結
以上是生活随笔為你收集整理的手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET Conf 2021 正在进行中
- 下一篇: 如何在 .NET Core 中获取 CP