手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序
注意:
文章中提到的命令行工具即是Windows Terminal/PowerShell/cmd其中的一個(gè),推薦使用Windows Terminal
運(yùn)行命令行工具的時(shí)候建議以管理員身份,避免踩坑
為了保證操作順暢,建議使用PowerShell先執(zhí)行一下set-ExecutionPolicy RemoteSigned
安裝Docker
因?yàn)镈apr CLI默認(rèn)會(huì)在Docker內(nèi)啟動(dòng) redis、zipkin、placement。
當(dāng)然這些也不是必須要安裝的,只是推薦安裝可以體驗(yàn)Dapr的完整能力,方便后續(xù)章節(jié)的學(xué)習(xí)。
下載并安裝Docker Desktop
https://www.docker.com/products/docker-desktop
安裝WSL2,使用命令行工具執(zhí)行命令
wsl --instal如果不能使用wsl直接安裝的話可以手動(dòng)安裝,運(yùn)行PowerShell并執(zhí)行下面兩句命令
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart如果你再次遇到錯(cuò)誤提示:0x800f080c 功能名稱 VirtualMachinePlatform 未知。
請(qǐng)保證自己的操作系統(tǒng)版本在Windows 10 build 18917以上
重啟電腦后下載WSL2內(nèi)核
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
運(yùn)行命令行工具,設(shè)置默認(rèn)使用WSL2
wsl --set-default-version 2下載Ubuntu 20.04
https://www.microsoft.com/store/apps/9n6svws3rx71
設(shè)置Docker使用WSL2
你“可能”需要一個(gè)小工具
不能說的秘密,看文件名猜功能
https://github.com.cnpmjs.org/dotnetcore/FastGithub/releases/download/2.0.4/fastgithub_win-x64.zip
安裝Dapr CLI
官方解釋:Dapr CLI 是您用于各種 Dapr 相關(guān)任務(wù)的主要工具。您可以使用它來運(yùn)行一個(gè)帶有Dapr sidecar的應(yīng)用程序, 以及查看sidecar日志、列出運(yùn)行中的服務(wù)、運(yùn)行 Dapr 儀表板。
CLI是必須要安裝嗎?其實(shí)也不是,但新手不建議去體驗(yàn)這些騷操作。后續(xù)文章會(huì)講解脫離dapr cli工作。
運(yùn)行Windows Terminal或PowerShell,執(zhí)行命令,這里要耐心等待一下
iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex如果是cmd執(zhí)行下面命令:
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"驗(yàn)證安裝,重新打開命令行工具執(zhí)行命令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
使用命令行工具執(zhí)行命令
dapr init如此之外還有一個(gè)精簡(jiǎn)版的安裝方式
dapr init --slim看看官方解釋slim
在此模式下安裝了兩個(gè)不同的二進(jìn)制文件 daprd 和 placement。placement是給actor用的,之前有提到過。
在此模式下,不會(huì)為狀態(tài)管理或發(fā)布/訂閱安裝任何默認(rèn)組件(如 Redis),那就只能用服務(wù)調(diào)用。其他的需要用戶自己設(shè)置環(huán)境和自定義組件。
說白了,你只用服務(wù)調(diào)用,那slim是可以的,否則的話你需要手動(dòng)配置來解決CLI代替你做的那部分工作。
驗(yàn)證Dapr版本
dapr --version當(dāng)前時(shí)間下,我的版本是
CLI version: 1.4.0 Runtime version: 1.4.3驗(yàn)證容器
docker psdapr init的容器是包括dapr_placement, dapr_redis, dapr_zipkin
驗(yàn)證組件目錄,打開目錄%USERPROFILE%\.dapr\,看到如下結(jié)構(gòu)即代表安裝完成
運(yùn)行一個(gè)示例代碼看看效果
運(yùn)行環(huán)境準(zhǔn)備
vs 2022/2019,建議直接上VS2022體驗(yàn)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沒有g(shù)it的需要先安裝一下git,具體教程可以自行百度
https://github.com/git-for-windows/git/releases/download/v2.33.1.windows.1/Git-2.33.1-64-bit.exe
使用Dapr運(yùn)行.Net示例代碼
使用命令行工具,跳轉(zhuǎn)到源碼目錄dapr-study-room\Assignment03\Assignment.Server
Tips:在資源管理器找到源碼目錄
Win 11可以右鍵空白區(qū)域 -> 在Windows終端中打開即可
Win 11以下在資源管理器輸入cmd
執(zhí)行命令dapr run --app-id assignment-server --app-port 5038 dotnet run
可以先不用了解命令行的意思,后續(xù)章節(jié)會(huì)講解
看到輸入內(nèi)容如下即運(yùn)行成功
== 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還有一個(gè)驗(yàn)證方式,使用命令行工具執(zhí)行命令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
調(diào)用Dapr API
重新打開一個(gè)命令行工具
測(cè)試Dapr API,再打開一個(gè)命令行工具
dapr invoke --app-id assignment-server --method hello驗(yàn)證運(yùn)行是否成功
執(zhí)行dapr inovoke的命令行窗口輸出如下
App invoked successfully
執(zhí)行dapr run的命令行窗口輸出如下
== APP == Hello World!
這個(gè)Hello World就是Assignment.Server的API /hello 打印出來的,此時(shí)你的環(huán)境已經(jīng)ok,且第一個(gè)示例也運(yùn)行成功了
給自己點(diǎn)個(gè)贊吧👍
在這個(gè)環(huán)境的搭建過程中,可以看到dapr還有點(diǎn)“稚嫩”,畢竟現(xiàn)在才1.4版本,還有很長(zhǎng)的路要走
但是這并不妨礙他帶來的一些思想的進(jìn)步,如果你錯(cuò)過了微服務(wù)的第一波浪潮,也錯(cuò)過了Service Mesh,那分布式應(yīng)用運(yùn)行時(shí)(Dapr)可以作為你新的起點(diǎn)
本章源碼
Assignment03
https://github.com/doddgu/dapr-study-room
我們正在行動(dòng),新的框架、新的生態(tài)
我們的目標(biāo)是自由的、易用的、可塑性強(qiáng)的、功能豐富的、健壯的。
所以我們借鑒Building blocks的設(shè)計(jì)理念,正在做一個(gè)新的框架MASA Framework,它有哪些特點(diǎn)呢?
原生支持Dapr,且允許將Dapr替換成傳統(tǒng)通信方式
架構(gòu)不限,單體應(yīng)用、SOA、微服務(wù)都支持
支持.Net原生框架,降低學(xué)習(xí)負(fù)擔(dān),除特定領(lǐng)域必須引入的概念,堅(jiān)持不造新輪子
豐富的生態(tài)支持,除了框架以外還有組件庫、權(quán)限中心、配置中心、故障排查中心、報(bào)警中心等一系列產(chǎn)品
核心代碼庫的單元測(cè)試覆蓋率90%+
開源、免費(fèi)、社區(qū)驅(qū)動(dòng)
還有什么?我們?cè)诘饶?#xff0c;一起來討論
經(jīng)過幾個(gè)月的生產(chǎn)項(xiàng)目實(shí)踐,已完成POC,目前正在把之前的積累重構(gòu)到新的開源項(xiàng)目中
目前源碼已開始同步到Github(文檔站點(diǎn)在規(guī)劃中,會(huì)慢慢完善起來):
MASA.BuildingBlocks
MASA.Contrib
MASA.Utils
MASA.EShop
BlazorComponent
MASA.Blazor
QQ群:7424099
微信群:加技術(shù)運(yùn)營(yíng)微信(MasaStackTechOps),備注來意,邀請(qǐng)進(jìn)群
總結(jié)
以上是生活随笔為你收集整理的手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET Conf 2021 正在进行中
- 下一篇: asp.net ajax控件工具集 Au