留神:

文章中提到的命令行工具即是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 /norestartdism.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 RuntimeUsage:  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: KubernetesFlags:  -h, --help      help for dapr  -v, --version   version for daprUse "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.0Runtime 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.Servertime="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.3time="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.3time="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.3time="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.3time="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),备注来意,邀请进群

转载自:(鬼谷子)