mirror of https://github.com/dapr/quickstarts.git
Service invocation example using Java and HTTP (#577)
* Service invocation example using HTTP Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com> * fixing review feedback Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>
This commit is contained in:
parent
ce9d7325c5
commit
70ba067fca
|
@ -0,0 +1,39 @@
|
|||
## Maven
|
||||
target/
|
||||
release.properties
|
||||
.mvn
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
# IDE generated files and directories
|
||||
*.iml
|
||||
.idea/
|
||||
*.ipr
|
||||
*.iws
|
||||
.vs/
|
||||
.vscode/
|
||||
.code-workspace
|
||||
.settings/
|
||||
.metadata
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
# macOS
|
||||
.DS_Store
|
|
@ -0,0 +1,96 @@
|
|||
# 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:
|
||||
|
||||
- Java client service `checkout`
|
||||
|
||||
And one order processor service:
|
||||
|
||||
- Java order-processor service `order-processor`
|
||||
|
||||
## Pre-requisites
|
||||
|
||||
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/).
|
||||
* Java JDK 11 (or greater): [Oracle JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) or [OpenJDK](https://jdk.java.net/11/).
|
||||
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.
|
||||
|
||||
### Run Java checkout service with Dapr
|
||||
|
||||
1. Open a new terminal window and navigate to `checkout` directory:
|
||||
|
||||
```bash
|
||||
cd checkout
|
||||
```
|
||||
|
||||
2. Install dependencies:
|
||||
|
||||
<!-- STEP
|
||||
name: Install maven dependencies
|
||||
working_dir: ./checkout
|
||||
-->
|
||||
|
||||
```bash
|
||||
mvn clean install
|
||||
```
|
||||
|
||||
3. Run the Java checkout app with Dapr:
|
||||
|
||||
<!-- STEP
|
||||
name: Run Java checkout service
|
||||
expected_stdout_lines:
|
||||
- "== APP == Order passed: 1"
|
||||
- "== APP == Order passed: 2"
|
||||
expected_stderr_lines:
|
||||
output_match_mode: substring
|
||||
background: true
|
||||
sleep: 10
|
||||
-->
|
||||
|
||||
```bash
|
||||
dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 -- java -jar target/CheckoutService-0.0.1-SNAPSHOT.jar
|
||||
```
|
||||
|
||||
<!-- END_STEP -->
|
||||
|
||||
### Run Java 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 maven dependencies
|
||||
working_dir: ./order-processor
|
||||
-->
|
||||
|
||||
```bash
|
||||
mvn clean install
|
||||
```
|
||||
|
||||
3. Run the Java order-processor app with Dapr:
|
||||
|
||||
<!-- STEP
|
||||
name: Run Java checkout service
|
||||
expected_stdout_lines:
|
||||
- "== APP == Order received: 1"
|
||||
- "== APP == Order received: 2"
|
||||
expected_stderr_lines:
|
||||
output_match_mode: substring
|
||||
background: true
|
||||
sleep: 10
|
||||
-->
|
||||
|
||||
```bash
|
||||
dapr run --app-id order-processor --app-port 6001 --app-protocol http --dapr-http-port 3501 -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
|
||||
```
|
||||
|
||||
<!-- END_STEP -->
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.service</groupId>
|
||||
<artifactId>CheckoutService</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<description>Demo for Dapr service invocation component</description>
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<slf4jVersion>1.6.1</slf4jVersion>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20211205</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>
|
||||
com.service.CheckoutServiceApplication
|
||||
</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,40 @@
|
|||
package com.service;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CheckoutServiceApplication {
|
||||
private static final HttpClient httpClient = HttpClient.newBuilder()
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
|
||||
private static final String DAPR_HTTP_PORT = System.getenv().getOrDefault("DAPR_HTTP_PORT", "3500");
|
||||
|
||||
public static void main(String[] args) throws InterruptedException, IOException {
|
||||
String dapr_url = "http://localhost:"+DAPR_HTTP_PORT+"/orders";
|
||||
for (int i=1; i<=10; i++) {
|
||||
int orderId = i;
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("orderId", orderId);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.POST(HttpRequest.BodyPublishers.ofString(obj.toString()))
|
||||
.uri(URI.create(dapr_url))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("dapr-app-id", "order-processor")
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
System.out.println("Order passed: "+ orderId);
|
||||
TimeUnit.MILLISECONDS.sleep(1000);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.6.3</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.service</groupId>
|
||||
<artifactId>OrderProcessingService</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>OrderProcessingService</name>
|
||||
<description>Demo for Dapr service invocation component</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,11 @@
|
|||
package com.service;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class OrderProcessingServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OrderProcessingServiceApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.service.controller;
|
||||
|
||||
import com.service.model.Order;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OrderProcessingServiceController {
|
||||
@PostMapping(path = "/orders", consumes = MediaType.ALL_VALUE)
|
||||
public String processOrders(@RequestBody Order body) {
|
||||
System.out.println("Order received: "+ body.getOrderId());
|
||||
return "CID" + body.getOrderId();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.service.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class Order {
|
||||
private int orderId;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
server.port=${port:6001}
|
Loading…
Reference in New Issue