mirror of https://github.com/dapr/docs.git
Merge pull request #2120 from amulyavarote/feature/configAPI_docs
Documentation of configuration API for python sdk
This commit is contained in:
commit
fafad633c5
|
@ -11,6 +11,12 @@ This HowTo uses the Redis configuration store component as an example on how to
|
||||||
|
|
||||||
*This API is currently in `Alpha` state and only available on gRPC. An HTTP1.1 supported version with this URL syntax `/v1.0/configuration` will be available before the API is certified into `Stable` state.*
|
*This API is currently in `Alpha` state and only available on gRPC. An HTTP1.1 supported version with this URL syntax `/v1.0/configuration` will be available before the API is certified into `Stable` state.*
|
||||||
|
|
||||||
|
## Example:
|
||||||
|
|
||||||
|
The below code examples loosely describe an application that processes orders. In the examples, there is an order processing service which has a Dapr sidecar. The order processing service uses Dapr to retrieve the configuration from a Redis configuration store.
|
||||||
|
|
||||||
|
<img src="/images/building-block-configuration-example.png" width=1000 alt="Diagram showing get configuration of example service">
|
||||||
|
|
||||||
## Step 1: Create a configuration item in store
|
## Step 1: Create a configuration item in store
|
||||||
|
|
||||||
First, create a configuration item in a supported configuration store. This can be a simple key-value item, with any key of your choice. For this example, we'll use the Redis configuration store component.
|
First, create a configuration item in a supported configuration store. This can be a simple key-value item, with any key of your choice. For this example, we'll use the Redis configuration store component.
|
||||||
|
@ -31,7 +37,7 @@ redis-cli -p 6379
|
||||||
Save a configuration item:
|
Save a configuration item:
|
||||||
|
|
||||||
```
|
```
|
||||||
set myconfig "wookie"
|
MSET orderId1 "101||1" orderId2 "102||1"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Configure a Dapr configuration store
|
### Configure a Dapr configuration store
|
||||||
|
@ -42,7 +48,7 @@ Save the following component file, for example to the [default components folder
|
||||||
apiVersion: dapr.io/v1alpha1
|
apiVersion: dapr.io/v1alpha1
|
||||||
kind: Component
|
kind: Component
|
||||||
metadata:
|
metadata:
|
||||||
name: redisconfigstore
|
name: configstore
|
||||||
spec:
|
spec:
|
||||||
type: configuration.redis
|
type: configuration.redis
|
||||||
metadata:
|
metadata:
|
||||||
|
@ -52,37 +58,100 @@ spec:
|
||||||
value: <PASSWORD>
|
value: <PASSWORD>
|
||||||
```
|
```
|
||||||
|
|
||||||
### Get configuration items using gRPC API
|
### Get configuration items using Dapr SDKs
|
||||||
|
|
||||||
Using your [favorite language](https://grpc.io/docs/languages/), create a Dapr gRPC client from the [Dapr proto](https://github.com/dapr/dapr/blob/master/dapr/proto/runtime/v1/dapr.proto). The following examples show Java, C#, Python and Javascript clients.
|
{{< tabs Dotnet Java Python>}}
|
||||||
|
|
||||||
{{< tabs Java Dotnet Python Javascript >}}
|
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```java
|
```csharp
|
||||||
|
//dependencies
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Dapr.Client;
|
||||||
|
|
||||||
Dapr.ServiceBlockingStub stub = Dapr.newBlockingStub(channel);
|
//code
|
||||||
stub.GetConfigurationAlpha1(new GetConfigurationRequest{ StoreName = "redisconfigstore", Keys = new String[]{"myconfig"} });
|
namespace ConfigurationApi
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
private static readonly string CONFIG_STORE_NAME = "configstore";
|
||||||
|
|
||||||
|
[Obsolete]
|
||||||
|
public static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
using var client = new DaprClientBuilder().Build();
|
||||||
|
var configuration = await client.GetConfiguration(CONFIG_STORE_NAME, new List<string>() { "orderId1", "orderId2" });
|
||||||
|
Console.WriteLine($"Got key=\n{configuration[0].Key} -> {configuration[0].Value}\n{configuration[1].Key} -> {configuration[1].Value}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Navigate to the directory containing the above code and run the following command to launch the application along with a Dapr sidecar:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dapr run --app-id orderprocessing --components-path ./components -- dotnet run
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```csharp
|
```java
|
||||||
|
//dependencies
|
||||||
|
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.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
var call = client.GetConfigurationAlpha1(new GetConfigurationRequest { StoreName = "redisconfigstore", Keys = new String[]{"myconfig"} });
|
//code
|
||||||
|
private static final String CONFIG_STORE_NAME = "configstore";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
|
||||||
|
List<String> keys = new ArrayList<>();
|
||||||
|
keys.add("orderId1");
|
||||||
|
keys.add("orderId2");
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Navigate to the directory containing the above code and run the following command to launch the application along with a Dapr sidecar:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dapr run --app-id orderprocessing --components-path ./components mvn spring-boot:run
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{% codetab %}}
|
{{% codetab %}}
|
||||||
```python
|
```python
|
||||||
response = stub.GetConfigurationAlpha1(request={ StoreName: 'redisconfigstore', Keys = ['myconfig'] })
|
#dependencies
|
||||||
|
from dapr.clients import DaprClient
|
||||||
|
#code
|
||||||
|
with DaprClient() as d:
|
||||||
|
CONFIG_STORE_NAME = 'configstore'
|
||||||
|
keys = ['orderId1', 'orderId2']
|
||||||
|
#Startup time for dapr
|
||||||
|
d.wait(20)
|
||||||
|
configuration = d.get_configuration(store_name=CONFIG_STORE_NAME, keys=[keys], config_metadata={})
|
||||||
|
print(f"Got key={configuration.items[0].key} value={configuration.items[0].value} version={configuration.items[0].version}")
|
||||||
```
|
```
|
||||||
{{% /codetab %}}
|
|
||||||
|
|
||||||
{{% codetab %}}
|
Navigate to the directory containing the above code and run the following command to launch the application along with a Dapr sidecar:
|
||||||
```javascript
|
|
||||||
client.GetConfigurationAlpha1({ StoreName: 'redisconfigstore', Keys = ['myconfig'] })
|
```bash
|
||||||
|
dapr run --app-id orderprocessing --components-path ./components python3 OrderProcessingService.py
|
||||||
```
|
```
|
||||||
|
|
||||||
{{% /codetab %}}
|
{{% /codetab %}}
|
||||||
|
|
||||||
{{< /tabs >}}
|
{{< /tabs >}}
|
||||||
|
@ -127,4 +196,4 @@ message UnSubscribeConfigurationRequest {
|
||||||
Using this unsubscribe method, you can stop watching configuration update events. Dapr locates the subscription stream based on the `store_name` and any optional keys supplied and closes it.
|
Using this unsubscribe method, you can stop watching configuration update events. Dapr locates the subscription stream based on the `store_name` and any optional keys supplied and closes it.
|
||||||
|
|
||||||
## Next steps
|
## Next steps
|
||||||
* Read [configuration API overview]({{< ref configuration-api-overview.md >}})
|
* Read [configuration API overview]({{< ref configuration-api-overview.md >}})
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 113 KiB |
Loading…
Reference in New Issue