Feature/java http pubsub quickstarts (#534)

* code changes for pubsub using http request

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* Removing .mvn files and fixing other review comments.

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* Incorporating review comments

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>

* Fixing README.md as suggested

Signed-off-by: Pravin Pushkar <ppushkar@microsoft.com>
This commit is contained in:
Pravin Pushkar 2022-02-18 01:24:44 +05:30 committed by GitHub
parent 85f6c6cc66
commit d4ffc3347e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 339 additions and 0 deletions

39
pub_sub/java/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,61 @@
# Dapr pub/sub
In this quickstart, there is a publisher microservice `checkout` and a subscriber microservice `order-processor` to demonstrate how Dapr enables a publish-subscribe pattern. `checkout` generates messages and publishes to a specific orders topic, and `order-processor` subscribers listen for messages of topic orders.
Visit [this](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) link for more information about Dapr and Pub-Sub.
> **Note:** This example leverages HTTPClient only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk).
This quickstart includes one publisher:
- Java client message generator `checkout`
And one subscriber:
- Java subscriber `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 message publisher app with Dapr
1. Open a new terminal window and navigate to `checkout` directory:
```bash
cd checkout
```
2. Install dependencies:
```bash
mvn clean install
```
3. Run the Java publisher app with Dapr:
```bash
dapr run --app-id checkout --components-path ../../../components -- java -jar target/CheckoutService-0.0.1-SNAPSHOT.jar
```
### Run Java message subscriber app with Dapr
1. Open a new terminal window and navigate to `order-processor` directory:
```bash
cd order-processor
```
2. Install dependencies:
```bash
mvn clean install
```
3. Run the Java subscriber app with Dapr:
```bash
dapr run --app-port 8080 --app-id order-processor --components-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar
```

View File

@ -0,0 +1,53 @@
<?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 pubsub 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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</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>

View File

@ -0,0 +1,48 @@
package com.service;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Random;
import java.util.concurrent.TimeUnit;
public class CheckoutServiceApplication {
private static final Logger logger = LoggerFactory.getLogger(CheckoutServiceApplication.class);
private static final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.build();
private static final String PUBSUB_NAME = "order_pub_sub";
private static final String TOPIC = "orders";
private static String DAPR_HOST = System.getenv().getOrDefault("DAPR_HOST", "http://localhost");
private static String DAPR_HTTP_PORT = System.getenv().getOrDefault("DAPR_HTTP_PORT", "3500");
public static void main(String[] args) throws InterruptedException, IOException {
String uri = DAPR_HOST +":"+ DAPR_HTTP_PORT + "/v1.0/publish/"+PUBSUB_NAME+"/"+TOPIC;
while (true) {
Random random = new Random();
int orderId = random.nextInt(1000 - 1) + 1;
JSONObject obj = new JSONObject();
obj.put("orderId", orderId);
// Publish an event/message using Dapr PubSub via HTTP Post
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(obj.toString()))
.uri(URI.create(uri))
.header("Content-Type", "application/json")
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
logger.info("Published data: {}", orderId);
TimeUnit.MILLISECONDS.sleep(3000);
}
}
}

View File

@ -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 pubsub 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>

View File

@ -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);
}
}

View File

@ -0,0 +1,47 @@
package com.service.controller;
import com.service.model.DaprSubscription;
import com.service.model.Order;
import com.service.model.SubscriptionData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {
private static final Logger logger = LoggerFactory.getLogger(OrderProcessingServiceController.class);
/**
* Register Dapr pub/sub subscriptions.
*
* @return DaprSubscription Object containing pubsub name, topic and route for subscription.
*/
@GetMapping(path = "/dapr/subscribe", produces = MediaType.APPLICATION_JSON_VALUE)
public DaprSubscription[] getSubscription() {
DaprSubscription daprSubscription = DaprSubscription.builder()
.pubSubName("order_pub_sub")
.topic("orders")
.route("orders")
.build();
logger.info("Subscribed to Pubsubname {} and topic {}", "order_pub_sub", "orders");
DaprSubscription[] arr = new DaprSubscription[]{daprSubscription};
return arr;
}
/**
* Dapr subscription in /dapr/subscribe sets up this route.
*
* @param body Request body
* @return ResponseEntity Returns ResponseEntity.ok()
*/
@PostMapping(path = "/orders", consumes = MediaType.ALL_VALUE)
public ResponseEntity<?> processOrders(@RequestBody SubscriptionData<Order> body) {
logger.info("Subscriber received: "+ body.getData().getOrderId());
return ResponseEntity.ok().build();
}
}

View File

@ -0,0 +1,12 @@
package com.service.model;
import lombok.Builder;
import lombok.Getter;
@Builder
@Getter
public class DaprSubscription {
private String pubSubName;
private String topic;
private String route;
}

View File

@ -0,0 +1,13 @@
package com.service.model;
import lombok.Getter;
import lombok.Setter;
/**
* A class to represent the data object that is being passed to subscribe endpoint.
*/
@Getter
@Setter
public class Order {
private int orderId;
}

View File

@ -0,0 +1,15 @@
package com.service.model;
import lombok.Getter;
import lombok.Setter;
/**
* Helps in unpacking a cloud event which Checkout application generates.
*
* @param <T> Type of data being passed to subscriber end point(orderProcessor app)
*/
@Getter
@Setter
public class SubscriptionData<T> {
private T data;
}