diff --git a/examples-boot-native/pom.xml b/examples-boot-native/pom.xml new file mode 100644 index 0000000000..a92d7c6321 --- /dev/null +++ b/examples-boot-native/pom.xml @@ -0,0 +1,82 @@ + + + 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..16591e8aca --- /dev/null +++ b/examples-boot-native/publisher-microservice/pom.xml @@ -0,0 +1,48 @@ + + + 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 03f8dc25ca..987d5b1674 100644 --- a/pom.xml +++ b/pom.xml @@ -270,7 +270,9 @@ sdk sdk-actors sdk-springboot + sdk-spring-boot-starter examples + examples-boot-native diff --git a/sdk-spring-boot-starter/pom.xml b/sdk-spring-boot-starter/pom.xml new file mode 100644 index 0000000000..4f195c899f --- /dev/null +++ b/sdk-spring-boot-starter/pom.xml @@ -0,0 +1,128 @@ + + + 4.0.0 + + + dapr-sdk-parent + io.dapr + 1.0.0-SNAPSHOT + + + dapr-spring-boot-starter + jar + ${project.artifactId} + Dapr Spring Boot Starter + + + + + false + + jcenter + jcenter + https://jcenter.bintray.com/ + + + + + false + 2.3.5.RELEASE + + + + + io.dapr + dapr-sdk-springboot + ${project.version} + + + org.springframework.boot + spring-boot-autoconfigure + ${springboot.version} + compile + + + org.springframework.boot + spring-boot-configuration-processor + ${springboot.version} + 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/sdk-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java b/sdk-spring-boot-starter/src/main/java/io/dapr/springboot/DaprAutoConfiguration.java new file mode 100644 index 0000000000..893ecd7a3e --- /dev/null +++ b/sdk-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/sdk-spring-boot-starter/src/main/resources/META-INF/spring.factories b/sdk-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..58ff4b58d6 --- /dev/null +++ b/sdk-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +io.dapr.springboot.DaprAutoConfiguration