当前位置:网站首页>Teach you to learn dapr - 8 binding

Teach you to learn dapr - 8 binding

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

Catalog

Hands teach you how to Dapr - 1. .Net The era of developers

Hands teach you how to Dapr - 2. Must know the concept of

Hands teach you how to Dapr - 3. Use Dapr Run the first .Net Program

Hands teach you how to Dapr - 4. The service call

Hands teach you how to Dapr - 5. State management

Hands teach you how to Dapr - 6. Publish subscribe

Hands teach you how to Dapr - 7. Actors

Hands teach you how to Dapr - 8. binding

Introduce

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

  • Eliminate connection and polling message systems ( Such as queues and message buses ) Complexity
  • Focus on business logic , Rather than the implementation details of how to interact with the system
  • Protect your code from SDK Or the impact of the library
  • Handle retry and failback
  • The runtime switches between bindings
  • Building portable applications , Environment specific bindings are set , No need to change the code

Input binding

The input binding is used to trigger your application when an event from an external resource occurs . Optional payload and Metadata Can be sent with the request .

To receive events from the input binding :

  1. Define and describe binding types and their metadata ( Connection information, etc ) The components of YAML
  2. Listen for incoming events HTTP Endpoint , Or use gRPC proto Library to get incoming events

Output binding

The output binding allows you to invoke external resources . Optional payload and Metadata Can be sent with the request .

To invoke the output binding :

  1. Define and describe binding types and their metadata ( Connection information, etc ) The components of YAML
  2. Use HTTP or gRPC Method calls have optional payload The binding of

Use scenarios

Use bindings , Your code can be triggered by incoming events from different resources , These resources can be anything : queue 、 Messaging pipeline 、 The cloud service 、 File system, etc .

This is important for event driven processing 、 A data pipeline or just a general response to events and further processing is ideal .

Dapr Binding allows you to :

  • Does not contain specific SDK Or library
  • Replace the binding without changing the code
  • Focus on business logic rather than event resource implementation

at present Dapr Cross is not yet supported Dapr Mutual call , and yaron One of the solutions given is binding

Currently, binding supports 40 Middle component , Include Aliyun、Azure、AWS And other cloud service vendors , It also includes common such as Cron, kafka, MQTT, SMTP, Redis As well as a variety of MQ etc. .

The picture below is .Net Dapr An example in the official tutorial

input-binding-flow.png

Configuration components

This article will use rabbitmq To demonstrate ( Why not redis, because redis Rollover , Only support output, I didn't notice supported), As mentioned in previous articles , First configure yaml

  1. install rabbitmq

    docker pull rabbitmq:3.8.25-management
  2. function rabbitmq

    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 Open Directory %USERPROFILE%\.dapr\components, establish binding.yaml, The contents are as follows

    apiVersion: dapr.io/v1alpha1kind: Componentmetadata:  name: myevent  namespace: defaultspec:  type: bindings.rabbitmq  version: v1  metadata:  - name: queueName    value: queue1  - name: host    value: amqp://admin:[email protected]: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 , Input url:http://localhost:15672/, The account password is admin, see rabbitmq It's working properly

    1.png

.Net call Dapr The binding of

establish Assignment.Server

establish Class library project , And add Dapr.Actors.AspNetCoreNuGet Package references and Assignment.Shared Project reference , 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();

notes : Be sure to use POST Method, Remember the parameters in Body Inside . Default Url And bindings Of name Corresponding . Can you change the route ? Of course I can , See the configuration below

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

function Assignment.Server

Use Dapr CLI To start up , First use the command line tool to jump to the directory dapr-study-room\Assignment07\Assignment.Server, Then execute the following command

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

Verify whether the server binding configuration is successful

Open the browser , Input url:http://localhost:15672/#/queues, The account password is admin, Check to see if you have created a file named queue1 Queues , As shown in the figure below :

2.png

establish Assignment.Client

establish Console project , And add Dapr.ActorsNuGet Package references and Assignment.Shared Project reference .

modify Program.cs

using Dapr.Client;var client = new DaprClientBuilder().Build();await client.InvokeBindingAsync("myevent", "create", "World");Console.WriteLine("Binding sent.");

function Assignment.Client

Use Dapr CLI To start up , First use the command line tool to jump to the directory dapr-study-room\Assignment07\Assignment.Client, Then execute the following command

dotnet run

Source code of this chapter

Assignment08

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

原网站

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

随机推荐