Giter Club home page Giter Club logo

dapr-pub-sub-routing-problem's Introduction

Dapr pub sub testing...

Dapr run

dapr run --app-id testapp --dapr-http-port 62329 --app-port 7040 --components-path ./dapr/components --config ./dapr/config/config.yaml -- dotnet run -p ./WebApplication/WebApplication.csproj --urls http://localhost:7040

Subscribe endpoint

http://localhost:7040/dapr/subscribe

[
    {
        "topic": "topic1",
        "pubsubName": "pubsub",
        "routes": {
            "default": "default-route",
            "rules": [
                {
                    "match": "event.type == \"event.v1\"",
                    "path": "matched-route"
                }
            ]
        }
    }
]

Publish message with type event.v1 will be routed to matched route

curl --request POST 'http://localhost:62329/v1.0/publish/pubsub/topic1' \
--header 'Content-Type: application/cloudevents+json' \
--data-raw '{
"type": "event.v1",
"source": "demo",
"data" : {"hello": "world", "foo":"bar"}
}'

Subscriber

== APP == MATCHED HANDLER INCOMING MESSAGE: {"hello":"world","foo":"bar"}

Publish message with random type will be routed to default route

curl --request POST 'http://localhost:62329/v1.0/publish/pubsub/topic1' \
--header 'Content-Type: application/cloudevents+json' \
--data-raw '{
"type": "xyz",
"source": "demo",
"data" : {"hello": "world", "foo":"bar"}
}'

Subscriber

== APP == DEFAULT HANDLER INCOMING MESSAGE: {"hello":"world","foo":"bar"}

Code change

Enable raw payload for at least one endpoint

endpoints.MapPost("matched-route", RouteMatchedHandler).WithMetadata(
    new TopicAttribute(
        "pubsub", "topic1", true,  "event.type == \"event.v1\"", 1
    )
);
endpoints.MapPost("default-route", RouteDefaultHandler).WithMetadata(
    new TopicAttribute(
        "pubsub", "topic1", true
    )
);

Subscribe endpoint

http://localhost:7040/dapr/subscribe

[
    {
        "topic": "topic1",
        "pubsubName": "pubsub",
        "routes": {
            "default": "default-route",
            "rules": [
                {
                    "match": "event.type == \"event.v1\"",
                    "path": "matched-route"
                }
            ]
        },
        "metadata": {
            "rawPayload": "true"
        }
    }
]

Failure: Publish message with type event.v1 will be routed to Default route

curl --request POST 'http://localhost:62329/v1.0/publish/pubsub/topic1' \
--header 'Content-Type: application/cloudevents+json' \
--data-raw '{
"type": "event.v1",
"source": "demo",
"data" : {"hello": "world", "foo":"bar"}
}'

Subscriber

== APP == DEFAULT HANDLER INCOMING MESSAGE: {"data":{"hello":"world","foo":"bar"},"traceid":"00-ebcfdc8cb05606bec3ce580bb42a7c9a-b96d6a31865045c1-01","topic":"topic1","pubsubname":"pubsub","specversion":"1.0","type":"event.v1","source":"demo"}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.