手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇:手把手教你学DaPR - 2. 必须知道的概念

注意:

文章中提到的命令行工具即是Windows Terminal/PowerShell/cmd其中的一个,推荐使用Windows Terminal

运行命令行工具的时候建议以管理员身份,避免踩坑

为了保证操作顺畅,建议使用PowerShell先执行一下set-Executionpolicy RemoteSigned

安装docker

因为Dapr CLI默认会在Docker内启动 redis、zipkin、placement。

当然这些也不是必须要安装的,只是推荐安装可以体验Dapr的完整能力,方便后续章节的学习。

  1. 下载并安装Docker Desktop

    https://www.docker.COM/products/docker-desktop

  2. 安装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以上

  3. 重启脑后下载WSL2内核

    https://wslStorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

  4. 运行命令行工具,设置默认使用WSL2

    wsl --set-default-version 2
    
  5. 下载Ubuntu 20.04

    https://www.microsoft.com/store/apps/9n6svws3rx71

  6. 设置Docker使用WSL2

    手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序

你“可能”需要一个小工具

不能说的秘密,看文件名猜功能

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工作。

  1. 运行Windows TerminalPowerShell,执行命令,这里要耐心等待一下

    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"
    
  2. 验证安装,重新打开命令行工具执行命令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

  1. 使用命令行工具执行命令

    dapr init
    

    如此之外还有一个精简版的安装方式

    dapr init --slim
    

    看看官方解释slim

    在此模式下安装了两个不同的二进制文件 daprdplacementplacement是给actor用的,之前有提到过。

    在此模式下,不会为状态管理或发布/订阅安装任何默认组件(如 Redis),那就只能用服务调用。其他的需要用户自己设置环境和自定义组件

    说白了,你只用服务调用,那slim是可以的,否则的话你需要手动配置来解决CLI代替你做的那部分工作。

  2. 验证Dapr版本

    dapr --version
    

    当前时间下,我的版本是

    CLI version: 1.4.0
    Runtime version: 1.4.3
    
  3. 验证容器

    docker ps
    

    dapr init的容器是包括dapr_placement, dapr_redis, dapr_zipkin

  4. 验证组件目录,打开目录%USERPROFILE%.dapr,看到如下结构即代表安装完成

    手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序

运行一个示例代码看看效果

运行环境准备

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.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运行.Net示例代码

  1. 使用命令行工具,跳转到码目录dapr-study-roomAssignment03Assignment.Server

    Tips:在资源管理器找到源码目录

    Win 11可以右键空白区域 -> 在Windows终端中打开即可

    Win 11以下在资源管理器输入cmd

  2. 执行命令dapr run --app-id assignment-server --app-port 5038 dotnet run

    可以先不用了解命令行的意思,后续章节会讲解

  3. 看到输入内容如下即运行成功

    == 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:ProjectOPEnSourcedapr-study-roomAssignment03Assignment.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
    
  4. 还有一个验证方式,使用命令行工具执行命令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

  1. 重新打开一个命令行工具

  2. 测试Dapr API,再打开一个命令行工具

    dapr invoke --app-id assignment-server --method hello
    
  3. 验证运行是否成功

    执行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程序

欢迎加入技术交流群:

手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序

招.Net中/高级开发工程师:坐标杭州下沙,联系方式进群找群主,薪资范围16-30K,有住房补贴

脚本宝典总结

以上是脚本宝典为你收集整理的手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序全部内容,希望文章能够帮你解决手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。