Dapr in vs2019 + Net core 3.1 environment trial

Last article< Installing Dapr for Windows 10 in domestic environment >, we have successfully installed Dapr CLI on the computer. Next, let's try Dapr.

 

Development environment: Visual Studio 2019 comunity / Net core 3.1 / dapr cli 1.5 / windows 10 enterprise 19043.1415

 

Zipkin and Dapr Dashboard

First, introduce the two auxiliary tools that come with the installation of the two DaprCLI.

Zipkin is a link tracking system. All our calls in Dapr will be displayed in the, which is convenient for us to test and debug Dapr. By default, you can enter in the browser http://localhost:9411 Open its page.

 

 

 

The dashboard needs to be turned on to run

dapr dashboard

If dapr dashboard running on is displayed http://localhost:8080 It means that it is successfully enabled. Then you can see which dapr apps are currently running on the corresponding web page

 

 

 

 

Dapr CLI command

Enter dapr in CMD to see the command list of Dapr CLI:

 

 

We can check the official Dapr document library To view a detailed description of each command.

Among them, init command Used to initialize the CLI environment, uninstall command For uninstall, upgrade command For upgrade. These three commands are important, but they are used less frequently after configuration.

In this article, focus on run command , and invoke command , one of them is responsible for running and the other is responsible for calling. They are all commonly used commands in development.

 

Dapr trial: call dapr with manual command

Let's create a new ASP Net core web API project. It should be noted that if Configure for HTTPS is checked during creation, the - App SSL parameter must be specified as true when running the run command later, otherwise it will appear the server closed connection before returning the first response byte Wrong.

After the project is created, we modify startup CS ConfigureServices method is as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddDapr();
}

Create another MemberController:

[Route("api/[controller]")]
    [ApiController]
    public class MemberController : ControllerBase
    {
        private readonly ILogger<MemberController> _logger;
        private readonly DaprClient _daprClient;

        public MemberController(ILogger<MemberController> logger, DaprClient daprClient)
        {
            _logger = logger;
            _daprClient = daprClient;
        }

        [HttpGet]
        [Route("Get")]
        public int Get()
        {
            return new Random().Next(10000, 10086);
        }
    }

The sample program is very simple. When there is a "Get" request, it returns a random integer between 10000 and 10086.

After the compilation is passed, open the folder where the compiled file is located and enter the following similar commands:

Dapr run -a MemberService -G 50004 -H 30004 -p 5004 -- dotnet Services.Member.dll --urls "http://*:5004"

Here, I use abbreviations for the parameters of the Dapr run command. The meanings of each parameter are as follows:

-a: -- app Id, the application Id used for service discovery, which should be unique in Dapr SideCar

-G: -- Dapr gRPC port, gRPC port that Dapr wants to listen on

-H: -- Dapr HTTP port, the HTTP port that Dapr wants to listen on

-p: -- app port, the port on which the application is listening

--dotnet: start the corresponding Net program, later Net program path

--URLs: configure URLs for application listening. See ASP. Five ways to configure monitoring URLs in. Net core

In this way, it can be deployed through dapr net application and dapr sidecar are integrated to test the application.

 

After dapr is started, we can see two related processes in the task manager:

 

 

Among them, dapr is directly started by the command just now, and daprd is driven by dapr, which is the process of real work. In fact, we can run daprd directly to achieve the same effect. But in this case, you can't see the app currently running in dapr on the dapr dashboard, so let's do it

 

Open another CMD or Powershell and enter the following command:

dapr invoke --app-id MemberService --method api/Member/Get --verb GET

This command automatically helps us find the MemberService SideCar just started by the run command, then finds the integrated dotnet app, and then finds the Get method in MemberController according to the method parameter "api/Member/Get", and finally calls it and gets the return value.

 

 

 

Dapr trial: Net program calls dapr

Prepare another ASP Net core web API Application, called Admin, is similar to the above Member project.

Add an AdminController:

[Route("api/[controller]")]
    [ApiController]
    public class AdminController : ControllerBase
    {
        private readonly ILogger<AdminController> _logger;
        private readonly DaprClient _daprClient;

        public AdminController(ILogger<AdminController> logger, DaprClient daprClient)
        {
            _logger = logger;
            _daprClient = daprClient;
        }

        [HttpGet]
        [Route("GetMemberCount")]
        public async Task<int> GetMemberCount()
        {
            _logger.LogInformation("[Begin] Query data from Member Service");

            var memberCount = await _daprClient.InvokeMethodAsync<int>
                (HttpMethod.Get, "MemberService", "api/Member/Get");

            _logger.LogInformation($"[End] Query data from Member Service, data : {memberCount}");

            return memberCount;
        }


        [HttpGet]
        [Route("GetHello")]

      public string GetHello(Person p)
      {
        return $"Hello! {p.Name}!";
      }


    }

public class Person
{
public string Name { get; set; }
}

 

The GetMemberCount method will call the corresponding method of MemberService through DaprClient.

After the compilation is passed, open CMD or Poweshell in the corresponding folder and enter the following command:

Dapr run -a AdminService -G 50001 -H 30001 -p 5001 -- dotnet WesoftPlus.Services.Admin.dll --urls "http://*:5001"

Then input the following command at another terminal:

dapr invoke --app-id AdminService --method api/Admin/GetMemberCount --verb GET

Successfully let AdminService get the required random integer from MemberService:

 

 

 

Of course, as ASP Net core web API Application, we can also access its methods through Http.

Enter the following path in the browser or Postman:

http://localhost:5001/api/Admin/GetMemberCount

 

Dapr CLI pass parameters

 dapr invoke --app-id AdminService --method api/Admin/GetHello --verb GET --data '{\"name\":\"Alvin\"}'

There is a GetHello method with parameters in AdminController. Dapr Invoke certainly supports parameter passing. But it seems that only object parameters are supported, so I define a Person class. When parameters are passed, a serialized string of the Person class is passed.

 

Added by tartou2 on Mon, 10 Jan 2022 04:51:06 +0200