mirror of https://github.com/dapr/docs.git
Added full code snippets for service invocation
Signed-off-by: Amulya Varote <amulyavarote@Amulyas-MacBook-Pro.local>
This commit is contained in:
parent
2490c8165e
commit
78848ef7bc
|
@ -183,9 +183,15 @@ Below are code examples that leverage Dapr SDKs for service invocation.
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```csharp
|
```csharp
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Dapr.Client;
|
using Dapr.Client;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
namespace EventService
|
namespace EventService
|
||||||
|
@ -194,113 +200,165 @@ namespace EventService
|
||||||
{
|
{
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
int orderId = 100;
|
while(true) {
|
||||||
CancellationTokenSource source = new CancellationTokenSource();
|
System.Threading.Thread.Sleep(5000);
|
||||||
CancellationToken cancellationToken = source.Token;
|
Random random = new Random();
|
||||||
//Using Dapr SDK to invoke a method
|
int orderId = random.Next(1,1000);
|
||||||
using var client = new DaprClientBuilder().Build();
|
CancellationTokenSource source = new CancellationTokenSource();
|
||||||
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
CancellationToken cancellationToken = source.Token;
|
||||||
await client.InvokeMethodAsync(result);
|
using var client = new DaprClientBuilder().Build();
|
||||||
|
//Using Dapr SDK to invoke a method
|
||||||
|
var result = client.CreateInvokeMethodRequest(HttpMethod.Get, "checkout", "checkout/" + orderId, cancellationToken);
|
||||||
|
await client.InvokeMethodAsync(result);
|
||||||
|
Console.WriteLine("Order requested: " + orderId);
|
||||||
|
Console.WriteLine("Result: " + result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```java
|
```java
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
import io.dapr.client.DaprClient;
|
import io.dapr.client.DaprClient;
|
||||||
import io.dapr.client.DaprClientBuilder;
|
import io.dapr.client.DaprClientBuilder;
|
||||||
import io.dapr.client.domain.HttpExtension;
|
import io.dapr.client.domain.HttpExtension;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
//code
|
//code
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class OrderProcessingServiceApplication {
|
public class OrderProcessingServiceApplication {
|
||||||
public static void main(String[] args) throws InterruptedException {
|
|
||||||
int orderId = 100;
|
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
|
||||||
//Using Dapr SDK to invoke a method
|
|
||||||
DaprClient client = new DaprClientBuilder().build();
|
public static void main(String[] args) throws InterruptedException{
|
||||||
var result = client.invokeMethod(
|
while(true) {
|
||||||
"checkout",
|
TimeUnit.MILLISECONDS.sleep(5000);
|
||||||
"checkout/" + orderId,
|
Random random = new Random();
|
||||||
null,
|
int orderId = random.nextInt(1000-1) + 1;
|
||||||
HttpExtension.GET,
|
DaprClient daprClient = new DaprClientBuilder().build();
|
||||||
String.class
|
//Using Dapr SDK to invoke a method
|
||||||
);
|
var result = daprClient.invokeMethod(
|
||||||
|
"checkout",
|
||||||
|
"checkout/" + orderId,
|
||||||
|
null,
|
||||||
|
HttpExtension.GET,
|
||||||
|
String.class
|
||||||
|
);
|
||||||
|
log.info("Order requested: " + orderId);
|
||||||
|
log.info("Result: " + result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```python
|
```python
|
||||||
|
|
||||||
#dependencies
|
#dependencies
|
||||||
|
import random
|
||||||
|
from time import sleep
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
from dapr.clients import DaprClient
|
from dapr.clients import DaprClient
|
||||||
|
|
||||||
#code
|
#code
|
||||||
orderId = 100
|
logging.basicConfig(level = logging.INFO)
|
||||||
#Using Dapr SDK to invoke a method
|
while True:
|
||||||
with DaprClient() as client:
|
sleep(random.randrange(50, 5000) / 1000)
|
||||||
result = client.invoke_method(
|
orderId = random.randint(1, 1000)
|
||||||
"checkout",
|
with DaprClient() as daprClient:
|
||||||
f"checkout/{orderId}",
|
#Using Dapr SDK to invoke a method
|
||||||
data=b'',
|
result = daprClient.invoke_method(
|
||||||
http_verb="GET"
|
"checkout",
|
||||||
)
|
f"checkout/{orderId}",
|
||||||
|
data=b'',
|
||||||
|
http_verb="GET"
|
||||||
|
)
|
||||||
|
logging.basicConfig(level = logging.INFO)
|
||||||
|
logging.info('Order requested: ' + str(orderId))
|
||||||
|
logging.info('Result: ' + str(result))
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```go
|
```go
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
"strconv"
|
"strconv"
|
||||||
dapr "github.com/dapr/go-sdk/client"
|
dapr "github.com/dapr/go-sdk/client"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//code
|
//code
|
||||||
func main() {
|
type Order struct {
|
||||||
orderId := 100
|
orderName string
|
||||||
//Using Dapr SDK to invoke a method
|
orderNum string
|
||||||
client, err := dapr.NewClient()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer client.Close()
|
|
||||||
ctx := context.Background()
|
|
||||||
result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(orderId), "get")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
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 invoke a method
|
||||||
|
result, err := client.InvokeMethod(ctx, "checkout", "checkout/" + strconv.Itoa(orderId), "get")
|
||||||
|
log.Println("Order requested: " + strconv.Itoa(orderId))
|
||||||
|
log.Println("Result: ")
|
||||||
|
log.Println(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```javascript
|
```javascript
|
||||||
|
|
||||||
//dependencies
|
//dependencies
|
||||||
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
|
||||||
|
|
||||||
//code
|
//code
|
||||||
const daprHost = "127.0.0.1";
|
const daprHost = "127.0.0.1";
|
||||||
|
|
||||||
var main = function() {
|
var main = function() {
|
||||||
var orderId = 100;
|
for(var i=0;i<10;i++) {
|
||||||
//Using Dapr SDK to invoke a method
|
sleep(5000);
|
||||||
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
|
||||||
const result = await client.invoker.invoke('checkout' , "checkout/" + orderId , HttpMethod.GET);
|
start(orderId).catch((e) => {
|
||||||
|
console.error(e);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function start(orderId) {
|
||||||
|
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
|
||||||
|
//Using Dapr SDK to invoke a method
|
||||||
|
const result = await client.invoker.invoke('checkoutservice' , "checkout/" + orderId , HttpMethod.GET);
|
||||||
|
console.log("Order requested: " + orderId);
|
||||||
|
console.log("Result: " + result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue