-
Notifications
You must be signed in to change notification settings - Fork 228
Config api support #670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Config api support #670
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8f5288b
inital draft for config api
pravinpushkar d9ac5c0
Introducing new client for preview apis and code refactoring
pravinpushkar 8d52bcb
Unit tests and code refactoring
pravinpushkar c28105f
Adding integration test
pravinpushkar 276debc
Copyright changes
pravinpushkar 2035de9
Review comments fixes
pravinpushkar 3be1576
Removed DaprPreviewClientProxy and updated example README
pravinpushkar 97d4fa0
Adding validate workflow for cofiguration api example
pravinpushkar b837bc8
fixing example autovalidation and code coverage
pravinpushkar 660744d
Fixing autovalidation and removing getAllConfiguration
pravinpushkar 6e8a4ec
Fixing review comments
pravinpushkar b353a16
Add regex header checkstyle.
artursouza 4a916f7
Fix headers and add javadocs to some.
artursouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ^/\*$ | ||
| ^ \* Copyright \d\d\d\d The Dapr Authors$ | ||
| ^ \* Licensed under the Apache License, Version 2.0 \(the "License"\)\;$ | ||
| ^ \* you may not use this file except in compliance with the License\.$ | ||
| ^ \* You may obtain a copy of the License at$ | ||
| ^ \* http://www.apache.org/licenses/LICENSE-2\.0$ | ||
| ^ \* Unless required by applicable law or agreed to in writing, software$ | ||
| ^ \* distributed under the License is distributed on an "AS IS" BASIS,$ | ||
| ^ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.$ | ||
| ^ \* See the License for the specific language governing permissions and$ | ||
| ^limitations under the License\.$ | ||
| ^\*/$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| apiVersion: dapr.io/v1alpha1 | ||
| kind: Component | ||
| metadata: | ||
| name: configstore | ||
| spec: | ||
| type: configuration.redis | ||
| version: v1 | ||
| metadata: | ||
| - name: redisHost | ||
| value: localhost:6379 | ||
| - name: redisPassword | ||
| value: "" |
164 changes: 164 additions & 0 deletions
164
examples/src/main/java/io/dapr/examples/configuration/grpc/ConfigurationClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* | ||
| * Copyright 2022 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.examples.configuration.grpc; | ||
|
|
||
| import io.dapr.client.DaprClientBuilder; | ||
| import io.dapr.client.DaprPreviewClient; | ||
| import io.dapr.client.domain.ConfigurationItem; | ||
| import io.dapr.client.domain.GetConfigurationRequest; | ||
| import io.dapr.client.domain.SubscribeConfigurationRequest; | ||
| import reactor.core.Disposable; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| public class ConfigurationClient { | ||
|
|
||
| private static final String CONFIG_STORE_NAME = "configstore"; | ||
|
|
||
| private static final List<String> keys = new ArrayList<>(Arrays.asList("myconfig1", "myconfig3", "myconfig2")); | ||
|
|
||
| /** | ||
| * Executes various methods to check the different apis. | ||
| * @param args arguments | ||
| * @throws Exception throws Exception | ||
| */ | ||
| public static void main(String[] args) throws Exception { | ||
| try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) { | ||
| System.out.println("Using preview client..."); | ||
| getConfigurationForaSingleKey(client); | ||
| getConfigurationsUsingVarargs(client); | ||
| getConfigurations(client); | ||
| subscribeConfigurationRequestWithSubscribe(client); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets configuration for a single key. | ||
| * | ||
| * @param client DaprPreviewClient object | ||
| */ | ||
| public static void getConfigurationForaSingleKey(DaprPreviewClient client) { | ||
| System.out.println("*******trying to retrieve configuration given a single key********"); | ||
| try { | ||
| Mono<ConfigurationItem> item = client.getConfiguration(CONFIG_STORE_NAME, keys.get(0)); | ||
| System.out.println("Value ->" + item.block().getValue() + " key ->" + item.block().getKey()); | ||
| } catch (Exception ex) { | ||
| System.out.println(ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets configurations for varibale no. of arguments. | ||
| * | ||
| * @param client DaprPreviewClient object | ||
| */ | ||
| public static void getConfigurationsUsingVarargs(DaprPreviewClient client) { | ||
| System.out.println("*******trying to retrieve configurations for a variable no. of keys********"); | ||
| try { | ||
| Mono<List<ConfigurationItem>> items = | ||
| client.getConfiguration(CONFIG_STORE_NAME, "myconfig1", "myconfig3"); | ||
| items.block().forEach(ConfigurationClient::print); | ||
| } catch (Exception ex) { | ||
| System.out.println(ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets configurations for a list of keys. | ||
| * | ||
| * @param client DaprPreviewClient object | ||
| */ | ||
| public static void getConfigurations(DaprPreviewClient client) { | ||
| System.out.println("*******trying to retrieve configurations for a list of keys********"); | ||
| List<String> keys = new ArrayList<>(); | ||
| keys.add("myconfig1"); | ||
| keys.add("myconfig2"); | ||
| keys.add("myconfig3"); | ||
| GetConfigurationRequest req = new GetConfigurationRequest(CONFIG_STORE_NAME, keys); | ||
| try { | ||
| Mono<List<ConfigurationItem>> items = client.getConfiguration(req); | ||
| items.block().forEach(ConfigurationClient::print); | ||
| } catch (Exception ex) { | ||
| System.out.println(ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Subscribe to a list of keys.Optional to above iterator way of retrieving the changes | ||
| * | ||
| * @param client DaprPreviewClient object | ||
| */ | ||
| public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient client) { | ||
| System.out.println("*****Subscribing to keys using subscribe method: " + keys.toString() + " *****"); | ||
| AtomicReference<Disposable> disposableAtomicReference = new AtomicReference<>(); | ||
| SubscribeConfigurationRequest req = new SubscribeConfigurationRequest(CONFIG_STORE_NAME, keys); | ||
| Runnable subscribeTask = () -> { | ||
| Flux<List<ConfigurationItem>> outFlux = client.subscribeToConfiguration(req); | ||
| disposableAtomicReference.set(outFlux | ||
| .subscribe( | ||
| cis -> cis.forEach(ConfigurationClient::print) | ||
| )); | ||
| }; | ||
| new Thread(subscribeTask).start(); | ||
| try { | ||
| // To ensure that subscribeThread gets scheduled | ||
| Thread.sleep(0); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| Runnable updateKeys = () -> { | ||
| int i = 1; | ||
| while (i <= 3) { | ||
| executeDockerCommand(i); | ||
| i++; | ||
| } | ||
| }; | ||
| new Thread(updateKeys).start(); | ||
| try { | ||
| // To ensure main thread does not die before outFlux subscribe gets called | ||
| Thread.sleep(10000); | ||
| disposableAtomicReference.get().dispose(); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| private static void print(ConfigurationItem item) { | ||
| System.out.println(item.getValue() + " : key ->" + item.getKey()); | ||
| } | ||
|
|
||
| private static void executeDockerCommand(int postfix) { | ||
| String[] command = new String[] { | ||
| "docker", "exec", "dapr_redis", "redis-cli", | ||
| "SET", | ||
| "myconfig" + postfix, "update_myconfigvalue" + postfix + "||2" | ||
| }; | ||
| ProcessBuilder processBuilder = new ProcessBuilder(command); | ||
| Process process = null; | ||
| try { | ||
| process = processBuilder.start(); | ||
| process.waitFor(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } |
113 changes: 113 additions & 0 deletions
113
examples/src/main/java/io/dapr/examples/configuration/grpc/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| ## Retrieve Configurations via Configuration API | ||
|
|
||
| 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/) | ||
| **This API is available in Preview Mode**. | ||
|
|
||
| ### Using the ConfigurationAPI | ||
|
|
||
| The java SDK exposes several methods for this - | ||
| * `client.getConfiguration(...)` for getting a configuration for a single/multiple keys. | ||
| * `client.subscribeToConfigurations(...)` for subscribing to a list of keys for any change. | ||
|
|
||
| ## 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/13/). | ||
| * [Apache Maven](https://maven.apache.org/install.html) version 3.x. | ||
|
|
||
| ### Checking out the code | ||
|
|
||
| Clone this repository: | ||
|
|
||
| ```sh | ||
| git clone https://github.com/dapr/java-sdk.git | ||
| cd java-sdk | ||
| ``` | ||
|
|
||
| Then build the Maven project: | ||
|
|
||
| ```sh | ||
| # make sure you are in the `java-sdk` directory. | ||
| mvn install | ||
| ``` | ||
| ## Store few dummy configurations in configurationstore | ||
| <!-- STEP | ||
| name: Set configuration value | ||
| expected_stdout_lines: | ||
| - "OK" | ||
| timeout_seconds: 20 | ||
| --> | ||
|
|
||
| ```bash | ||
| docker exec dapr_redis redis-cli MSET myconfig1 "val1||1" myconfig2 "val2||1" myconfig3 "val3||1" | ||
| ``` | ||
| <!-- END_STEP --> | ||
|
|
||
| ### Running the example | ||
|
|
||
| Get into the examples' directory: | ||
| ```sh | ||
| cd examples | ||
| ``` | ||
|
|
||
| Use the following command to run this example- | ||
|
|
||
| <!-- STEP | ||
| name: Run ConfigurationClient example | ||
| expected_stdout_lines: | ||
| - "== APP == Using preview client..." | ||
| - "== APP == *******trying to retrieve configuration given a single key********" | ||
| - "== APP == Value ->val1 key ->myconfig1" | ||
| - "== APP == *******trying to retrieve configurations for a variable no. of keys********" | ||
| - "== APP == val1 : key ->myconfig1" | ||
| - "== APP == val3 : key ->myconfig3" | ||
| - "== APP == *******trying to retrieve configurations for a list of keys********" | ||
| - "== APP == val1 : key ->myconfig1" | ||
| - "== APP == val2 : key ->myconfig2" | ||
| - "== APP == val3 : key ->myconfig3" | ||
| - "== APP == *****Subscribing to keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****" | ||
| - "== APP == update_myconfigvalue1 : key ->myconfig1" | ||
| - "== APP == update_myconfigvalue2 : key ->myconfig2" | ||
| - "== APP == update_myconfigvalue3 : key ->myconfig3" | ||
| background: true | ||
| sleep: 5 | ||
| --> | ||
|
|
||
| ```bash | ||
|
pravinpushkar marked this conversation as resolved.
|
||
| 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 | ||
| ``` | ||
|
|
||
| <!-- END_STEP --> | ||
|
|
||
| ### Sample output | ||
| ``` | ||
| == APP == Using preview client... | ||
| == APP == *******trying to retrieve configuration given a single key******** | ||
| == APP == Value ->val1 key ->myconfig1 | ||
| == APP == *******trying to retrieve configurations for a variable no. of keys******** | ||
| == APP == val1 : key ->myconfig1 | ||
| == APP == val3 : key ->myconfig3 | ||
| == APP == *******trying to retrieve configurations for a list of keys******** | ||
| == APP == val1 : key ->myconfig1 | ||
| == APP == val2 : key ->myconfig2 | ||
| == APP == val3 : key ->myconfig3 | ||
| == APP == *****Subscribing to keys using subscribe method: [myconfig1, myconfig3, myconfig2] ***** | ||
| == APP == update_myconfigvalue1 : key ->myconfig1 | ||
| == APP == update_myconfigvalue2 : key ->myconfig2 | ||
| == APP == update_myconfigvalue3 : key ->myconfig3 | ||
|
|
||
| ``` | ||
| ### Cleanup | ||
|
|
||
| To stop the app, run (or press CTRL+C): | ||
|
|
||
| <!-- STEP | ||
| name: Cleanup | ||
| --> | ||
|
|
||
| ```bash | ||
| dapr stop --app-id configgrpc | ||
| ``` | ||
|
|
||
| <!-- END_STEP --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| apiVersion: dapr.io/v1alpha1 | ||
| kind: Component | ||
| metadata: | ||
| name: redisconfigstore | ||
| spec: | ||
| type: configuration.redis | ||
| version: v1 | ||
| metadata: | ||
| - name: redisHost | ||
| value: localhost:6379 | ||
| - name: redisPassword | ||
| value: "" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.