mirror of https://github.com/dapr/docs.git
Added full code snippets of pub sub
Signed-off-by: Amulya Varote <amulyavarote@Amulyas-MacBook-Pro.local>
This commit is contained in:
parent
ec61461b00
commit
e1bbdaa9ed
|
|
@ -262,8 +262,11 @@ Below are code examples that leverage Dapr SDKs to subscribe to a topic.
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Dapr;
|
using Dapr;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
|
||||||
|
|
@ -351,19 +354,25 @@ import io.dapr.client.domain.CloudEvent;
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
=======
|
=======
|
||||||
=======
|
=======
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> 0e83af7a (Added pub sub documentation)
|
>>>>>>> 0e83af7a (Added pub sub documentation)
|
||||||
=======
|
=======
|
||||||
>>>>>>> fbadd23a (Modified based on the review comments - 1)
|
>>>>>>> fbadd23a (Modified based on the review comments - 1)
|
||||||
=======
|
=======
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
|
|
@ -403,6 +412,7 @@ from dapr.ext.grpc import App
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
=======
|
=======
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
|
@ -413,6 +423,10 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
|
|
||||||
#code
|
#code
|
||||||
app = App()
|
app = App()
|
||||||
|
|
@ -648,6 +662,7 @@ Below are code examples that leverage Dapr SDKs to publish a topic.
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
=======
|
=======
|
||||||
using System;
|
using System;
|
||||||
|
|
@ -672,6 +687,16 @@ using Dapr.Client;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Dapr.Client;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Threading;
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
namespace EventService
|
namespace EventService
|
||||||
|
|
@ -680,6 +705,7 @@ namespace EventService
|
||||||
{
|
{
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
string PUBSUB_NAME = "order_pub_sub";
|
string PUBSUB_NAME = "order_pub_sub";
|
||||||
string TOPIC_NAME = "orders";
|
string TOPIC_NAME = "orders";
|
||||||
|
|
@ -699,6 +725,8 @@ namespace EventService
|
||||||
await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken);
|
await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken);
|
||||||
Console.WriteLine("Published data: " + orderId);
|
Console.WriteLine("Published data: " + orderId);
|
||||||
=======
|
=======
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
string PUBSUB_NAME = "order_pub_sub";
|
string PUBSUB_NAME = "order_pub_sub";
|
||||||
string TOPIC_NAME = "orders";
|
string TOPIC_NAME = "orders";
|
||||||
while(true) {
|
while(true) {
|
||||||
|
|
@ -712,7 +740,10 @@ namespace EventService
|
||||||
await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken);
|
await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken);
|
||||||
Console.WriteLine("Published data: " + orderId);
|
Console.WriteLine("Published data: " + orderId);
|
||||||
}
|
}
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -737,23 +768,30 @@ import static java.util.Collections.singletonMap;
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
=======
|
=======
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
>>>>>>> 0e83af7a (Added pub sub documentation)
|
>>>>>>> 0e83af7a (Added pub sub documentation)
|
||||||
=======
|
=======
|
||||||
>>>>>>> fbadd23a (Modified based on the review comments - 1)
|
>>>>>>> fbadd23a (Modified based on the review comments - 1)
|
||||||
=======
|
=======
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class OrderProcessingServiceApplication {
|
public class OrderProcessingServiceApplication {
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
public static void main(String[] args) throws InterruptedException{
|
public static void main(String[] args) throws InterruptedException{
|
||||||
String MESSAGE_TTL_IN_SECONDS = "1000";
|
String MESSAGE_TTL_IN_SECONDS = "1000";
|
||||||
|
|
@ -782,6 +820,30 @@ public class OrderProcessingServiceApplication {
|
||||||
}
|
}
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
|
||||||
|
=======
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException{
|
||||||
|
String MESSAGE_TTL_IN_SECONDS = "1000";
|
||||||
|
String TOPIC_NAME = "orders";
|
||||||
|
String PUBSUB_NAME = "order_pub_sub";
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
TimeUnit.MILLISECONDS.sleep(5000);
|
||||||
|
Random random = new Random();
|
||||||
|
int orderId = random.nextInt(1000-1) + 1;
|
||||||
|
DaprClient client = new DaprClientBuilder().build();
|
||||||
|
//Using Dapr SDK to publish a topic
|
||||||
|
client.publishEvent(
|
||||||
|
PUBSUB_NAME,
|
||||||
|
TOPIC_NAME,
|
||||||
|
orderId,
|
||||||
|
singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS)).block();
|
||||||
|
log.info("Published data:" + orderId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
```
|
```
|
||||||
|
|
||||||
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
||||||
|
|
@ -843,14 +905,23 @@ import json
|
||||||
>>>>>>> 0e83af7a (Added pub sub documentation)
|
>>>>>>> 0e83af7a (Added pub sub documentation)
|
||||||
=======
|
=======
|
||||||
#dependencies
|
#dependencies
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> fbadd23a (Modified based on the review comments - 1)
|
>>>>>>> fbadd23a (Modified based on the review comments - 1)
|
||||||
=======
|
=======
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
|
import json
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
|
|
||||||
#code
|
#code
|
||||||
logging.basicConfig(level = logging.INFO)
|
logging.basicConfig(level = logging.INFO)
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
|
|
||||||
orderId = 100
|
orderId = 100
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
|
@ -869,6 +940,8 @@ logging.info('Published data: ' + str(orderId))
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
|
||||||
=======
|
=======
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
while True:
|
while True:
|
||||||
sleep(random.randrange(50, 5000) / 1000)
|
sleep(random.randrange(50, 5000) / 1000)
|
||||||
orderId = random.randint(1, 1000)
|
orderId = random.randint(1, 1000)
|
||||||
|
|
@ -883,7 +956,10 @@ while True:
|
||||||
data_content_type='application/json',
|
data_content_type='application/json',
|
||||||
)
|
)
|
||||||
logging.info('Published data: ' + str(orderId))
|
logging.info('Published data: ' + str(orderId))
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
```
|
```
|
||||||
|
|
||||||
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
||||||
|
|
@ -938,6 +1014,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
orderId := 100
|
orderId := 100
|
||||||
client, err := dapr.NewClient()
|
client, err := dapr.NewClient()
|
||||||
|
|
@ -975,6 +1052,18 @@ func main() {
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
//Using Dapr SDK to publish a topic
|
//Using Dapr SDK to publish a topic
|
||||||
|
=======
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
time.Sleep(5000)
|
||||||
|
orderId := rand.Intn(1000-1) + 1
|
||||||
|
client, err := dapr.NewClient()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
ctx := context.Background()
|
||||||
|
//Using Dapr SDK to publish a topic
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
if err := client.PublishEvent(ctx, PUBSUB_NAME, TOPIC_NAME, []byte(strconv.Itoa(orderId)));
|
if err := client.PublishEvent(ctx, PUBSUB_NAME, TOPIC_NAME, []byte(strconv.Itoa(orderId)));
|
||||||
err != nil {
|
err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -982,7 +1071,10 @@ func main() {
|
||||||
|
|
||||||
log.Println("Published data: " + strconv.Itoa(orderId))
|
log.Println("Published data: " + strconv.Itoa(orderId))
|
||||||
}
|
}
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
|
=======
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
|
@ -1010,10 +1102,13 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
//dependencies
|
//dependencies
|
||||||
import { DaprServer, DaprClient, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprServer, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
const daprHost = "127.0.0.1";
|
const daprHost = "127.0.0.1";
|
||||||
|
const serverHost = "127.0.0.1";
|
||||||
|
const serverPort = "6002";
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
var main = function() {
|
var main = function() {
|
||||||
|
|
@ -1073,10 +1168,29 @@ async function start(orderId) {
|
||||||
//Using Dapr SDK to publish a topic
|
//Using Dapr SDK to publish a topic
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
=======
|
||||||
|
start().catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
async function start(orderId) {
|
||||||
|
const PUBSUB_NAME = "order_pub_sub"
|
||||||
|
const TOPIC_NAME = "orders"
|
||||||
|
const server = new DaprServer(
|
||||||
|
serverHost,
|
||||||
|
serverPort,
|
||||||
|
daprHost,
|
||||||
|
process.env.DAPR_HTTP_PORT,
|
||||||
|
CommunicationProtocolEnum.HTTP
|
||||||
|
);
|
||||||
|
//Using Dapr SDK to publish a topic
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (orderId) => {
|
await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (orderId) => {
|
||||||
console.log(`Subscriber received: ${JSON.stringify(orderId)}`)
|
console.log(`Subscriber received: ${JSON.stringify(orderId)}`)
|
||||||
});
|
});
|
||||||
await server.startServer();
|
await server.startServer();
|
||||||
|
<<<<<<< HEAD
|
||||||
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
>>>>>>> f30a0acb (Added full code snippets of pub sub)
|
||||||
=======
|
=======
|
||||||
await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, orderId);
|
await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, orderId);
|
||||||
|
|
@ -1102,6 +1216,9 @@ function sleep(ms) {
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
=======
|
||||||
|
}
|
||||||
|
>>>>>>> 8dfbce07 (Added full code snippets of pub sub)
|
||||||
```
|
```
|
||||||
|
|
||||||
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue