update examples for pub sub

Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com>
This commit is contained in:
Hannah Hunter 2024-10-25 14:12:10 -04:00
parent 730a9963f8
commit 225736deee
1 changed files with 15 additions and 14 deletions

View File

@ -37,7 +37,7 @@ metadata:
spec: spec:
topic: orders topic: orders
routes: routes:
default: /checkout default: /orders
pubsubname: pubsub pubsubname: pubsub
scopes: scopes:
- orderprocessing - orderprocessing
@ -46,7 +46,7 @@ scopes:
Here the subscription called `order`: Here the subscription called `order`:
- Uses the pub/sub component called `pubsub` to subscribes to the topic called `orders`. - Uses the pub/sub component called `pubsub` to subscribes to the topic called `orders`.
- Sets the `route` field to send all topic messages to the `/checkout` endpoint in the app. - Sets the `route` field to send all topic messages to the `/orders` endpoint in the app.
- Sets `scopes` field to scope this subscription for access only by apps with IDs `orderprocessing` and `checkout`. - Sets `scopes` field to scope this subscription for access only by apps with IDs `orderprocessing` and `checkout`.
When running Dapr, set the YAML component file path to point Dapr to the component. When running Dapr, set the YAML component file path to point Dapr to the component.
@ -113,7 +113,7 @@ In your application code, subscribe to the topic specified in the Dapr pub/sub c
```csharp ```csharp
//Subscribe to a topic //Subscribe to a topic
[HttpPost("checkout")] [HttpPost("orders")]
public void getCheckout([FromBody] int orderId) public void getCheckout([FromBody] int orderId)
{ {
Console.WriteLine("Subscriber received : " + orderId); Console.WriteLine("Subscriber received : " + orderId);
@ -128,7 +128,7 @@ public void getCheckout([FromBody] int orderId)
import io.dapr.client.domain.CloudEvent; import io.dapr.client.domain.CloudEvent;
//Subscribe to a topic //Subscribe to a topic
@PostMapping(path = "/checkout") @PostMapping(path = "/orders")
public Mono<Void> getCheckout(@RequestBody(required = false) CloudEvent<String> cloudEvent) { public Mono<Void> getCheckout(@RequestBody(required = false) CloudEvent<String> cloudEvent) {
return Mono.fromRunnable(() -> { return Mono.fromRunnable(() -> {
try { try {
@ -146,7 +146,7 @@ public Mono<Void> getCheckout(@RequestBody(required = false) CloudEvent<String>
from cloudevents.sdk.event import v1 from cloudevents.sdk.event import v1
#Subscribe to a topic #Subscribe to a topic
@app.route('/checkout', methods=['POST']) @app.route('/orders', methods=['POST'])
def checkout(event: v1.Event) -> None: def checkout(event: v1.Event) -> None:
data = json.loads(event.Data()) data = json.loads(event.Data())
logging.info('Subscriber received: ' + str(data)) logging.info('Subscriber received: ' + str(data))
@ -163,7 +163,7 @@ const app = express()
app.use(bodyParser.json({ type: 'application/*+json' })); app.use(bodyParser.json({ type: 'application/*+json' }));
// listen to the declarative route // listen to the declarative route
app.post('/checkout', (req, res) => { app.post('/orders', (req, res) => {
console.log(req.body); console.log(req.body);
res.sendStatus(200); res.sendStatus(200);
}); });
@ -178,7 +178,7 @@ app.post('/checkout', (req, res) => {
var sub = &common.Subscription{ var sub = &common.Subscription{
PubsubName: "pubsub", PubsubName: "pubsub",
Topic: "orders", Topic: "orders",
Route: "/checkout", Route: "/orders",
} }
func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) { func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
@ -191,7 +191,7 @@ func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err er
{{< /tabs >}} {{< /tabs >}}
The `/checkout` endpoint matches the `route` defined in the subscriptions and this is where Dapr sends all topic messages to. The `/orders` endpoint matches the `route` defined in the subscriptions and this is where Dapr sends all topic messages to.
### Streaming subscriptions ### Streaming subscriptions
@ -325,7 +325,7 @@ In the example below, you define the values found in the [declarative YAML subsc
```csharp ```csharp
[Topic("pubsub", "orders")] [Topic("pubsub", "orders")]
[HttpPost("/checkout")] [HttpPost("/orders")]
public async Task<ActionResult<Order>>Checkout(Order order, [FromServices] DaprClient daprClient) public async Task<ActionResult<Order>>Checkout(Order order, [FromServices] DaprClient daprClient)
{ {
// Logic // Logic
@ -337,7 +337,7 @@ or
```csharp ```csharp
// Dapr subscription in [Topic] routes orders topic to this route // Dapr subscription in [Topic] routes orders topic to this route
app.MapPost("/checkout", [Topic("pubsub", "orders")] (Order order) => { app.MapPost("/orders", [Topic("pubsub", "orders")] (Order order) => {
Console.WriteLine("Subscriber received : " + order); Console.WriteLine("Subscriber received : " + order);
return Results.Ok(order); return Results.Ok(order);
}); });
@ -359,7 +359,7 @@ app.UseEndpoints(endpoints =>
```java ```java
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Topic(name = "checkout", pubsubName = "pubsub") @Topic(name = "orders", pubsubName = "pubsub")
@PostMapping(path = "/orders") @PostMapping(path = "/orders")
public Mono<Void> handleMessage(@RequestBody(required = false) CloudEvent<String> cloudEvent) { public Mono<Void> handleMessage(@RequestBody(required = false) CloudEvent<String> cloudEvent) {
return Mono.fromRunnable(() -> { return Mono.fromRunnable(() -> {
@ -370,6 +370,7 @@ public Mono<Void> handleMessage(@RequestBody(required = false) CloudEvent<String
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}); });
}
``` ```
{{% /codetab %}} {{% /codetab %}}
@ -382,7 +383,7 @@ def subscribe():
subscriptions = [ subscriptions = [
{ {
'pubsubname': 'pubsub', 'pubsubname': 'pubsub',
'topic': 'checkout', 'topic': 'orders',
'routes': { 'routes': {
'rules': [ 'rules': [
{ {
@ -418,7 +419,7 @@ app.get('/dapr/subscribe', (req, res) => {
res.json([ res.json([
{ {
pubsubname: "pubsub", pubsubname: "pubsub",
topic: "checkout", topic: "orders",
routes: { routes: {
rules: [ rules: [
{ {
@ -480,7 +481,7 @@ func configureSubscribeHandler(w http.ResponseWriter, _ *http.Request) {
t := []subscription{ t := []subscription{
{ {
PubsubName: "pubsub", PubsubName: "pubsub",
Topic: "checkout", Topic: "orders",
Routes: routes{ Routes: routes{
Rules: []rule{ Rules: []rule{
{ {