From 0deedd4cc1a712ce715df40e7f130c685afe17cf Mon Sep 17 00:00:00 2001 From: Darren Wang Date: Tue, 26 Jan 2021 13:15:58 +0800 Subject: [PATCH 1/5] Adding Dapr Spring Boot Starter. --- dapr-spring-boot-starter/pom.xml | 138 ++++++++++++++++++ .../springboot/DaprAutoConfiguration.java | 19 +++ .../main/resources/META-INF/spring.factories | 2 + pom.xml | 1 + 4 files changed, 160 insertions(+) create mode 100644 dapr-spring-boot-starter/pom.xml create mode 100644 dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java create mode 100644 dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/dapr-spring-boot-starter/pom.xml b/dapr-spring-boot-starter/pom.xml new file mode 100644 index 0000000000..32c92878ef --- /dev/null +++ b/dapr-spring-boot-starter/pom.xml @@ -0,0 +1,138 @@ + + + 4.0.0 + + + dapr-sdk-parent + io.dapr + 1.0.0-SNAPSHOT + + + dapr-spring-boot-starter + jar + ${project.artifactId} + Dapr Springboot Starter + + + + + false + + jcenter + jcenter + https://jcenter.bintray.com/ + + + + + false + 2.3.5.RELEASE + + + + + + org.springframework.boot + spring-boot-dependencies + ${springboot.version} + pom + import + + + + + + + io.dapr + dapr-sdk-springboot + ${project.version} + + + org.springframework.boot + spring-boot-autoconfigure + compile + + + org.springframework.boot + spring-boot-configuration-processor + compile + true + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.2.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + attach-javadocs + + jar + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.6 + + + default-prepare-agent + + prepare-agent + + + + report + test + + report + + + target/jacoco-report/ + + + + check + + check + + + + + BUNDLE + + + LINE + COVEREDRATIO + 80% + + + + + + + + + + + \ No newline at end of file diff --git a/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java b/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java new file mode 100644 index 0000000000..893ecd7a3e --- /dev/null +++ b/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.springboot; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +/** + * Dapr's Spring Boot AutoConfiguration. + */ +@Configuration +@ConditionalOnWebApplication +@ComponentScan("io.dapr.springboot") +public class DaprAutoConfiguration { +} diff --git a/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories b/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..58ff4b58d6 --- /dev/null +++ b/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.dapr.springboot.DaprAutoConfiguration diff --git a/pom.xml b/pom.xml index 03f8dc25ca..ea444b8236 100644 --- a/pom.xml +++ b/pom.xml @@ -271,6 +271,7 @@ sdk-actors sdk-springboot examples + dapr-spring-boot-starter From 2205d97cc9602caeccba3c1ad019fcad84c66bc9 Mon Sep 17 00:00:00 2001 From: Darren Wang Date: Tue, 26 Jan 2021 22:38:15 +0800 Subject: [PATCH 2/5] Rename the directory of dapr spring boot starter and update its dependencies --- dapr-spring-boot-starter/pom.xml | 138 ------------------ .../springboot/DaprAutoConfiguration.java | 19 --- .../main/resources/META-INF/spring.factories | 2 - pom.xml | 2 +- 4 files changed, 1 insertion(+), 160 deletions(-) delete mode 100644 dapr-spring-boot-starter/pom.xml delete mode 100644 dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java delete mode 100644 dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/dapr-spring-boot-starter/pom.xml b/dapr-spring-boot-starter/pom.xml deleted file mode 100644 index 32c92878ef..0000000000 --- a/dapr-spring-boot-starter/pom.xml +++ /dev/null @@ -1,138 +0,0 @@ - - - 4.0.0 - - - dapr-sdk-parent - io.dapr - 1.0.0-SNAPSHOT - - - dapr-spring-boot-starter - jar - ${project.artifactId} - Dapr Springboot Starter - - - - - false - - jcenter - jcenter - https://jcenter.bintray.com/ - - - - - false - 2.3.5.RELEASE - - - - - - org.springframework.boot - spring-boot-dependencies - ${springboot.version} - pom - import - - - - - - - io.dapr - dapr-sdk-springboot - ${project.version} - - - org.springframework.boot - spring-boot-autoconfigure - compile - - - org.springframework.boot - spring-boot-configuration-processor - compile - true - - - - - - - org.apache.maven.plugins - maven-source-plugin - 3.2.1 - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.2.0 - - - attach-javadocs - - jar - - - - - - org.jacoco - jacoco-maven-plugin - 0.8.6 - - - default-prepare-agent - - prepare-agent - - - - report - test - - report - - - target/jacoco-report/ - - - - check - - check - - - - - BUNDLE - - - LINE - COVEREDRATIO - 80% - - - - - - - - - - - \ No newline at end of file diff --git a/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java b/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java deleted file mode 100644 index 893ecd7a3e..0000000000 --- a/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT License. - */ - -package io.dapr.springboot; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -/** - * Dapr's Spring Boot AutoConfiguration. - */ -@Configuration -@ConditionalOnWebApplication -@ComponentScan("io.dapr.springboot") -public class DaprAutoConfiguration { -} diff --git a/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories b/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 58ff4b58d6..0000000000 --- a/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -io.dapr.springboot.DaprAutoConfiguration diff --git a/pom.xml b/pom.xml index ea444b8236..452314b8aa 100644 --- a/pom.xml +++ b/pom.xml @@ -270,8 +270,8 @@ sdk sdk-actors sdk-springboot + sdk-spring-boot-starter examples - dapr-spring-boot-starter From 3fc5f936c5dbd4d0f4e5c0c2e3e3cedf1303c1ef Mon Sep 17 00:00:00 2001 From: Darren Wang Date: Tue, 26 Jan 2021 22:38:15 +0800 Subject: [PATCH 3/5] Rename the directory of dapr spring boot starter and update its dependencies --- pom.xml | 2 +- .../pom.xml | 16 +++------------- .../dapr/springboot/DaprAutoConfiguration.java | 0 .../src/main/resources/META-INF/spring.factories | 0 4 files changed, 4 insertions(+), 14 deletions(-) rename {dapr-spring-boot-starter => sdk-spring-boot-starter}/pom.xml (90%) rename {dapr-spring-boot-starter => sdk-spring-boot-starter}/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java (100%) rename {dapr-spring-boot-starter => sdk-spring-boot-starter}/src/main/resources/META-INF/spring.factories (100%) diff --git a/pom.xml b/pom.xml index ea444b8236..452314b8aa 100644 --- a/pom.xml +++ b/pom.xml @@ -270,8 +270,8 @@ sdk sdk-actors sdk-springboot + sdk-spring-boot-starter examples - dapr-spring-boot-starter diff --git a/dapr-spring-boot-starter/pom.xml b/sdk-spring-boot-starter/pom.xml similarity index 90% rename from dapr-spring-boot-starter/pom.xml rename to sdk-spring-boot-starter/pom.xml index 32c92878ef..4f195c899f 100644 --- a/dapr-spring-boot-starter/pom.xml +++ b/sdk-spring-boot-starter/pom.xml @@ -13,7 +13,7 @@ dapr-spring-boot-starter jar ${project.artifactId} - Dapr Springboot Starter + Dapr Spring Boot Starter @@ -31,18 +31,6 @@ 2.3.5.RELEASE - - - - org.springframework.boot - spring-boot-dependencies - ${springboot.version} - pom - import - - - - io.dapr @@ -52,11 +40,13 @@ org.springframework.boot spring-boot-autoconfigure + ${springboot.version} compile org.springframework.boot spring-boot-configuration-processor + ${springboot.version} compile true diff --git a/dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java b/sdk-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java similarity index 100% rename from dapr-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java rename to sdk-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java diff --git a/dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories b/sdk-spring-boot-starter/src/main/resources/META-INF/spring.factories similarity index 100% rename from dapr-spring-boot-starter/src/main/resources/META-INF/spring.factories rename to sdk-spring-boot-starter/src/main/resources/META-INF/spring.factories From 053b0fe5ed87b50d524e1999d23260af9d391012 Mon Sep 17 00:00:00 2001 From: Darren Wang Date: Tue, 26 Jan 2021 23:46:48 +0800 Subject: [PATCH 4/5] Add examples of using dapr-spring-boot-starter. --- examples-boot-native/pom.xml | 85 +++++++++++++++++ .../publisher-microservice/README.md | 93 +++++++++++++++++++ .../publisher-microservice/pom.xml | 51 ++++++++++ .../boot/pubsub/http/ClientConfig.java | 19 ++++ .../http/DaprExamplePublisherApplication.java | 16 ++++ .../boot/pubsub/http/PublisherRunner.java | 55 +++++++++++ .../subscriber-microservcie/README.md | 93 +++++++++++++++++++ .../subscriber-microservcie/pom.xml | 48 ++++++++++ .../DaprExampleSubscriberApplication.java | 16 ++++ .../pubsub/http/SubscriberController.java | 48 ++++++++++ pom.xml | 1 + 11 files changed, 525 insertions(+) create mode 100644 examples-boot-native/pom.xml create mode 100644 examples-boot-native/publisher-microservice/README.md create mode 100644 examples-boot-native/publisher-microservice/pom.xml create mode 100644 examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/ClientConfig.java create mode 100644 examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExamplePublisherApplication.java create mode 100644 examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/PublisherRunner.java create mode 100644 examples-boot-native/subscriber-microservcie/README.md create mode 100644 examples-boot-native/subscriber-microservcie/pom.xml create mode 100644 examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExampleSubscriberApplication.java create mode 100644 examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/SubscriberController.java diff --git a/examples-boot-native/pom.xml b/examples-boot-native/pom.xml new file mode 100644 index 0000000000..2dc2325491 --- /dev/null +++ b/examples-boot-native/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + + dapr-sdk-parent + io.dapr + 1.0.0-SNAPSHOT + + + dapr-sdk-examples-boot-native + pom + ${project.artifactId} + + + true + 2.3.5.RELEASE + + + + + + io.dapr + dapr-spring-boot-starter + ${project.version} + + + org.springframework.boot + spring-boot-starter-web + ${springboot.version} + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + org.apache.maven.plugins + maven-site-plugin + 3.9.1 + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + repackage + + + + + + + + + + subscriber-microservcie + publisher-microservice + + \ No newline at end of file diff --git a/examples-boot-native/publisher-microservice/README.md b/examples-boot-native/publisher-microservice/README.md new file mode 100644 index 0000000000..6ff9f2892a --- /dev/null +++ b/examples-boot-native/publisher-microservice/README.md @@ -0,0 +1,93 @@ +### Checking out the code + +Clone this repository: + +```shell +git clone https://github.com/dapr/java-sdk.git +cd java-sdk +``` + +Then build the Maven project: + +```shell +# make sure you are in the `java-sdk` directory. +mvn install +``` + +Then get into the `examples-boot-native` directory: + +```shell +cd examples-boot-native +``` + +The output jar is `publisher-microservice/target/publisher-microservice.jar` + +### Running the publisher + +**In this demo, you should run subscriber-microservice first.** + +running this microservice in dapr, e.g. using port 9090, + +```shell +dapr run -a publisher -p 9090 -- java -jar publisher-microservice/target/publisher-microservice.jar --server.port=9090 & +``` + +You can also create a file `application.properties` in `publisher-microservice/src/main/resources`, with content: + +```properties +server.port=9090 +``` + +then rebuild and exec dapr CLI with + +```shell +dapr run -a publisher -p 9090 -- java -jar publisher-microservice/target/publisher-microservice.jar & +``` + +as `server.port` is the key used to set the port that spring boot applications listen on. + +since subscriber ran first, you should get this in subscriber's terminal after publisher is running: + +``` +== APP == Published message: This is message #0 + +== APP == Subscriber got: {"id":"9abe67b7-6565-47bb-b2cc-234b8db40c0a","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #0"} + +== APP == Published message: This is message #1 + +== APP == Subscriber got: {"id":"a14b1426-86c9-4f4a-aa38-3ac49a40f2d8","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #1"} + +== APP == Published message: This is message #2 + +== APP == Subscriber got: {"id":"be83bab7-7d69-4ff3-8537-087ba56b158a","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #2"} + +== APP == Published message: This is message #3 + +== APP == Subscriber got: {"id":"9e677a56-a3dd-4009-85f9-346ef39a4797","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #3"} + +== APP == Published message: This is message #4 + +== APP == Subscriber got: {"id":"6c81e262-cc16-498f-928e-bed5ab2356a4","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #4"} + +== APP == Published message: This is message #5 + +== APP == Subscriber got: {"id":"11eded9a-dd68-4a12-8560-6433124ffc68","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #5"} + +== APP == Subscriber got: {"id":"c699e25b-1b02-48d0-8321-30a4cf11a972","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #6"} + +== APP == Published message: This is message #6 + +== APP == Published message: This is message #7 + +== APP == Subscriber got: {"id":"01620c88-333d-4e25-ac4e-bb0a29b77e57","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #7"} + +== APP == Subscriber got: {"id":"87bcfa69-2dbe-4e7f-b499-740ef381e8b1","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #8"} + +== APP == Published message: This is message #8 + +== APP == Published message: This is message #9 + +== APP == Subscriber got: {"id":"9a1d3ba3-04a1-42bb-8d3b-8f753fc0a348","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #9"} +``` + +this demo uses dapr's default subpub. \ No newline at end of file diff --git a/examples-boot-native/publisher-microservice/pom.xml b/examples-boot-native/publisher-microservice/pom.xml new file mode 100644 index 0000000000..c4dd04ec58 --- /dev/null +++ b/examples-boot-native/publisher-microservice/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + io.dapr + dapr-sdk-examples-boot-native + 1.0.0-SNAPSHOT + + + publisher-microservice + + + + org.springframework.boot + spring-boot-starter-web + + + io.dapr + dapr-spring-boot-starter + + + + + ${project.artifactId} + + + maven-install-plugin + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + repackage + + + + + + + \ No newline at end of file diff --git a/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/ClientConfig.java b/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/ClientConfig.java new file mode 100644 index 0000000000..de6991aac4 --- /dev/null +++ b/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/ClientConfig.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.examples.boot.pubsub.http; + +import io.dapr.client.DaprClient; +import io.dapr.client.DaprClientBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ClientConfig { + @Bean + public DaprClient buildDaprClient() { + return new DaprClientBuilder().build(); + } +} diff --git a/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExamplePublisherApplication.java b/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExamplePublisherApplication.java new file mode 100644 index 0000000000..c9f111f6ba --- /dev/null +++ b/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExamplePublisherApplication.java @@ -0,0 +1,16 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.examples.boot.pubsub.http; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DaprExamplePublisherApplication { + public static void main(String[] args) { + SpringApplication.run(DaprExamplePublisherApplication.class, args); + } +} diff --git a/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/PublisherRunner.java b/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/PublisherRunner.java new file mode 100644 index 0000000000..abe1b6cf34 --- /dev/null +++ b/examples-boot-native/publisher-microservice/src/main/java/io/dapr/examples/boot/pubsub/http/PublisherRunner.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.examples.boot.pubsub.http; + +import io.dapr.client.DaprClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +import static java.util.Collections.singletonMap; + +@Component +public class PublisherRunner implements CommandLineRunner { + //Number of messages to be sent: 10 + private static final int NUM_MESSAGES = 10; + + //Time-to-live for messages published. + private static final String MESSAGE_TTL_IN_SECONDS = "1000"; + + //The name of topic + public static final String TOPIC_NAME = "testingtopic"; + + //The name of the pubsub + public static final String PUBSUB_NAME = "pubsub"; + + @Autowired + private DaprClient client; + + @Override + public void run(String... args) throws Exception { + TimeUnit.SECONDS.sleep(5); + + //Creating the DaprClient: Using the default builder client produces an HTTP Dapr Client + for (int i = 0; i < NUM_MESSAGES; i++) { + String message = String.format("This is message #%d", i); + //Publishing messages + client.publishEvent(PUBSUB_NAME, TOPIC_NAME, message, + singletonMap("ttlInSeconds", MESSAGE_TTL_IN_SECONDS)).block(); + System.out.println("Published message: " + message); + + try { + Thread.sleep((long) (1000 * Math.random())); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + return; + } + } + } +} diff --git a/examples-boot-native/subscriber-microservcie/README.md b/examples-boot-native/subscriber-microservcie/README.md new file mode 100644 index 0000000000..cc64cbaea8 --- /dev/null +++ b/examples-boot-native/subscriber-microservcie/README.md @@ -0,0 +1,93 @@ +### Checking out the code + +Clone this repository: + +```shell +git clone https://github.com/dapr/java-sdk.git +cd java-sdk +``` + +Then build the Maven project: + +```shell +# make sure you are in the `java-sdk` directory. +mvn install +``` + +Then get into the `examples-boot-native` directory: + +```shell +cd examples-boot-native +``` + +The output jar is `subscriber-microservice/target/subscriber-microservice.jar` + +### Running the subscriber + +**In this demo, you should run subscriber-microservice first.** + +running this microservice in dapr, e.g. using port 8090, + +```shell +dapr run -a subscriber -p 8090 -- java -jar subscriber-microservice/target/subscriber-microservice.jar --server.port=8090 & +``` + +You can also create a file `application.properties` in `subscriber-microservice/src/main/resources`, with content: + +```properties +server.port=8090 +``` + +then rebuild and exec dapr CLI with + +```shell +dapr run -a subscriber -p 8090 -- java -jar subscriber-microservice/target/subscriber-microservice.jar & +``` + +as `server.port` is the key used to set the port that spring boot applications listen on. + +since subscriber ran first, you should get this in subscriber's terminal after publisher is running: + +``` +== APP == Published message: This is message #0 + +== APP == Subscriber got: {"id":"9abe67b7-6565-47bb-b2cc-234b8db40c0a","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #0"} + +== APP == Published message: This is message #1 + +== APP == Subscriber got: {"id":"a14b1426-86c9-4f4a-aa38-3ac49a40f2d8","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #1"} + +== APP == Published message: This is message #2 + +== APP == Subscriber got: {"id":"be83bab7-7d69-4ff3-8537-087ba56b158a","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #2"} + +== APP == Published message: This is message #3 + +== APP == Subscriber got: {"id":"9e677a56-a3dd-4009-85f9-346ef39a4797","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #3"} + +== APP == Published message: This is message #4 + +== APP == Subscriber got: {"id":"6c81e262-cc16-498f-928e-bed5ab2356a4","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #4"} + +== APP == Published message: This is message #5 + +== APP == Subscriber got: {"id":"11eded9a-dd68-4a12-8560-6433124ffc68","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #5"} + +== APP == Subscriber got: {"id":"c699e25b-1b02-48d0-8321-30a4cf11a972","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #6"} + +== APP == Published message: This is message #6 + +== APP == Published message: This is message #7 + +== APP == Subscriber got: {"id":"01620c88-333d-4e25-ac4e-bb0a29b77e57","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #7"} + +== APP == Subscriber got: {"id":"87bcfa69-2dbe-4e7f-b499-740ef381e8b1","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #8"} + +== APP == Published message: This is message #8 + +== APP == Published message: This is message #9 + +== APP == Subscriber got: {"id":"9a1d3ba3-04a1-42bb-8d3b-8f753fc0a348","source":"pub","type":"com.dapr.event.sent","specversion":"1.0","datacontenttype":"application/json","data":"This is message #9"} +``` + +this demo uses dapr's default subpub. \ No newline at end of file diff --git a/examples-boot-native/subscriber-microservcie/pom.xml b/examples-boot-native/subscriber-microservcie/pom.xml new file mode 100644 index 0000000000..c99c656e02 --- /dev/null +++ b/examples-boot-native/subscriber-microservcie/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + + io.dapr + dapr-sdk-examples-boot-native + 1.0.0-SNAPSHOT + + + subscriber-microservcie + + + + org.springframework.boot + spring-boot-starter-web + + + io.dapr + dapr-spring-boot-starter + + + + + ${project.artifactId} + + + maven-install-plugin + + true + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + \ No newline at end of file diff --git a/examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExampleSubscriberApplication.java b/examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExampleSubscriberApplication.java new file mode 100644 index 0000000000..41909c3932 --- /dev/null +++ b/examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/DaprExampleSubscriberApplication.java @@ -0,0 +1,16 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.examples.boot.pubsub.http; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DaprExampleSubscriberApplication { + public static void main(String[] args) { + SpringApplication.run(DaprExampleSubscriberApplication.class, args); + } +} diff --git a/examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/SubscriberController.java b/examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/SubscriberController.java new file mode 100644 index 0000000000..4f6d2ca8c7 --- /dev/null +++ b/examples-boot-native/subscriber-microservcie/src/main/java/io/dapr/examples/boot/pubsub/http/SubscriberController.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.examples.boot.pubsub.http; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dapr.Topic; +import io.dapr.client.domain.CloudEvent; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +@RestController +public class SubscriberController { + + //The name of topic + private static final String TOPIC_NAME = "testingtopic"; + + //The name of the pubsub + private static final String PUBSUB_NAME = "pubsub"; + + @Autowired + private ObjectMapper objectMapper; + + /** + * 1.0.0-rc2的时候cloudEvent还没有无参构造方法,直接放在请求参数里面会运行时报错,因为jackson无法创建对象 + * Handles a registered publish endpoint on this app. + * @param body The cloud event received. + * @return A message containing the time. + */ + @Topic(name = TOPIC_NAME, pubsubName = PUBSUB_NAME) + @PostMapping(path = "/" + TOPIC_NAME) + public Mono handleMessage(@RequestBody(required = false) byte[] body) { + return Mono.fromRunnable(() -> { + try { + CloudEvent envelope = CloudEvent.deserialize(body); + String message = envelope.getData() == null ? "" : envelope.getData().toString(); + System.out.println("Subscriber got: " + objectMapper.writeValueAsString(envelope)); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } +} diff --git a/pom.xml b/pom.xml index 452314b8aa..987d5b1674 100644 --- a/pom.xml +++ b/pom.xml @@ -272,6 +272,7 @@ sdk-springboot sdk-spring-boot-starter examples + examples-boot-native From 4af271f6789b4e54bba8891f0070606e2ff8e2b0 Mon Sep 17 00:00:00 2001 From: Darren Wang Date: Wed, 27 Jan 2021 00:17:53 +0800 Subject: [PATCH 5/5] Cleaning code --- examples-boot-native/pom.xml | 3 --- examples-boot-native/publisher-microservice/pom.xml | 3 --- 2 files changed, 6 deletions(-) diff --git a/examples-boot-native/pom.xml b/examples-boot-native/pom.xml index 2dc2325491..a92d7c6321 100644 --- a/examples-boot-native/pom.xml +++ b/examples-boot-native/pom.xml @@ -63,9 +63,6 @@ org.springframework.boot spring-boot-maven-plugin - - - diff --git a/examples-boot-native/publisher-microservice/pom.xml b/examples-boot-native/publisher-microservice/pom.xml index c4dd04ec58..16591e8aca 100644 --- a/examples-boot-native/publisher-microservice/pom.xml +++ b/examples-boot-native/publisher-microservice/pom.xml @@ -35,9 +35,6 @@ org.springframework.boot spring-boot-maven-plugin - - -