关于c#:手把手教你学Dapr-3-使用Dapr运行第一个Net程序

3次阅读

共计 6061 个字符,预计需要花费 16 分钟才能阅读完成。

留神:

文章中提到的命令行工具即是 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/produc…

  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….

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

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

    https://www.microsoft.com/sto…

  6. 设置 Docker 应用 WSL2

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

不能说的机密,看文件名猜性能

https://github.com.cnpmjs.org…

装置 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\,看到如下构造即代表装置实现

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

运行环境筹备

vs 2022/2019,倡议间接上 VS2022 体验 64 位 VS

https://visualstudio.microsof…

.net 6,如果你用的不是 vs2022,须要自行装置.net 6

https://download.visualstudio…

从 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-wi…

应用 Dapr 运行.Net 示例代码

  1. 应用命令行工具,跳转到源码目录dapr-study-room\Assignment03\Assignment.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:\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
  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/dap…

咱们正在口头,新的框架、新的生态

咱们的指标是 自在的 易用的 可塑性强的 功能丰富的 强壮的

所以咱们借鉴 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),备注来意,邀请进群

转载自:(鬼谷子)

正文完
 0