上一篇:手把手教你学Dapr - 2. 必须知道的概念
注意:
文章中提到的命令行工具即是
Windows Terminal
/PowerShell
/cmd
其中的一个,推荐使用Windows Terminal
运行命令行工具的时候建议以管理员身份,避免踩坑
为了保证操作顺畅,建议使用PowerShell先执行一下
set-ExecutionPolicy RemoteSigned
因为Dapr CLI默认会在Docker内启动 redis、zipkin、placement。
当然这些也不是必须要安装的,只是推荐安装可以体验Dapr的完整能力,方便后续章节的学习。
下载并安装Docker Desktop
https://www.docker.com/products/docker-desktop
安装WSL2,使用命令行工具执行命令
wsl --instal
PowerShell
并执行下面两句命令dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
请保证自己的操作系统版本在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 相关任务的主要工具。 您可以使用它来运行一个带有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 Runtime Usage: dapr [command] Available Commands: completion Generates shell completion scripts components List all Dapr components. Supported platforms: Kubernetes configurations List all Dapr configurations. Supported platforms: Kubernetes dashboard Start Dapr dashboard. Supported platforms: Kubernetes and self-hosted help Help about any command init Install Dapr on supported hosting platforms. Supported platforms: Kubernetes and self-hosted invoke Invoke a method on a given Dapr application. Supported platforms: Self-hosted list List all Dapr instances. Supported platforms: Kubernetes and self-hosted logs Get Dapr sidecar logs for an application. Supported platforms: Kubernetes mtls Check if mTLS is enabled. Supported platforms: Kubernetes publish Publish a pub-sub event. Supported platforms: Self-hosted run Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted status Show the health status of Dapr services. Supported platforms: Kubernetes stop Stop Dapr instances and their associated apps. . Supported platforms: Self-hosted uninstall Uninstall Dapr runtime. Supported platforms: Kubernetes and self-hosted upgrade Upgrades a Dapr control plane installation in a cluster. Supported platforms: Kubernetes Flags: -h, --help help for dapr -v, --version version for dapr Use "dapr [command] --help" for more information about a command.
使用命令行工具执行命令
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 ps
dapr 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
使用命令行工具克隆代码库到本地
ssh(推荐)用下面的命令
git clone git@github.com:doddgu/dapr-study-room.git
https用下面的命令
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-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 invoke --app-id assignment-server --method hello
验证运行是否成功
执行dapr inovoke的命令行窗口输出如下
App invoked successfully
执行dapr run的命令行窗口输出如下
== APP == Hello World!
这个Hello World就是Assignment.Server的API /hello
打印出来的,此时你的环境已经ok,且第一个示例也运行成功了