-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added service invocation examples (#536)
* Added service invocation examples Signed-off-by: Amulya Varote <amulyavarote@QTM-SWATHIKIL-1.redmond.corp.microsoft.com> * Modified package files Signed-off-by: Amulya Varote <amulyavarote@QTM-SWATHIKIL-1.redmond.corp.microsoft.com> * Changes based on the review comments Signed-off-by: Amulya Varote <amulyavarote@QTM-SWATHIKIL-1.redmond.corp.microsoft.com> * Changes based on the review comments Signed-off-by: Amulya Varote <amulyavarote@QTM-SWATHIKIL-1.redmond.corp.microsoft.com> * Readme changes based on the review comments Signed-off-by: Amulya Varote <amulyavarote@QTM-SWATHIKIL-1.redmond.corp.microsoft.com>
- Loading branch information
1 parent
3158377
commit cc8aa93
Showing
26 changed files
with
849 additions
and
0 deletions.
There are no files selected for viewing
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,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 --> |
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,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 |
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,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); |
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,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,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); |
15 changes: 15 additions & 0 deletions
15
service_invocation/csharp/http/order-processor/Properties/launchSettings.json
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,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" | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
service_invocation/csharp/http/order-processor/appsettings.Development.json
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,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
service_invocation/csharp/http/order-processor/appsettings.json
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,10 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
14 changes: 14 additions & 0 deletions
14
service_invocation/csharp/http/order-processor/order-processor.csproj
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,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> |
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,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 --> |
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,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)) | ||
} | ||
} |
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,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 | ||
) |
Oops, something went wrong.