Skip to content

Commit 185cdba

Browse files
Config api support (dapr#670)
* inital draft for config api Signed-off-by: Pravin Pushkar <[email protected]> * Introducing new client for preview apis and code refactoring Signed-off-by: Pravin Pushkar <[email protected]> * Unit tests and code refactoring Signed-off-by: Pravin Pushkar <[email protected]> * Adding integration test Signed-off-by: pravinpushkar <[email protected]> * Copyright changes Signed-off-by: pravinpushkar <[email protected]> * Review comments fixes Signed-off-by: pravinpushkar <[email protected]> * Removed DaprPreviewClientProxy and updated example README Signed-off-by: pravinpushkar <[email protected]> * Adding validate workflow for cofiguration api example Signed-off-by: pravinpushkar <[email protected]> * fixing example autovalidation and code coverage Signed-off-by: pravinpushkar <[email protected]> * Fixing autovalidation and removing getAllConfiguration Signed-off-by: pravinpushkar <[email protected]> * Fixing review comments Signed-off-by: pravinpushkar <[email protected]> * Add regex header checkstyle. Signed-off-by: Artur Souza <[email protected]> * Fix headers and add javadocs to some. Signed-off-by: Artur Souza <[email protected]> Co-authored-by: Artur Souza <[email protected]>
1 parent d8b8a5b commit 185cdba

File tree

20 files changed

+1328
-15
lines changed

20 files changed

+1328
-15
lines changed

.github/workflows/validate.yml

+4
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,7 @@ jobs:
150150
working-directory: ./examples
151151
run: |
152152
mm.py ./src/main/java/io/dapr/examples/unittesting/README.md
153+
- name: Validate Configuration API example
154+
working-directory: ./examples
155+
run: |
156+
mm.py ./src/main/java/io/dapr/examples/configuration/grpc/README.md

.java_header

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
^/\*$
2+
^ \* Copyright \d\d\d\d The Dapr Authors$
3+
^ \* Licensed under the Apache License, Version 2.0 \(the "License"\)\;$
4+
^ \* you may not use this file except in compliance with the License\.$
5+
^ \* You may obtain a copy of the License at$
6+
^ \* http://www.apache.org/licenses/LICENSE-2\.0$
7+
^ \* Unless required by applicable law or agreed to in writing, software$
8+
^ \* distributed under the License is distributed on an "AS IS" BASIS,$
9+
^ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.$
10+
^ \* See the License for the specific language governing permissions and$
11+
^limitations under the License\.$
12+
^\*/$

checkstyle.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@
5151
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
5252
</module>
5353

54-
<module name="Header">
54+
<module name="RegexpHeader">
5555
<property name="fileExtensions" value="java"/>
56-
<!-- We just validate the top 2 lines so this attribute's value does not blow away. -->
57-
<property name="header" value='/*\n * Copyright 2021 The Dapr Authors\n * Licensed under the Apache License, Version 2.0 (the "License");\n'/>
56+
<property name="headerFile" value=".java_header"/>
5857
</module>
5958

6059
<module name="TreeWalker">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: configstore
5+
spec:
6+
type: configuration.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright 2022 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.configuration.grpc;
15+
16+
import io.dapr.client.DaprClientBuilder;
17+
import io.dapr.client.DaprPreviewClient;
18+
import io.dapr.client.domain.ConfigurationItem;
19+
import io.dapr.client.domain.GetConfigurationRequest;
20+
import io.dapr.client.domain.SubscribeConfigurationRequest;
21+
import reactor.core.Disposable;
22+
import reactor.core.publisher.Flux;
23+
import reactor.core.publisher.Mono;
24+
25+
import java.io.IOException;
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.List;
29+
import java.util.concurrent.atomic.AtomicReference;
30+
31+
public class ConfigurationClient {
32+
33+
private static final String CONFIG_STORE_NAME = "configstore";
34+
35+
private static final List<String> keys = new ArrayList<>(Arrays.asList("myconfig1", "myconfig3", "myconfig2"));
36+
37+
/**
38+
* Executes various methods to check the different apis.
39+
* @param args arguments
40+
* @throws Exception throws Exception
41+
*/
42+
public static void main(String[] args) throws Exception {
43+
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
44+
System.out.println("Using preview client...");
45+
getConfigurationForaSingleKey(client);
46+
getConfigurationsUsingVarargs(client);
47+
getConfigurations(client);
48+
subscribeConfigurationRequestWithSubscribe(client);
49+
}
50+
}
51+
52+
/**
53+
* Gets configuration for a single key.
54+
*
55+
* @param client DaprPreviewClient object
56+
*/
57+
public static void getConfigurationForaSingleKey(DaprPreviewClient client) {
58+
System.out.println("*******trying to retrieve configuration given a single key********");
59+
try {
60+
Mono<ConfigurationItem> item = client.getConfiguration(CONFIG_STORE_NAME, keys.get(0));
61+
System.out.println("Value ->" + item.block().getValue() + " key ->" + item.block().getKey());
62+
} catch (Exception ex) {
63+
System.out.println(ex.getMessage());
64+
}
65+
}
66+
67+
/**
68+
* Gets configurations for varibale no. of arguments.
69+
*
70+
* @param client DaprPreviewClient object
71+
*/
72+
public static void getConfigurationsUsingVarargs(DaprPreviewClient client) {
73+
System.out.println("*******trying to retrieve configurations for a variable no. of keys********");
74+
try {
75+
Mono<List<ConfigurationItem>> items =
76+
client.getConfiguration(CONFIG_STORE_NAME, "myconfig1", "myconfig3");
77+
items.block().forEach(ConfigurationClient::print);
78+
} catch (Exception ex) {
79+
System.out.println(ex.getMessage());
80+
}
81+
}
82+
83+
/**
84+
* Gets configurations for a list of keys.
85+
*
86+
* @param client DaprPreviewClient object
87+
*/
88+
public static void getConfigurations(DaprPreviewClient client) {
89+
System.out.println("*******trying to retrieve configurations for a list of keys********");
90+
List<String> keys = new ArrayList<>();
91+
keys.add("myconfig1");
92+
keys.add("myconfig2");
93+
keys.add("myconfig3");
94+
GetConfigurationRequest req = new GetConfigurationRequest(CONFIG_STORE_NAME, keys);
95+
try {
96+
Mono<List<ConfigurationItem>> items = client.getConfiguration(req);
97+
items.block().forEach(ConfigurationClient::print);
98+
} catch (Exception ex) {
99+
System.out.println(ex.getMessage());
100+
}
101+
}
102+
103+
/**
104+
* Subscribe to a list of keys.Optional to above iterator way of retrieving the changes
105+
*
106+
* @param client DaprPreviewClient object
107+
*/
108+
public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient client) {
109+
System.out.println("*****Subscribing to keys using subscribe method: " + keys.toString() + " *****");
110+
AtomicReference<Disposable> disposableAtomicReference = new AtomicReference<>();
111+
SubscribeConfigurationRequest req = new SubscribeConfigurationRequest(CONFIG_STORE_NAME, keys);
112+
Runnable subscribeTask = () -> {
113+
Flux<List<ConfigurationItem>> outFlux = client.subscribeToConfiguration(req);
114+
disposableAtomicReference.set(outFlux
115+
.subscribe(
116+
cis -> cis.forEach(ConfigurationClient::print)
117+
));
118+
};
119+
new Thread(subscribeTask).start();
120+
try {
121+
// To ensure that subscribeThread gets scheduled
122+
Thread.sleep(0);
123+
} catch (InterruptedException e) {
124+
e.printStackTrace();
125+
}
126+
Runnable updateKeys = () -> {
127+
int i = 1;
128+
while (i <= 3) {
129+
executeDockerCommand(i);
130+
i++;
131+
}
132+
};
133+
new Thread(updateKeys).start();
134+
try {
135+
// To ensure main thread does not die before outFlux subscribe gets called
136+
Thread.sleep(10000);
137+
disposableAtomicReference.get().dispose();
138+
} catch (InterruptedException e) {
139+
e.printStackTrace();
140+
}
141+
}
142+
143+
private static void print(ConfigurationItem item) {
144+
System.out.println(item.getValue() + " : key ->" + item.getKey());
145+
}
146+
147+
private static void executeDockerCommand(int postfix) {
148+
String[] command = new String[] {
149+
"docker", "exec", "dapr_redis", "redis-cli",
150+
"SET",
151+
"myconfig" + postfix, "update_myconfigvalue" + postfix + "||2"
152+
};
153+
ProcessBuilder processBuilder = new ProcessBuilder(command);
154+
Process process = null;
155+
try {
156+
process = processBuilder.start();
157+
process.waitFor();
158+
} catch (IOException e) {
159+
e.printStackTrace();
160+
} catch (InterruptedException e) {
161+
e.printStackTrace();
162+
}
163+
}
164+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## Retrieve Configurations via Configuration API
2+
3+
This example provides the different capabilities provided by Dapr Java SDK for Configuration. For further information about Configuration APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/configuration/)
4+
**This API is available in Preview Mode**.
5+
6+
### Using the ConfigurationAPI
7+
8+
The java SDK exposes several methods for this -
9+
* `client.getConfiguration(...)` for getting a configuration for a single/multiple keys.
10+
* `client.subscribeToConfigurations(...)` for subscribing to a list of keys for any change.
11+
12+
## Pre-requisites
13+
14+
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/).
15+
* Java JDK 11 (or greater): [Oracle JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) or [OpenJDK](https://jdk.java.net/13/).
16+
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.
17+
18+
### Checking out the code
19+
20+
Clone this repository:
21+
22+
```sh
23+
git clone https://github.com/dapr/java-sdk.git
24+
cd java-sdk
25+
```
26+
27+
Then build the Maven project:
28+
29+
```sh
30+
# make sure you are in the `java-sdk` directory.
31+
mvn install
32+
```
33+
## Store few dummy configurations in configurationstore
34+
<!-- STEP
35+
name: Set configuration value
36+
expected_stdout_lines:
37+
- "OK"
38+
timeout_seconds: 20
39+
-->
40+
41+
```bash
42+
docker exec dapr_redis redis-cli MSET myconfig1 "val1||1" myconfig2 "val2||1" myconfig3 "val3||1"
43+
```
44+
<!-- END_STEP -->
45+
46+
### Running the example
47+
48+
Get into the examples' directory:
49+
```sh
50+
cd examples
51+
```
52+
53+
Use the following command to run this example-
54+
55+
<!-- STEP
56+
name: Run ConfigurationClient example
57+
expected_stdout_lines:
58+
- "== APP == Using preview client..."
59+
- "== APP == *******trying to retrieve configuration given a single key********"
60+
- "== APP == Value ->val1 key ->myconfig1"
61+
- "== APP == *******trying to retrieve configurations for a variable no. of keys********"
62+
- "== APP == val1 : key ->myconfig1"
63+
- "== APP == val3 : key ->myconfig3"
64+
- "== APP == *******trying to retrieve configurations for a list of keys********"
65+
- "== APP == val1 : key ->myconfig1"
66+
- "== APP == val2 : key ->myconfig2"
67+
- "== APP == val3 : key ->myconfig3"
68+
- "== APP == *****Subscribing to keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****"
69+
- "== APP == update_myconfigvalue1 : key ->myconfig1"
70+
- "== APP == update_myconfigvalue2 : key ->myconfig2"
71+
- "== APP == update_myconfigvalue3 : key ->myconfig3"
72+
background: true
73+
sleep: 5
74+
-->
75+
76+
```bash
77+
dapr run --components-path ./components/configuration --app-id configgrpc --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.configuration.grpc.ConfigurationClient
78+
```
79+
80+
<!-- END_STEP -->
81+
82+
### Sample output
83+
```
84+
== APP == Using preview client...
85+
== APP == *******trying to retrieve configuration given a single key********
86+
== APP == Value ->val1 key ->myconfig1
87+
== APP == *******trying to retrieve configurations for a variable no. of keys********
88+
== APP == val1 : key ->myconfig1
89+
== APP == val3 : key ->myconfig3
90+
== APP == *******trying to retrieve configurations for a list of keys********
91+
== APP == val1 : key ->myconfig1
92+
== APP == val2 : key ->myconfig2
93+
== APP == val3 : key ->myconfig3
94+
== APP == *****Subscribing to keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****
95+
== APP == update_myconfigvalue1 : key ->myconfig1
96+
== APP == update_myconfigvalue2 : key ->myconfig2
97+
== APP == update_myconfigvalue3 : key ->myconfig3
98+
99+
```
100+
### Cleanup
101+
102+
To stop the app, run (or press CTRL+C):
103+
104+
<!-- STEP
105+
name: Cleanup
106+
-->
107+
108+
```bash
109+
dapr stop --app-id configgrpc
110+
```
111+
112+
<!-- END_STEP -->
113+

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<grpc.version>1.39.0</grpc.version>
1818
<protobuf.version>3.13.0</protobuf.version>
19-
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.4.0-rc.6/dapr/proto</dapr.proto.baseurl>
19+
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.5.1/dapr/proto</dapr.proto.baseurl>
2020
<os-maven-plugin.version>1.6.2</os-maven-plugin.version>
2121
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
2222
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: redisconfigstore
5+
spec:
6+
type: configuration.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""

0 commit comments

Comments
 (0)