Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added service invocation examples #536

Merged
merged 5 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions service_invocation/csharp/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Service Invocation

In this quickstart, you'll create a checkout service and an order processor service to demonstrate how to use the service invocation API. The checkout service uses Dapr's http proxying capability to invoke a method on the order processing service.

Visit [this](https://docs.dapr.io/developing-applications/building-blocks/service-invocation/) link for more information about Dapr and service invocation.

This quickstart includes one checkout service:

- Dotnet client service `checkout`

And one order processor service:

- Dotnet order-processor service `order-processor`

### Run Dotnet checkout with Dapr

1. Open a new terminal window and navigate to the `checkout` directory:

```bash
cd checkout
```

2. Install dependencies:

<!-- STEP
name: Install Dotnet dependencies
working_dir: ./checkout
-->

```bash
dotnet restore
dotnet build
```

3. Run the Dotnet checkout app with Dapr:

```bash
dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- dotnet run
```

<!-- END_STEP -->
### Run Dotnet order-processor with Dapr

1. Open a new terminal window and navigate to `order-processor` directory:

```bash
cd order-processor
```

2. Install dependencies:

<!-- STEP
name: Install Dotnet dependencies
working_dir: ./order-processor
-->

```bash
dotnet restore
dotnet build
```

3. Run the Dotnet order-processor app with Dapr:

```bash
dapr run --app-port 6001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- dotnet run
```

<!-- END_STEP -->
25 changes: 25 additions & 0 deletions service_invocation/csharp/http/checkout.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32112.339
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "checkout", "checkout\checkout.csproj", "{636C3192-12CE-4884-AC45-B7C8A276075A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{636C3192-12CE-4884-AC45-B7C8A276075A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{636C3192-12CE-4884-AC45-B7C8A276075A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{636C3192-12CE-4884-AC45-B7C8A276075A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{636C3192-12CE-4884-AC45-B7C8A276075A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49215BFE-48D9-44C4-A8F6-374E24D59DED}
EndGlobalSection
EndGlobal
24 changes: 24 additions & 0 deletions service_invocation/csharp/http/checkout/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

var baseURL = (Environment.GetEnvironmentVariable("BASE_URL") ?? "http://localhost") + ":" + (Environment.GetEnvironmentVariable("DAPR_HTTP_PORT") ?? "3500");

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
// Adding app id as part of the header
client.DefaultRequestHeaders.Add("dapr-app-id", "order-processor");

for (int i = 1; i <= 10; i++) {
var order = new Order(i);
var orderJson = JsonSerializer.Serialize<Order>(order);
var content = new StringContent(orderJson, Encoding.UTF8, "application/json");

// Invoking a service
var response = await client.PostAsync($"{baseURL}/orders", content);
Console.WriteLine("Order passed: " + order);

await Task.Delay(TimeSpan.FromSeconds(1));
}

public record Order([property: JsonPropertyName("orderId")] int OrderId);
10 changes: 10 additions & 0 deletions service_invocation/csharp/http/checkout/checkout.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
17 changes: 17 additions & 0 deletions service_invocation/csharp/http/order-processor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

if (app.Environment.IsDevelopment()) {app.UseDeveloperExceptionPage();}

app.MapPost("/orders", async context => {
var data = await context.Request.ReadFromJsonAsync<Order>();
Console.WriteLine("Order received : " + data);
await context.Response.WriteAsync(data.ToString());
});

await app.RunAsync();

public record Order([property: JsonPropertyName("orderId")] int orderId);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"CheckoutService": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:7001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
10 changes: 10 additions & 0 deletions service_invocation/csharp/http/order-processor/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions service_invocation/go/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Service Invocation

In this quickstart, you'll create a checkout service and an order processor service to demonstrate how to use the service invocation API. The checkout service uses Dapr's http proxying capability to invoke a method on the order processing service.

Visit [this](https://docs.dapr.io/developing-applications/building-blocks/service-invocation/) link for more information about Dapr and service invocation.

This quickstart includes one checkout service:

- Go client service `checkout`

And one order processor service:

- Go order-processor service `order-processor`

### Run Go checkout with Dapr

1. Open a new terminal window and navigate to `checkout` directory:

```bash
cd checkout
```

2. Install dependencies:

<!-- STEP
name: Install Go dependencies
working_dir: ./checkout
-->

```bash
go build app.go
```

3. Run the Go checkout app with Dapr:

```bash
dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- go run app.go
```

<!-- END_STEP -->
### Run Go order-processor with Dapr

1. Open a new terminal window and navigate to `order-processor` directory:

```bash
cd order-processor
```

2. Install dependencies:

<!-- STEP
name: Install Go dependencies
working_dir: ./order-processor
-->

```bash
go build app.go
```

3. Run the Go order-processor app with Dapr:

```bash
dapr run --app-port 6001 --app-id order-processor --app-protocol http --dapr-http-port 3501 -- go run app.go
```

<!-- END_STEP -->
48 changes: 48 additions & 0 deletions service_invocation/go/http/checkout/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)

func main() {
var DAPR_HOST, DAPR_HTTP_PORT string
var okHost, okPort bool
if DAPR_HOST, okHost = os.LookupEnv("DAPR_HOST"); !okHost {
DAPR_HOST = "http://localhost"
}
if DAPR_HTTP_PORT, okPort = os.LookupEnv("DAPR_HTTP_PORT"); !okPort {
DAPR_HTTP_PORT = "3500"
}
for i := 1; i <= 10; i++ {
order := "{\"orderId\":" + strconv.Itoa(i) + "}"
client := &http.Client{}
req, err := http.NewRequest("POST", DAPR_HOST+":"+DAPR_HTTP_PORT+"/orders", strings.NewReader(order))
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
// Adding app id as part of th header
req.Header.Add("dapr-app-id", "order-processor")

// Invoking a service
response, err := client.Do(req)

if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}

result, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}

log.Println("Order passed: ", string(result))
}
}
16 changes: 16 additions & 0 deletions service_invocation/go/http/checkout/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module dapr_example

go 1.17

require (
github.com/dapr/go-sdk v1.2.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
golang.org/x/sys v0.0.0-20201202213521-69691e467435 // indirect
golang.org/x/text v0.3.4 // indirect
google.golang.org/genproto v0.0.0-20201204160425-06b3db808446 // indirect
google.golang.org/grpc v1.34.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)
Loading