How-To: Trigger your application with input bindings
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 generally reacting to events and performing 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
An input binding represents a resource that Dapr uses to read events from and push to your application. Read the bindings overview for more information.
Create a new binding component with the name of .
Inside the metadata
section, configure Kafka-related properties, such as the topic to publish the message to and the broker.
Create the following binding.yaml
file and save it to a components
sub-folder in your application directory.
Use the --components-path
flag with the dapr run
command to point to your custom components directory.
To deploy into a Kubernetes cluster, run kubectl apply -f binding.yaml
.
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: checkout
spec:
type: bindings.kafka
version: v1
metadata:
# Kafka broker connection setting
- name: brokers
value: localhost:9092
# consumer configuration: topic and consumer group
- name: topics
value: sample
- name: consumerGroup
value: group1
# publisher configuration: topic
value: sample
- name: authRequired
value: "false"
Below are code examples that leverage Dapr SDKs to demonstrate an output binding.
//dependencies
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
//code
@RestController
@RequestMapping("/")
public class CheckoutServiceController {
private static final Logger log = LoggerFactory.getLogger(CheckoutServiceController.class);
@PostMapping(path = "/checkout")
public Mono<String> getCheckout(@RequestBody(required = false) byte[] body) {
return Mono.fromRunnable(() ->
log.info("Received Message: " + new String(body)));
}
}
//dependencies
import (
"encoding/json"
"log"
"github.com/gorilla/mux"
)
func getCheckout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var orderId int
err := json.NewDecoder(r.Body).Decode(&orderId)
log.Println("Received Message: ", orderId)
if err != nil {
log.Printf("error parsing checkout input binding payload: %s", err)
w.WriteHeader(http.StatusOK)
return
}
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/checkout", getCheckout).Methods("POST", "OPTIONS")
http.ListenAndServe(":6002", r)
}
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
:
name: mybinding
spec:
type: binding.rabbitmq
metadata:
- name: route
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.