Skip to content

Commit 4611f40

Browse files
authored
Springboot integration. (dapr#256)
1 parent 61e8a41 commit 4611f40

File tree

24 files changed

+354
-167
lines changed

24 files changed

+354
-167
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ hs_err_pid*
4949
/docs/dapr-sdk
5050
/proto/dapr
5151
/proto/daprclient
52+
53+
# macOS
54+
.DS_Store

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ For a Maven project, add the following to your `pom.xml` file:
6868
<artifactId>dapr-sdk-actors</artifactId>
6969
<version>0.3.0</version>
7070
</dependency>
71+
<!-- Dapr's SDK integration with SpringBoot (optional). -->
72+
<dependency>
73+
<groupId>io.dapr</groupId>
74+
<artifactId>dapr-sdk-springboot</artifactId>
75+
<version>0.4.0-SNAPSHOT</version>
76+
</dependency>
7177
<!-- If needed, resolve version conflict of okhttp3. -->
7278
<dependency>
7379
<groupId>com.squareup.okhttp3</groupId>
@@ -103,6 +109,8 @@ dependencies {
103109
compile('io.dapr:dapr-sdk:0.3.0')
104110
// Dapr's SDK for Actors (optional).
105111
compile('io.dapr:dapr-sdk-actors:0.3.0')
112+
// Dapr's SDK integration with SpringBoot (optional).
113+
compile('io.dapr:dapr-sdk-springboot:0.4.0-SNAPSHOT')
106114
107115
// If needed, force conflict resolution for okhttp3.
108116
configurations.all {

examples/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
<artifactId>spring-boot-autoconfigure</artifactId>
7474
<version>2.2.2.RELEASE</version>
7575
</dependency>
76+
<dependency>
77+
<groupId>io.dapr</groupId>
78+
<artifactId>dapr-sdk-springboot</artifactId>
79+
<version>${project.version}</version>
80+
</dependency>
7681
<dependency>
7782
<groupId>io.dapr</groupId>
7883
<artifactId>dapr-sdk-actors</artifactId>

examples/src/main/java/io/dapr/examples/actors/http/DemoActorService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.apache.commons.cli.DefaultParser;
1313
import org.apache.commons.cli.Options;
1414

15+
import java.time.Duration;
16+
1517
/**
1618
* Service for Actor runtime.
1719
* 1. Build and install jars:
@@ -36,7 +38,16 @@ public static void main(String[] args) throws Exception {
3638
CommandLine cmd = parser.parse(options, args);
3739

3840
// If port string is not valid, it will throw an exception.
39-
int port = Integer.parseInt(cmd.getOptionValue("port"));
41+
final int port = Integer.parseInt(cmd.getOptionValue("port"));
42+
43+
// Idle timeout until actor instance is deactivated.
44+
ActorRuntime.getInstance().getConfig().setActorIdleTimeout(Duration.ofSeconds(30));
45+
// How often actor instances are scanned for deactivation and balance.
46+
ActorRuntime.getInstance().getConfig().setActorScanInterval(Duration.ofSeconds(10));
47+
// How long to wait until for draining an ongoing API call for an actor instance.
48+
ActorRuntime.getInstance().getConfig().setDrainOngoingCallTimeout(Duration.ofSeconds(10));
49+
// Determines whether to drain API calls for actors instances being balanced.
50+
ActorRuntime.getInstance().getConfig().setDrainBalancedActors(true);
4051

4152
// Register the Actor class.
4253
ActorRuntime.getInstance().registerActor(DemoActorImpl.class);

examples/src/main/java/io/dapr/examples/actors/http/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ mvn install
3838
The first Java class is `DemoActorService`. Its job is to register an implementation of `DemoActor` in the Dapr's Actor runtime. In `DemoActorService.java` file, you will find the `DemoActorService` class and the `main` method. See the code snippet below:
3939

4040
```java
41-
@SpringBootApplication
4241
public class DemoActorService {
4342

4443
public static void main(String[] args) throws Exception {

examples/src/main/java/io/dapr/examples/pubsub/http/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,14 @@ public class Subscriber {
4444
```
4545
`DaprApplication.start()` Method will run an Spring Boot application that registers the `SubscriberController`, which exposes the message retrieval as a POST request. The Dapr's sidecar is the one that performs the actual call to the controller, based on the pubsub features.
4646

47-
This Spring Controller handles the message endpoint, Printing the recieved message which is recieved as the POST body. See the code snippet below:
47+
This Spring Controller handles the message endpoint, Printing the message which is received as the POST body. The topic subscription in Dapr is handled automatically via the `@Topic` annotation. See the code snippet below:
4848

4949
```java
5050
@RestController
5151
public class SubscriberController {
5252
///...
53-
@GetMapping("/dapr/subscribe")
54-
public byte[] daprConfig() throws Exception {
55-
return SERIALIZER.serialize(new String[] { "message" });
56-
}
57-
58-
@PostMapping(path = "/message")
53+
@Topic(name = "testingtopic")
54+
@PostMapping(path = "/testingtopic")
5955
public Mono<Void> handleMessage(@RequestBody(required = false) byte[] body,
6056
@RequestHeader Map<String, String> headers) {
6157
return Mono.fromRunnable(() -> {

examples/src/main/java/io/dapr/examples/pubsub/http/SubscriberController.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.dapr.examples.pubsub.http;
77

8+
import io.dapr.Topic;
89
import io.dapr.client.domain.CloudEvent;
910
import io.dapr.serializer.DefaultObjectSerializer;
1011
import org.springframework.web.bind.annotation.GetMapping;
@@ -22,22 +23,13 @@
2223
@RestController
2324
public class SubscriberController {
2425

25-
/**
26-
* Dapr's default serializer/deserializer.
27-
*/
28-
private static final DefaultObjectSerializer SERIALIZER = new DefaultObjectSerializer();
29-
30-
@GetMapping("/dapr/subscribe")
31-
public byte[] daprConfig() throws Exception {
32-
return SERIALIZER.serialize(new String[]{"testingtopic"});
33-
}
34-
3526
/**
3627
* Handles a registered publish endpoint on this app.
3728
* @param body The body of the http message.
3829
* @param headers The headers of the http message.
3930
* @return A message containing the time.
4031
*/
32+
@Topic(name = "testingtopic")
4133
@PostMapping(path = "/testingtopic")
4234
public Mono<Void> handleMessage(@RequestBody(required = false) byte[] body,
4335
@RequestHeader Map<String, String> headers) {

examples/src/main/java/io/dapr/springboot/DaprApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/**
1212
* Dapr's HTTP callback implementation via SpringBoot.
13+
* Scanning package io.dapr.springboot is required.
1314
*/
1415
@SpringBootApplication(scanBasePackages = {"io.dapr.springboot", "io.dapr.examples"})
1516
public class DaprApplication {

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
<module>sdk-autogen</module>
253253
<module>sdk</module>
254254
<module>sdk-actors</module>
255+
<module>sdk-springboot</module>
255256
<module>examples</module>
256257
</modules>
257258

sdk-springboot/pom.xml

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<project
2+
xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.dapr</groupId>
9+
<artifactId>dapr-sdk-parent</artifactId>
10+
<version>0.4.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>dapr-sdk-springboot</artifactId>
14+
<packaging>jar</packaging>
15+
<version>0.4.0-SNAPSHOT</version>
16+
<name>dapr-sdk-springboot</name>
17+
<description>SDK extension for Springboot</description>
18+
19+
<repositories>
20+
<repository>
21+
<snapshots>
22+
<enabled>false</enabled>
23+
</snapshots>
24+
<id>central</id>
25+
<name>libs-release</name>
26+
<url>https://repo.spring.io/libs-release</url>
27+
</repository>
28+
</repositories>
29+
30+
<properties>
31+
<maven.deploy.skip>false</maven.deploy.skip>
32+
</properties>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>io.dapr</groupId>
37+
<artifactId>dapr-sdk</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.dapr</groupId>
42+
<artifactId>dapr-sdk-actors</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.mockito</groupId>
52+
<artifactId>mockito-core</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.github.gmazzo</groupId>
57+
<artifactId>okhttp-mock</artifactId>
58+
<version>1.3.2</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter-api</artifactId>
64+
<version>5.5.2</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.springframework</groupId>
69+
<artifactId>spring-beans</artifactId>
70+
<version>5.2.3.RELEASE</version>
71+
<scope>compile</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework</groupId>
75+
<artifactId>spring-web</artifactId>
76+
<version>5.2.2.RELEASE</version>
77+
<scope>compile</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.springframework</groupId>
81+
<artifactId>spring-web</artifactId>
82+
<version>5.2.2.RELEASE</version>
83+
<scope>compile</scope>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.springframework</groupId>
87+
<artifactId>spring-context</artifactId>
88+
<version>5.2.2.RELEASE</version>
89+
<scope>compile</scope>
90+
</dependency>
91+
</dependencies>
92+
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-source-plugin</artifactId>
98+
<version>3.2.0</version>
99+
<executions>
100+
<execution>
101+
<id>attach-sources</id>
102+
<goals>
103+
<goal>jar-no-fork</goal>
104+
</goals>
105+
</execution>
106+
</executions>
107+
</plugin>
108+
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-javadoc-plugin</artifactId>
112+
<version>3.1.1</version>
113+
<executions>
114+
<execution>
115+
<id>attach-javadocs</id>
116+
<goals>
117+
<goal>jar</goal>
118+
</goals>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
<plugin>
123+
<groupId>org.jacoco</groupId>
124+
<artifactId>jacoco-maven-plugin</artifactId>
125+
<version>0.8.4</version>
126+
<executions>
127+
<execution>
128+
<id>default-prepare-agent</id>
129+
<goals>
130+
<goal>prepare-agent</goal>
131+
</goals>
132+
</execution>
133+
<execution>
134+
<id>report</id>
135+
<phase>test</phase>
136+
<goals>
137+
<goal>report</goal>
138+
</goals>
139+
<configuration>
140+
<outputDirectory>target/jacoco-report/</outputDirectory>
141+
</configuration>
142+
</execution>
143+
<execution>
144+
<id>check</id>
145+
<goals>
146+
<goal>check</goal>
147+
</goals>
148+
<configuration>
149+
<rules>
150+
<rule>
151+
<element>BUNDLE</element>
152+
<limits>
153+
<limit>
154+
<counter>LINE</counter>
155+
<value>COVEREDRATIO</value>
156+
<minimum>80%</minimum>
157+
</limit>
158+
</limits>
159+
</rule>
160+
</rules>
161+
</configuration>
162+
</execution>
163+
164+
</executions>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
</project>

0 commit comments

Comments
 (0)