当前位置:网站首页>Teach you to learn dapr - 3 Run the first with dapr Net program

Teach you to learn dapr - 3 Run the first with dapr Net program

2022-06-26 16:45:00 Masa technical team

Be careful :

The command line tool mentioned in this article is Windows Terminal/PowerShell/cmd One of them , Recommended Windows Terminal

It is recommended to run the command line tool as an administrator , Avoid trampling

In order to ensure smooth operation , It is recommended to use PowerShell So let's do that set-ExecutionPolicy RemoteSigned

install Docker

because Dapr CLI The default will be Docker Start inside redis、zipkin、placement.

Of course, these are not necessary to install , It is only recommended that you can experience Dapr Complete capability of , Facilitate the learning of subsequent chapters .

  1. Download and install Docker Desktop

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

  2. install WSL2, Use the command line tool to execute the command

    wsl --instal
    If you can't use wsl If you install it directly, you can install it manually , function PowerShell And execute the following two commands
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestartdism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    If you encounter an error again :0x800f080c The name of the function VirtualMachinePlatform Unknown .

    Please ensure that your operating system version is Windows 10 build 18917 above

  3. Restart the computer and download WSL2 kernel

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

  4. Run the command line , Set default usage WSL2

    wsl --set-default-version 2
  5. download Ubuntu 20.04

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

  6. Set up Docker Use WSL2

    image2021-5-14_11-29-45.png

you “ Probably ” Need a gadget

A secret that can't be said , Look at the file name guessing function

https://github.com.cnpmjs.org/dotnetcore/FastGithub/releases/download/2.0.4/fastgithub_win-x64.zip

install Dapr CLI

Official explanation :Dapr CLI You use it for all kinds of Dapr The main tools for related tasks . You can use it to run a program with Dapr sidecar Applications for , And view sidecar journal 、 List running services 、 function Dapr instrument panel .

CLI Is it necessary to install ? It's not , But novices are not recommended to experience these operations . The following articles will explain dapr cli Work .

  1. function Windows Terminal or PowerShell, Carry out orders , Wait patiently here

    iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex

    If it is cmd Execute the following command :

    powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
  2. Verify the installation , Reopen Command line tools execute commands dapr, See the following prompt that the installation is correct

             __    ____/ /___ _____  _____   / __  / __ '/ __ \/ ___/  / /_/ / /_/ / /_/ / /  \__,_/\__,_/ .___/_/              /_/===============================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.

initialization Dapr

  1. Use the command line tool to execute the command

    dapr init

    In addition, there is a simplified installation method

    dapr init --slim

    Look at the official explanation slim

    Two different binaries are installed in this mode daprd and placement. placement It's for actor With , It was mentioned before .

    In this mode , Will not manage or publish for status / Subscribe to install any default components ( Such as Redis), Then you can only use service calls . Others require users to set their own environment and custom components .

    To put it bluntly , You only use service calls , that slim Yes. , Otherwise, you need to configure it manually to solve CLI Take your place of that part of the work .

  2. verification Dapr edition

    dapr --version

    At the current time , My version is

    CLI version: 1.4.0Runtime version: 1.4.3
  3. Verify container

    docker ps

    dapr init The container is made up of dapr_placement, dapr_redis, dapr_zipkin

  4. Verify component Directory , Open Directory %USERPROFILE%\.dapr\, See the following structure, which means that the installation is completed

    16353073367779.png

Run a sample code to see the effect

Operation environment preparation

vs 2022/2019, It is suggested to go straight to VS2022 Experience 64 position VS

https://visualstudio.microsoft.com/zh-hans/vs/preview/#download-preview

.net 6, If you don't use it vs2022, Self installation required .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

from Github Download sample code

Use the command line tool to clone the code base to the local

ssh( recommend ) Use the following command

git clone [email protected]:doddgu/dapr-study-room.git

https Use the following command

git clone https://github.com/doddgu/dapr-study-room.git

No, git You need to install it first git, Specific tutorials can be self Baidu

https://github.com/git-for-windows/git/releases/download/v2.33.1.windows.1/Git-2.33.1-64-bit.exe

Use Dapr function .Net Sample code

  1. Use command line tools , Jump to the source directory dapr-study-room\Assignment03\Assignment.Server

    Tips: Find the source directory in Explorer

    Win 11 You can right-click the blank area -> stay Windows Just open it in the terminal

    Win 11 Enter the following in Explorer cmd

  2. Carry out orders dapr run --app-id assignment-server --app-port 5038 dotnet run

    You don't have to understand the meaning of the command line , The following chapters will explain

  3. See the input as follows, that is, the operation is successful

    == 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. There is another verification method , Use the command line tool to execute the command dapr list

    among APP ID,APP PORT,COMMAND It is the same. , The rest is changed

    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

call Dapr API

  1. Reopen a command line tool

  2. test Dapr API, Open another command line tool

    dapr invoke --app-id assignment-server --method hello
  3. Verify that the operation was successful

    perform dapr inovoke The output of the command line window is as follows

    App invoked successfully

    perform dapr run The output of the command line window is as follows

    == APP == Hello World!

    This Hello World Namely Assignment.Server Of API /hello Printed , At this point, your environment has ok, And the first example runs successfully

Give yourself a compliment

In the process of building this environment , You can see dapr Still some “ Immature ”, After all, it's only now 1.4 edition , There is still a long way to go

But this does not hinder some of the ideological progress he brought , If you miss the first wave of microservices , Also missed Service Mesh, that Distributed application runtime (Dapr) It can be your new starting point

Source code of this chapter

Assignment03

https://github.com/doddgu/dapr-study-room

We are acting , New framework 、 New ecology

Our goal is The freedom of the Easy-to-use Highly malleable functional Robust .

So we learn from Building blocks Design concept of , Working on a new framework MASA Framework, What are its characteristics ?

  • Native support Dapr, And allow Dapr Replace with traditional means of communication
  • Unlimited architecture , Single application 、SOA、 Micro services support
  • Support .Net Native framework , Reduce the burden of learning , In addition to the concepts that must be introduced in a specific field , Insist on not making new wheels
  • Rich ecological support , In addition to the framework, there are component libraries 、 Authority Center 、 Configuration center 、 Troubleshooting center 、 A series of products such as Alarm Center
  • Unit test coverage of the core code base 90%+
  • Open source 、 free 、 Community driven
  • What is the ? We are waiting for you , Come together and discuss

After several months of production project practice , Completed POC, At present, the previous accumulation is being refactored into new open source projects

At present, the source code has been synchronized to Github( The document site is under planning , Will gradually improve ):

MASA.BuildingBlocks

MASA.Contrib

MASA.Utils

MASA.EShop

BlazorComponent

MASA.Blazor

QQ Group :7424099

Wechat group : Plus technology operation wechat (MasaStackTechOps), Remarks , Invite in

masa_stack_tech_ops.png

Reprinted from :( guiguzi )

原网站

版权声明
本文为[Masa technical team]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170506438038.html