-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Pub/sub] Overview & how-tos (#2414)
* overview; parse out howto-publish-subscribe Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * updates after talk w Mark Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * break up cloudevents into two articles Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * update to overview Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * how to and overview updates Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * grammar pass before opening for review Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * updates from Mark Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> * updated diagram Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com> Co-authored-by: Mark Fussell <markfussell@gmail.com>
- Loading branch information
1 parent
63b6b3f
commit 92e1b10
Showing
10 changed files
with
769 additions
and
389 deletions.
There are no files selected for viewing
247 changes: 96 additions & 151 deletions
247
...nt/en/developing-applications/building-blocks/pubsub/howto-publish-subscribe.md
Large diffs are not rendered by default.
Oops, something went wrong.
265 changes: 113 additions & 152 deletions
265
...ntent/en/developing-applications/building-blocks/pubsub/howto-route-messages.md
Large diffs are not rendered by default.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
...content/en/developing-applications/building-blocks/pubsub/pubsub-cloudevents.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
type: docs | ||
title: "Publishing & subscribing messages with Cloudevents" | ||
linkTitle: "Messages with Cloudevents" | ||
weight: 2100 | ||
description: "Learn why Dapr uses CloudEvents, how they work in Dapr pub/sub, and how to create CloudEvents." | ||
--- | ||
|
||
To enable message routing and provide additional context with each message, Dapr uses the [CloudEvents 1.0 specification](https://github.com/cloudevents/spec/tree/v1.0) as its message format. Any message sent by an application to a topic using Dapr is automatically wrapped in a CloudEvents envelope, using the [`Content-Type` header value]({{< ref "pubsub-overview.md#content-types" >}}) for `datacontenttype` attribute. | ||
|
||
Dapr uses CloudEvents to provide additional context to the event payload, enabling features like: | ||
|
||
- Tracing | ||
- Deduplication by message Id | ||
- Content-type for proper deserialization of event data | ||
|
||
## CloudEvents example | ||
|
||
Dapr implements the following CloudEvents fields when creating a message topic. | ||
|
||
- `id` | ||
- `source` | ||
- `specversion` | ||
- `type` | ||
- `traceparent` | ||
- `datacontenttype` (optional) | ||
|
||
The following example demonstrates an `orders` topic message sent by Dapr that includes a W3C `traceid` unique to the message, the `data` and the fields for the CloudEvent where the data content is serialized as JSON. | ||
|
||
```json | ||
{ | ||
"topic": "orders", | ||
"pubsubname": "order_pub_sub", | ||
"traceid": "00-113ad9c4e42b27583ae98ba698d54255-e3743e35ff56f219-01", | ||
"tracestate": "", | ||
"data": { | ||
"orderId": 1 | ||
}, | ||
"id": "5929aaac-a5e2-4ca1-859c-edfe73f11565", | ||
"specversion": "1.0", | ||
"datacontenttype": "application/json; charset=utf-8", | ||
"source": "checkout", | ||
"type": "com.dapr.event.sent", | ||
"traceparent": "00-113ad9c4e42b27583ae98ba698d54255-e3743e35ff56f219-01" | ||
} | ||
``` | ||
|
||
As another example of a v1.0 CloudEvent, the following shows data as XML content in a CloudEvent message serialized as JSON: | ||
|
||
```json | ||
{ | ||
"specversion" : "1.0", | ||
"type" : "xml.message", | ||
"source" : "https://example.com/message", | ||
"subject" : "Test XML Message", | ||
"id" : "id-1234-5678-9101", | ||
"time" : "2020-09-23T06:23:21Z", | ||
"datacontenttype" : "text/xml", | ||
"data" : "<note><to>User1</to><from>user2</from><message>hi</message></note>" | ||
} | ||
``` | ||
|
||
## Publish your own CloudEvent | ||
|
||
If you want to use your own CloudEvent, make sure to specify the [`datacontenttype`]({{< ref "pubsub-overview.md#setting-message-content-types" >}}) as `application/cloudevents+json`. | ||
|
||
### Example | ||
|
||
{{< tabs "Dapr CLI" "HTTP API (Bash)" "HTTP API (PowerShell)">}} | ||
|
||
{{% codetab %}} | ||
|
||
Publish a CloudEvent to the `orders` topic: | ||
|
||
```bash | ||
dapr publish --publish-app-id orderprocessing --pubsub order-pub-sub --topic orders --data '{"specversion" : "1.0", "type" : "com.dapr.cloudevent.sent", "source" : "testcloudeventspubsub", "subject" : "Cloud Events Test", "id" : "someCloudEventId", "time" : "2021-08-02T09:00:00Z", "datacontenttype" : "application/cloudevents+json", "data" : {"orderId": "100"}}' | ||
``` | ||
|
||
{{% /codetab %}} | ||
|
||
{{% codetab %}} | ||
|
||
Publish a CloudEvent to the `orders` topic: | ||
|
||
```bash | ||
curl -X POST http://localhost:3601/v1.0/publish/order-pub-sub/orders -H "Content-Type: application/cloudevents+json" -d '{"specversion" : "1.0", "type" : "com.dapr.cloudevent.sent", "source" : "testcloudeventspubsub", "subject" : "Cloud Events Test", "id" : "someCloudEventId", "time" : "2021-08-02T09:00:00Z", "datacontenttype" : "application/cloudevents+json", "data" : {"orderId": "100"}}' | ||
``` | ||
|
||
{{% /codetab %}} | ||
|
||
{{% codetab %}} | ||
|
||
Publish a CloudEvent to the `orders` topic: | ||
|
||
```powershell | ||
Invoke-RestMethod -Method Post -ContentType 'application/cloudevents+json' -Body '{"specversion" : "1.0", "type" : "com.dapr.cloudevent.sent", "source" : "testcloudeventspubsub", "subject" : "Cloud Events Test", "id" : "someCloudEventId", "time" : "2021-08-02T09:00:00Z", "datacontenttype" : "application/cloudevents+json", "data" : {"orderId": "100"}}' -Uri 'http://localhost:3601/v1.0/publish/order-pub-sub/orders' | ||
``` | ||
|
||
{{% /codetab %}} | ||
|
||
{{< /tabs >}} | ||
|
||
## Next steps | ||
|
||
- Learn why you might [not want to use CloudEvents]({{< ref pubsub-raw.md >}}) | ||
- Try out the [pub/sub Quickstart]({{< ref pubsub-quickstart.md >}}) | ||
- List of [pub/sub components]({{< ref setup-pubsub >}}) | ||
- Read the [API reference]({{< ref pubsub_api.md >}}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.