How-To: Trigger your application with input bindings

With input bindings, you can trigger your application when an event from an external resource occurs. An external resource could be a queue, messaging pipeline, cloud-service, filesystem, etc. An optional payload and metadata may be sent with the request.

Input bindings are ideal for event-driven processing, data pipelines, or generally reacting to events and performing further processing. Dapr input 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

This guide uses a Kafka binding as an example. You can find your preferred binding spec from . In this guide:

  1. The example invokes the endpoint with checkout, the name of the binding to invoke.
  2. The payload goes inside the mandatory data field, and can be any JSON serializable value.
  3. The operation field tells the binding what action it needs to take. For example, the Kafka binding supports the create operation.
    • You can check .

Note

If you haven’t already, for a quick walk-through on how to use the bindings API.

Create a new binding component named checkout. Within the metadata section, configure the following Kafka-related properties:

  • The topic to which you’ll publish the message
  • The broker

  • Kubernetes

Use the --resources-path flag with the dapr run command to point to your custom resources directory.

To deploy into a Kubernetes cluster, 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. - name: consumerGroup
  15. value: group1
  16. # publisher configuration: topic
  17. - name: publishTopic
  18. - name: authRequired
  19. value: "false"
  • Listen on a POST endpoint with the name of the binding, as specified in metadata.name in the binding.yaml file.
  • Verify your application allows Dapr to make an OPTIONS request for this endpoint.

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

  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. }
  1. //dependencies
  2. "encoding/json"
  3. "log"
  4. "net/http"
  5. )
  6. //code
  7. func getCheckout(w http.ResponseWriter, r *http.Request) {
  8. w.Header().Set("Content-Type", "application/json")
  9. var orderId int
  10. err := json.NewDecoder(r.Body).Decode(&orderId)
  11. log.Println("Received Message: ", orderId)
  12. if err != nil {
  13. log.Printf("error parsing checkout input binding payload: %s", err)
  14. w.WriteHeader(http.StatusOK)
  15. return
  16. }
  17. }
  18. func main() {
  19. r := mux.NewRouter()
  20. r.HandleFunc("/checkout", getCheckout).Methods("POST", "OPTIONS")
  21. http.ListenAndServe(":6002", r)
  22. }

Tell Dapr you’ve successfully processed an event in your application by returning a 200 OK response from your HTTP handler.

Tell Dapr the event was not processed correctly in your application and schedule it for redelivery by returning any response other than 200 OK. For example, a 500 Error.

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 in binding.yaml:

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

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.