手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序
注意:
文章中提到的命令行工具即是//其中的一个,推荐使用
运行命令行工具的时候建议以管理员身份,避免踩坑
为了保证操作顺畅,建议使用PowerShell先执行一下
安装Docker
因为Dapr CLI默认会在Docker内启动 redis、zipkin、placement。
当然这些也不是必须要安装的,只是推荐安装可以体验Dapr的完整能力,方便后续章节的学习。
wsl --instal
如果不能使用wsl直接安装的话可以手动安装,运行并执行下面两句命令
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以上
wsl --set-default-version 2

你“可能”需要一个小工具
不能说的秘密,看文件名猜功能
安装Dapr CLI
官方解释:Dapr CLI 是您用于各种 Dapr 相关任务的主要工具。 您可以使用它来运行一个带有Dapr sidecar的应用程序, 以及查看sidecar日志、列出运行中的服务、运行 Dapr 仪表板。
CLI是必须要安装吗?其实也不是,但新手不建议去体验这些骚操作。后续文章会讲解脱离dapr cli工作。
iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex
如果是执行下面命令:
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
__
____/ /___ _____ _____
/ __ / __ '/ __ \/ ___/
/ /_/ / /_/ / /_/ / /
\__,_/\__,_/ .___/_/
/_/
===============================
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
dapr init
如此之外还有一个精简版的安装方式
dapr init --slim
看看官方解释slim
在此模式下安装了两个不同的二进制文件 和 。 是给actor用的,之前有提到过。
在此模式下,不会为状态管理或发布/订阅安装任何默认组件(如 Redis),那就只能用服务调用。其他的需要用户自己设置环境和自定义组件。
说白了,你只用服务调用,那slim是可以的,否则的话你需要手动配置来解决CLI代替你做的那部分工作。
dapr --version
当前时间下,我的版本是
CLI version: 1.4.0
Runtime version: 1.4.3
docker ps
dapr init 的容器是包括dapr_placement, dapr_redis, dapr_zipkin

运行一个示例代码看看效果
运行环境准备
vs 2022/2019,建议直接上VS2022体验64位VS
.net 6,如果你用的不是vs2022,需要自行安装.net 6
从Github下载示例代码
使用命令行工具克隆代码库到本地
ssh(推荐)用下面的命令
git clone git@github.com:doddgu/dapr-study-room.git
https用下面的命令
git clone https://github.com/doddgu/dapr-study-room.git
没有git的需要先安装一下git,具体教程可以自行百度
使用Dapr运行.Net示例代码
Tips:在资源管理器找到源码目录
Win 11可以右键空白区域 -> 在Windows终端中打开即可
Win 11以下在资源管理器输入cmd
可以先不用了解命令行的意思,后续章节会讲解
== 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
其中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 inovoke的命令行窗口输出如下
App invoked successfully
执行dapr run的命令行窗口输出如下
== APP == Hello World!
这个Hello World就是Assignment.Server的API 打印出来的,此时你的环境已经ok,且第一个示例也运行成功了
给自己点个赞吧👍
在这个环境的搭建过程中,可以看到dapr还有点“稚嫩”,毕竟现在才1.4版本,还有很长的路要走
但是这并不妨碍他带来的一些思想的进步,如果你错过了微服务的第一波浪潮,也错过了Service Mesh,那可以作为你新的起点
本章源码
Assignment03
我们正在行动,新的框架、新的生态
我们的目标是、、、、。
所以我们借鉴Building blocks的设计理念,正在做一个新的框架,它有哪些特点呢?
原生支持Dapr,且允许将Dapr替换成传统通信方式
架构不限,单体应用、SOA、微服务都支持
支持.Net原生框架,降低学习负担,除特定领域必须引入的概念,坚持不造新轮子
丰富的生态支持,除了框架以外还有组件库、权限中心、配置中心、故障排查中心、报警中心等一系列产品
核心代码库的单元测试覆盖率90%+
开源、免费、社区驱动
还有什么?我们在等你,一起来讨论
经过几个月的生产项目实践,已完成POC,目前正在把之前的积累重构到新的开源项目中
目前源码已开始同步到Github(文档站点在规划中,会慢慢完善起来):
QQ群:7424099
微信群:加技术运营微信(MasaStackTechOps),备注来意,邀请进群

MASA技术团队:(鬼谷子)