Teach you to learn Dapr - 8. Binding

content

Teach you to learn Dapr - 1. The big era of .Net developers

Teach you to learn Dapr - 2. Must-know concepts

Teach you to learn Dapr step by step - 3. Use Dapr to run the first .Net program

Teach you to learn Dapr - 4. Service call

Teach you to learn Dapr - 5. State management

Teach you to learn Dapr - 6. Publish and subscribe

Teach you to learn Dapr - 7. Actors

Teach you to learn Dapr - 8. Binding

introduce

Using bindings, you can use events from external systems to trigger your application, or interact with external systems. This building block provides you and your code with several benefits:

  • Eliminate the complexity of connecting and polling message systems such as queues and message buses
  • Focus on business logic, not implementation details of how to interact with the system
  • Make your code independent of SDKs or libraries
  • Handling retries and failback
  • Switch between bindings at runtime
  • Build portable applications where environment-specific bindings are set and no code changes are required

input binding

Input bindings are used to trigger your application when events from external resources occur. Optional payloadand 元数据can be sent with the request.

To receive events from input bindings:

  1. Defines component YAML that describes the binding type and its metadata (connection information, etc.)
  2. An HTTP endpoint to listen for incoming events, or use the gRPC proto library to get incoming events

output binding

Output bindings allow you to call external resources. Optional payloadand 元数据can be sent with the request.

In order to invoke the output binding:

  1. Defines component YAML that describes the binding type and its metadata (connection information, etc.)
  2. Use HTTP or gRPC method calls with optional payloadbindings

scenes to be used

Using bindings, your code can be triggered by incoming events from different resources, which can be anything: queues, messaging pipelines, cloud services, filesystems, etc.

This is ideal for event-driven processing, data pipelines, or just general reacting to events and further processing.

Dapr bindings allow you to:

  • Receive events without including a specific SDK or library
  • Replacing bindings without changing code
  • Focus on business logic rather than event resource implementation

At present, Dapr does not support cross-Dapr mutual calls, and yaronone of the solutions given is binding

Currently binding supports 40 components, including Aliyun, Azure, AWS and other cloud service vendors' products, as well as common ones such as Cron, kafka, MQTT, SMTP, Redisand various MQs.

The following image is an example from the official .Net Dapr tutorial

input-binding-flow.png

Configure components

This article will be rabbitmqused to demonstrate (why not use redis, because redis overturned, only supports output, did not pay attention to supported), as mentioned in the previous articles, configure yaml first

  1. Installrabbitmq

    docker pull rabbitmq:3.8.25-management
    
  2. runrabbitmq

    docker run -d --hostname rabbitMQ --name my-rabbitMQ -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:3.8.25-management
    
  3. Windows opens the directory %USERPROFILE%\.dapr\components, creates binding.yamlit, and the contents are as follows

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: myevent
      namespace: default
    spec:
      type: bindings.rabbitmq
      version: v1
      metadata:
      - name: queueName
        value: queue1
      - name: host
        value: amqp://admin:admin@localhost:5672
      - name: durable
        value: true
      - name: deleteWhenUnused
        value: false
      - name: ttlInSeconds
        value: 60
      - name: prefetchCount
        value: 0
      - name: exclusive
        value: false
      - name: maxPriority
        value: 5
    
  4. Open the browser, enter url: http://localhost:15672/, the account password is admin, and check that rabbitmq is running normally

    1.png

.Net calls the binding of Dapr

Create Assignment.Server

Create a 类库project, add Dapr.Actors.AspNetCoreNuGet package reference and Assignment.Sharedproject reference, and finally modify the program port to 5000.

Modify program.cs

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapPost("/myevent", ([Microsoft.AspNetCore.Mvc.FromBody] string word) => Console.WriteLine($"Hello {word}!"));

app.Run();

: Be sure to use POSTMethod, and remember the parameters in the Body. The default Url corresponds to the name of the bindings. Can you change the route? Of course, see the configuration below

spec:
  type: binding.rabbitmq
  metadata:
  - name: route
    value: /onevent

Run Assignment.Server

Use Dapr CLI to start, first use the command line tool to jump to the directory dapr-study-room\Assignment07\Assignment.Server, and then execute the following command

dapr run --app-id testbinding --app-port 5000 --dapr-http-port 3500 --dapr-grpc-port 50001 dotnet run

Verify that the server-side binding configuration is successful

Open the browser, enter url: http://localhost:15672/#/queues, the account password is admin, and check whether a queue1queue named has been created, as shown in the following figure:

2.png

Create Assignment.Client

Create a 控制台project and add Dapr.ActorsNuGet package reference and Assignment.Sharedproject reference.

Modify Program.cs

using Dapr.Client;

var client = new DaprClientBuilder().Build();
await client.InvokeBindingAsync("myevent", "create", "World");

Console.WriteLine("Binding sent.");

Run Assignment.Client

Use Dapr CLI to start, first use the command line tool to jump to the directory dapr-study-room\Assignment07\Assignment.Client, and then execute the following command

dotnet run

Source code of this chapter

Assignment08

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

We are in action, new framework, new ecology

Our goals are 自由的, 易用的, 可塑性强的, 功能丰富的, 健壮的.

So we draw on the design concept of Building blocks and are making a new framework MASA Framework. What are its characteristics?

  • Natively supports Dapr and allows replacing Dapr with traditional communication methods
  • Unlimited architecture, monolithic applications, SOA, and microservices are supported
  • Support .Net native framework, reduce learning burden, and insist not to create new wheels except for concepts that must be introduced in specific fields
  • Rich ecological support, in addition to the framework, there are a series of products such as component library, authority center, configuration center, troubleshooting center, and alarm center
  • Unit test coverage of core code base is 90%+
  • Open source, free, community driven
  • what else? We are waiting for you, let's discuss together

After several months of production project practice, the POC has been completed, and the previous accumulation is currently being refactored into a new open source project

At present, the source code has begun to be synchronized to Github (the documentation site is under planning and will be gradually improved):

MASA.BuildingBlocks

MASA.Contrib

MASA.Utils

MASA.EShop

BlazorComponent

MASA.Blazor

QQ group: 7424099

WeChat group: add technology operation WeChat (MasaStackTechOps), note your intention, invite to join the group

masa_stack_tech_ops.png

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324029832&siteId=291194637