How-To: Trigger your application with input bindings

Use Dapr input bindings to trigger event driven applications

Using bindings, your code can be triggered with incoming events from different resources which can be anything: a queue, messaging pipeline, cloud-service, filesystem etc.

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

Dapr bindings allow you to:

  • Receive events without including specific SDKs or libraries
  • Replace bindings without changing your code
  • Focus on business logic and not the event resource implementation

For more info on bindings, read this overview.

Example:

The below code example loosely describes an application that processes orders. In the example, there is an order processing service which has a Dapr sidecar. The checkout service uses Dapr to trigger the application via an input binding.

Diagram showing bindings of example service

1. Create a binding

An input binding represents a resource that Dapr uses to read events from and push to your application.

For the purpose of this guide, you’ll use a Kafka binding. You can find a list of supported binding components here.

Create a new binding component with the name of checkout.

Inside the metadata section, configure Kafka related properties, such as the topic to publish the message to and the broker.

Create the following YAML file, named binding.yaml, and save this to a components sub-folder in your application directory. (Use the --components-path flag with dapr run to point to your custom components dir)

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: checkout
  5. spec:
  6. type: bindings.kafka
  7. version: v1
  8. metadata:
  9. # Kafka broker connection setting
  10. - name: brokers
  11. value: localhost:9092
  12. # consumer configuration: topic and consumer group
  13. - name: topics
  14. value: sample
  15. - name: consumerGroup
  16. value: group1
  17. # publisher configuration: topic
  18. - name: publishTopic
  19. value: sample
  20. - name: authRequired
  21. value: "false"

To deploy this into a Kubernetes cluster, fill in the metadata connection details of your desired binding component in the yaml below (in this case kafka), save as binding.yaml, and run kubectl apply -f binding.yaml.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: checkout
  5. spec:
  6. type: bindings.kafka
  7. version: v1
  8. metadata:
  9. # Kafka broker connection setting
  10. - name: brokers
  11. value: localhost:9092
  12. # consumer configuration: topic and consumer group
  13. - name: topics
  14. value: sample
  15. - name: consumerGroup
  16. value: group1
  17. # publisher configuration: topic
  18. - name: publishTopic
  19. value: sample
  20. - name: authRequired
  21. value: "false"

2. Listen for incoming events (input binding)

Now configure your application to receive incoming events. If using HTTP, you need to listen on a POST endpoint with the name of the binding as specified in metadata.name in the file.

Below are code examples that leverage Dapr SDKs to demonstrate an output binding.

  1. //dependencies
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System;
  5. using Microsoft.AspNetCore.Mvc;
  6. //code
  7. namespace CheckoutService.controller
  8. {
  9. [ApiController]
  10. public class CheckoutServiceController : Controller
  11. {
  12. [HttpPost("/checkout")]
  13. public ActionResult<string> getCheckout([FromBody] int orderId)
  14. {
  15. Console.WriteLine("Received Message: " + orderId);
  16. return "CID" + orderId;
  17. }
  18. }
  19. }

Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:

  1. dapr run --app-id checkout --app-port 6002 --dapr-http-port 3602 --dapr-grpc-port 60002 --app-ssl dotnet run
  1. //dependencies
  2. import org.springframework.web.bind.annotation.*;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import reactor.core.publisher.Mono;
  6. //code
  7. @RestController
  8. @RequestMapping("/")
  9. public class CheckoutServiceController {
  10. private static final Logger log = LoggerFactory.getLogger(CheckoutServiceController.class);
  11. @PostMapping(path = "/checkout")
  12. public Mono<String> getCheckout(@RequestBody(required = false) byte[] body) {
  13. return Mono.fromRunnable(() ->
  14. log.info("Received Message: " + new String(body)));
  15. }
  16. }

Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:

  1. dapr run --app-id checkout --app-port 6002 --dapr-http-port 3602 --dapr-grpc-port 60002 mvn spring-boot:run
  1. #dependencies
  2. import logging
  3. from dapr.ext.grpc import App, BindingRequest
  4. #code
  5. app = App()
  6. @app.binding('checkout')
  7. def getCheckout(request: BindingRequest):
  8. logging.basicConfig(level = logging.INFO)
  9. logging.info('Received Message : ' + request.text())
  10. app.run(6002)

Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:

  1. dapr run --app-id checkout --app-port 6002 --dapr-http-port 3602 --app-protocol grpc -- python3 CheckoutService.py
  1. //dependencies
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "github.com/gorilla/mux"
  7. )
  8. //code
  9. func getCheckout(w http.ResponseWriter, r *http.Request) {
  10. w.Header().Set("Content-Type", "application/json")
  11. var orderId int
  12. err := json.NewDecoder(r.Body).Decode(&orderId)
  13. log.Println("Received Message: ", orderId)
  14. if err != nil {
  15. log.Printf("error parsing checkout input binding payload: %s", err)
  16. w.WriteHeader(http.StatusOK)
  17. return
  18. }
  19. }
  20. func main() {
  21. r := mux.NewRouter()
  22. r.HandleFunc("/checkout", getCheckout).Methods("POST", "OPTIONS")
  23. http.ListenAndServe(":6002", r)
  24. }

Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:

  1. dapr run --app-id checkout --app-port 6002 --dapr-http-port 3602 --dapr-grpc-port 60002 go run CheckoutService.go
  1. //dependencies
  2. import { DaprServer, CommunicationProtocolEnum } from 'dapr-client';
  3. //code
  4. const daprHost = "127.0.0.1";
  5. const serverHost = "127.0.0.1";
  6. const serverPort = "6002";
  7. const daprPort = "3602";
  8. start().catch((e) => {
  9. console.error(e);
  10. process.exit(1);
  11. });
  12. async function start() {
  13. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.HTTP);
  14. await server.binding.receive('checkout', async (orderId) => console.log(`Received Message: ${JSON.stringify(orderId)}`));
  15. await server.startServer();
  16. }

Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:

  1. dapr run --app-id checkout --app-port 6002 --dapr-http-port 3602 --dapr-grpc-port 60002 dotnet npm start

ACK-ing an event

In order to tell Dapr that you successfully processed an event in your application, return a 200 OK response from your HTTP handler.

Rejecting an event

In order to tell Dapr that the event was not processed correctly in your application and schedule it for redelivery, return any response other than 200 OK. For example, a 500 Error.

Specifying a custom route

By default, incoming events will be sent to an HTTP endpoint that corresponds to the name of the input binding. You can override this by setting the following metadata property:

  1. name: mybinding
  2. spec:
  3. type: binding.rabbitmq
  4. metadata:
  5. - name: route
  6. value: /onevent

Event delivery Guarantees

Event delivery guarantees are controlled by the binding implementation. Depending on the binding implementation, the event delivery can be exactly once or at least once.

References

Last modified February 18, 2022: Update setup-jetstream.md (#2200) (428d8c2)