Skip to content

Spring Cloud Stream Binder Dapr Design

王琼弟 edited this page Jun 14, 2022 · 2 revisions

Goal

The project is ultimately to implement a "Spring Cloud Stream Binder For Dapr", which is a library for users.

When the project relies on specific message middleware to send and receive messages, users can replace the message middleware with dapr by modifying the pom and configuration files to send and receive messages.

Function

Send and receive messages based on dapr's api.

How to use

  1. Add or update dependency
<dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-cloud-azure-stream-binder-dapr</artifactId>
</dependency>
  1. Update configuration

If the project has previously used message oriented middleware, you only need to remove the middleware configuration and keep the spring cloud stream configuration.

If the project has not used message oriented middleware before, you only need to add the configuration related to messaging in spring cloud stream.

spring:
  cloud:
    stream:
      function:
        definition: consume;supply
      bindings:
        consume-in-0:
          destination: ${AZURE_DAPR_NAME}
        supply-out-0:
          destination: ${AZURE_DAPR_NAME} # same as the above destination
      poller:
        initial-delay: 0
        fixed-delay: 1000
  1. Code
@Bean
public Supplier<Message<String>> supply() {
    return () -> {
        LOGGER.info("Sending message, sequence " + i);
        return MessageBuilder.withPayload("Hello world, " + i++).build();
    };
}


@Bean
public Consumer<Message<String>> consume() {
    return message -> {
        LOGGER.info("New message received: '{}'", message.getPayload());
    };
}

Delivery

  • Github project including source code, readme doc and test
  • Sample project
  • Quick Start and Migrate doc
  • Blogs (optional)

Scope

Stream Feature Support Currently Support
Consumer Groups
Consumer Types
Partitioning Support
Error Handling ☑️
Event Routing ☑️
Health Indicator ☑️
Producing and Consuming Messages ☑️ ☑️

Dependency

We rely on dapr-sdk-autogen but not dapr-sdk, because dapr-sdk will have features that our project doesn't need and may cause dependency conflicts.

image

Structure

image

Implementation

  • Add the required dependencies
    • spring-cloud-stream
    • dapr-sdk-autogen
  • Provide a Binder implementation
    • createProducerMessageHandler(provide a MessageHandler implementation)
    • createConsumerEndpoint(provide a MessageProducer implementation)
  • Create a Binder Configuration(initialize all bean)
  • Define your binder in META-INF/spring.binders
    • dapr=com.azure.spring.cloud.stream.binder.dapr.config.DaprBinderConfiguration
  • Provide a ProvisioningProvider implementation
    • DaprProducerDestination implements ProducerDestination
    • DaprConsumerDestination implements ConsumerDestination
Clone this wiki locally