본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Quickstart: Configuration

원문 보기 위키 갱신

Dapr의 Configuration 빌딩 블록으로 시작해 봐요.

Dapr의 Configuration 빌딩 블록을 살펴볼게요. 구성 항목(configuration item)은 종종 동적이며, 이를 소비하는 어플리케이션의 요구와 밀접하게 연결돼 있어요. 구성 항목은 다음과 같은 구성 데이터를 담고 있는 키/값 쌍이에요.

  • App ids
  • 파티션 키(Partition keys)
  • 데이터베이스 이름 등

이 quickstart에서는 Configuration API를 활용하는 order-processor 마이크로서비스를 실행할 거예요. 서비스는 다음을 해요.

  • 구성 저장소에서 구성 항목을 가져와요.
  • 구성 업데이트를 구독해요.

Quickstart를 진행하기 전에 선호하는 언어별 Dapr SDK를 선택해요.

  • Python
  • JavaScript
  • .NET
  • Java
  • Go

출처: Dapr 문서

본문

사전 조건 (Pre-requisites)

이 예제에 필요한 것들:

  • Dapr CLI와 초기화된 환경.

  • Python 3.7+ 설치.

  • Docker Desktop.

1단계: 환경 설정하기

Quickstarts 저장소에 제공된 샘플을 클론해요.

git clone https://github.com/dapr/quickstarts.git

클론이 끝나면 새 터미널을 열고 다음 명령을 실행해서 구성 항목 orderId1과 orderId2의 값을 설정해요.

docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

2단계: order-processor 서비스 실행하기

Quickstarts 클론 디렉터리 루트에서 order-processor 디렉터리로 이동해요.

cd configuration/python/sdk/order-processor

의존성을 설치해요.

pip3 install -r requirements.txt

order-processor 서비스를 Dapr sidecar와 함께 실행해요.

dapr run --app-id order-processor --resources-path ../../../components/ --app-port 6001 -- python3 app.py

참고: Windows에는 Python3.exe가 정의되어 있지 않으므로 python3 app.py 대신 python app.py를 사용해야 할 수 있어요.

예상 출력:

== APP == Configuration for orderId1 : value: "101"
== APP ==
== APP == Configuration for orderId2 : value: "102"
== APP ==
== APP == App unsubscribed from config changes

(선택) 3단계: 구성 항목 값 업데이트하기

앱이 구독을 해지한 후, 구성 항목 값을 업데이트해 봐요. 다음 명령으로 orderId1과 orderId2 값을 변경해요.

docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

order-processor 서비스를 다시 실행해요.

dapr run --app-id order-processor --resources-path ../../../components/ --app-port 6001 -- python3 app.py

참고: Windows에는 Python3.exe가 정의되어 있지 않으므로 python3 app.py 대신 python app.py를 사용해야 할 수 있어요.

앱은 업데이트된 구성 값을 반환해요.

== APP == Configuration for orderId1 : value: "103"
== APP ==
== APP == Configuration for orderId2 : value: "104"
== APP ==

order-processor 서비스

order-processor 서비스에는 다음에 대한 코드가 포함돼 있어요.

  • 구성 저장소에서 구성 항목 가져오기
  • 구성 업데이트 구독 (앞서 CLI에서 수행한 것)
  • 20초간 활동이 없으면 구성 업데이트 구독을 해지하고 앱 종료.

구성 항목 가져오기:

# Get config items from the config store
for config_item in CONFIGURATION_ITEMS:
config = client.get_configuration(store_name=DAPR_CONFIGURATION_STORE, keys=[config_item], config_metadata={})
print(f"Configuration for {config_item} : {config.items[config_item]}", flush=True)

구성 업데이트 구독:

# Subscribe for configuration changes
configuration = await client.subscribe_configuration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS)

구성 업데이트 구독 해지 및 어플리케이션 종료:

# Unsubscribe from configuration updates
unsubscribed = True
for config_item in CONFIGURATION_ITEMS:
unsub_item = client.unsubscribe_configuration(DAPR_CONFIGURATION_STORE, config_item)
#...
if unsubscribed == True:
print("App unsubscribed from config changes", flush=True)

사전 조건 (Pre-requisites)

이 예제에 필요한 것들:

  • Dapr CLI와 초기화된 환경.

  • 최신 Node.js 설치.

  • Docker Desktop.

1단계: 환경 설정하기

Quickstarts 저장소에 제공된 샘플을 클론해요.

git clone https://github.com/dapr/quickstarts.git

클론이 끝나면 새 터미널을 열고 다음 명령을 실행해서 구성 항목 orderId1과 orderId2의 값을 설정해요.

docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

2단계: order-processor 서비스 실행하기

Quickstarts 클론 디렉터리 루트에서 order-processor 디렉터리로 이동해요.

cd configuration/javascript/sdk/order-processor

의존성을 설치해요.

npm install

order-processor 서비스를 Dapr sidecar와 함께 실행해요.

dapr run --app-id order-processor --resources-path ../../../components/ --app-protocol grpc --dapr-grpc-port 3500 -- node index.js

예상 출력:

== APP == Configuration for orderId1: {"key":"orderId1","value":"101","version":"","metadata":{}}
== APP == Configuration for orderId2: {"key":"orderId2","value":"102","version":"","metadata":{}}
== APP == App unsubscribed to config changes

(선택) 3단계: 구성 항목 값 업데이트하기

앱이 구독을 해지한 후, 구성 항목 값을 업데이트해 봐요. 다음 명령으로 orderId1과 orderId2 값을 변경해요.

docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

order-processor 서비스를 다시 실행해요.

dapr run --app-id order-processor --resources-path ../../../components/ --app-protocol grpc --dapr-grpc-port 3500 -- node index.js

앱은 업데이트된 구성 값을 반환해요.

== APP == Configuration for orderId1: {"key":"orderId1","value":"103","version":"","metadata":{}}
== APP == Configuration for orderId2: {"key":"orderId2","value":"104","version":"","metadata":{}}

order-processor 서비스

order-processor 서비스에는 다음에 대한 코드가 포함돼 있어요.

  • 구성 저장소에서 구성 항목 가져오기
  • 구성 업데이트 구독 (앞서 CLI에서 수행한 것)
  • 20초간 활동이 없으면 구성 업데이트 구독을 해지하고 앱 종료.

구성 항목 가져오기:

// Get config items from the config store
//...
const config = await client.configuration.get(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
Object.keys(config.items).forEach((key) => {
console.log("Configuration for " + key + ":", JSON.stringify(config.items[key]));
});

구성 업데이트 구독:

// Subscribe to config updates
try {
const stream = await client.configuration.subscribeWithKeys(
DAPR_CONFIGURATION_STORE,
CONFIGURATION_ITEMS,
(config) => {
console.log("Configuration update", JSON.stringify(config.items));
}
);

구성 업데이트 구독 해지 및 어플리케이션 종료:

// Unsubscribe to config updates and exit app after 20 seconds
setTimeout(() => {
stream.stop();
console.log("App unsubscribed to config changes");
process.exit(0);
},

사전 조건 (Pre-requisites)

이 예제에 필요한 것들:

  • Dapr CLI와 초기화된 환경.

  • Docker Desktop.

  • .NET 6, .NET 8 또는 .NET 9 설치.

참고: .NET 6은 이 릴리스에서 Dapr .NET SDK 패키지가 지원하는 최소 .NET 버전이에요. Dapr v1.16 이상에서는 .NET 8과 .NET 9만 지원됩니다.

1단계: 환경 설정하기

Quickstarts 저장소에 제공된 샘플을 클론해요.

git clone https://github.com/dapr/quickstarts.git

클론이 끝나면 새 터미널을 열고 다음 명령을 실행해서 구성 항목 orderId1과 orderId2의 값을 설정해요.

docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

2단계: order-processor 서비스 실행하기

Quickstarts 클론 디렉터리 루트에서 order-processor 디렉터리로 이동해요.

cd configuration/csharp/sdk/order-processor

NuGet 패키지를 불러와요.

dotnet restore
dotnet build

order-processor 서비스를 Dapr sidecar와 함께 실행해요.

dapr run --app-id order-processor-http --resources-path ../../../components/ --app-port 7001 -- dotnet run --project .

예상 출력:

== APP == Configuration for orderId1: {"Value":"101","Version":"","Metadata":{}}
== APP == Configuration for orderId2: {"Value":"102","Version":"","Metadata":{}}
== APP == App unsubscribed from config changes

(선택) 3단계: 구성 항목 값 업데이트하기

앱이 구독을 해지한 후, 구성 항목 값을 업데이트해 봐요. 다음 명령으로 orderId1과 orderId2 값을 변경해요.

docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

order-processor 서비스를 다시 실행해요.

dapr run --app-id order-processor-http --resources-path ../../../components/ --app-port 7001 -- dotnet run --project .

앱은 업데이트된 구성 값을 반환해요.

== APP == Configuration for orderId1: {"Value":"103","Version":"","Metadata":{}}
== APP == Configuration for orderId2: {"Value":"104","Version":"","Metadata":{}}

order-processor 서비스

order-processor 서비스에는 다음에 대한 코드가 포함돼 있어요.

  • 구성 저장소에서 구성 항목 가져오기
  • 구성 업데이트 구독 (앞서 CLI에서 수행한 것)
  • 20초간 활동이 없으면 구성 업데이트 구독을 해지하고 앱 종료.

구성 항목 가져오기:

// Get config from configuration store
GetConfigurationResponse config = await client.GetConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);
foreach (var item in config.Items)
{
var cfg = System.Text.Json.JsonSerializer.Serialize(item.Value);
Console.WriteLine("Configuration for " + item.Key + ": " + cfg);
}

구성 업데이트 구독:

// Subscribe to config updates
SubscribeConfigurationResponse subscribe = await client.SubscribeConfiguration(DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS);

구성 업데이트 구독 해지 및 어플리케이션 종료:

// Unsubscribe to config updates and exit the app
try
{
client.UnsubscribeConfiguration(DAPR_CONFIGURATION_STORE, subscriptionId);
Console.WriteLine("App unsubscribed from config changes");
Environment.Exit(0);
}

사전 조건 (Pre-requisites)

이 예제에 필요한 것들:

  • Dapr CLI와 초기화된 환경.

  • Java JDK 17 (이상): Oracle JDK, 또는

  • OpenJDK

  • Apache Maven, 버전 3.x.

  • Docker Desktop.

1단계: 환경 설정하기

Quickstarts 저장소에 제공된 샘플을 클론해요.

git clone https://github.com/dapr/quickstarts.git

클론이 끝나면 새 터미널을 열고 다음 명령을 실행해서 구성 항목 orderId1과 orderId2의 값을 설정해요.

docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

2단계: order-processor 서비스 실행하기

Quickstarts 클론 디렉터리 루트에서 order-processor 디렉터리로 이동해요.

cd configuration/java/sdk/order-processor

의존성을 설치해요.

mvn clean install

order-processor 서비스를 Dapr sidecar와 함께 실행해요.

dapr run --app-id order-processor --resources-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

예상 출력:

== APP == Configuration for orderId1: {'value':'101'}
== APP == Configuration for orderId2: {'value':'102'}
== APP == App unsubscribed to config changes

(선택) 3단계: 구성 항목 값 업데이트하기

앱이 구독을 해지한 후, 구성 항목 값을 업데이트해 봐요. 다음 명령으로 orderId1과 orderId2 값을 변경해요.

docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

order-processor 서비스를 다시 실행해요.

dapr run --app-id order-processor --resources-path ../../../components -- java -jar target/OrderProcessingService-0.0.1-SNAPSHOT.jar

앱은 업데이트된 구성 값을 반환해요.

== APP == Configuration for orderId1: {'value':'103'}
== APP == Configuration for orderId2: {'value':'104'}

order-processor 서비스

order-processor 서비스에는 다음에 대한 코드가 포함돼 있어요.

  • 구성 저장소에서 구성 항목 가져오기
  • 구성 업데이트 구독 (앞서 CLI에서 수행한 것)
  • 20초간 활동이 없으면 구성 업데이트 구독을 해지하고 앱 종료.

구성 항목 가져오기:

// Get config items from the config store
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
for (String configurationItem : CONFIGURATION_ITEMS) {
ConfigurationItem item = client.getConfiguration(DAPR_CONFIGURATON_STORE, configurationItem).block();
System.out.println("Configuration for " + configurationItem + ": {'value':'" + item.getValue() + "'}");
}

구성 업데이트 구독:

// Subscribe for config changes
Flux<SubscribeConfigurationResponse> subscription = client.subscribeConfiguration(DAPR_CONFIGURATON_STORE,
CONFIGURATION_ITEMS.toArray(String[]::new));

구성 업데이트 구독 해지 및 어플리케이션 종료:

// Unsubscribe from config changes
UnsubscribeConfigurationResponse unsubscribe = client
.unsubscribeConfiguration(subscriptionId, DAPR_CONFIGURATON_STORE).block();
if (unsubscribe.getIsUnsubscribed()) {
System.out.println("App unsubscribed to config changes");
}

사전 조건 (Pre-requisites)

이 예제에 필요한 것들:

  • Dapr CLI와 초기화된 환경.

  • 최신 버전의 Go.

  • Docker Desktop.

1단계: 환경 설정하기

Quickstarts 저장소에 제공된 샘플을 클론해요.

git clone https://github.com/dapr/quickstarts.git

클론이 끝나면 새 터미널을 열고 다음 명령을 실행해서 구성 항목 orderId1과 orderId2의 값을 설정해요.

docker exec dapr_redis redis-cli MSET orderId1 "101" orderId2 "102"

2단계: order-processor 서비스 실행하기

Quickstarts 클론 디렉터리 루트에서 order-processor 디렉터리로 이동해요.

cd configuration/go/sdk/order-processor

order-processor 서비스를 Dapr sidecar와 함께 실행해요.

dapr run --app-id order-processor --app-port 6001 --resources-path ../../../components -- go run .

예상 출력:

== APP == Configuration for orderId1: {"Value":"101","Version":"","Metadata":null}
== APP == Configuration for orderId2: {"Value":"102","Version":"","Metadata":null}
== APP == dapr configuration subscribe finished.
== APP == App unsubscribed to config changes

(선택) 3단계: 구성 항목 값 업데이트하기

앱이 구독을 해지한 후, 구성 항목 값을 업데이트해 봐요. 다음 명령으로 orderId1과 orderId2 값을 변경해요.

docker exec dapr_redis redis-cli MSET orderId1 "103" orderId2 "104"

order-processor 서비스를 다시 실행해요.

dapr run --app-id order-processor --app-port 6001 --resources-path ../../../components -- go run .

앱은 업데이트된 구성 값을 반환해요.

== APP == Configuration for orderId1: {"Value":"103","Version":"","Metadata":null}
== APP == Configuration for orderId2: {"Value":"104","Version":"","Metadata":null}

order-processor 서비스

order-processor 서비스에는 다음에 대한 코드가 포함돼 있어요.

  • 구성 저장소에서 구성 항목 가져오기
  • 구성 업데이트 구독 (앞서 CLI에서 수행한 것)
  • 20초간 활동이 없으면 구성 업데이트 구독을 해지하고 앱 종료.

구성 항목 가져오기:

// Get config items from config store
for _, item := range CONFIGURATION_ITEMS {
config, err := client.GetConfigurationItem(ctx, DAPR_CONFIGURATION_STORE, item)
//...
c, _ := json.Marshal(config)
fmt.Println("Configuration for " + item + ": " + string(c))
}

구성 업데이트 구독:

// Subscribe for config changes
err = client.SubscribeConfigurationItems(ctx, DAPR_CONFIGURATION_STORE, CONFIGURATION_ITEMS, func(id string, config map[string]*dapr.ConfigurationItem) {
// First invocation when app subscribes to config changes only returns subscription id
if len(config) == 0 {
fmt.Println("App subscribed to config changes with subscription id: " + id)
subscriptionId = id
return
}
})

구성 업데이트 구독 해지 및 어플리케이션 종료:

// Unsubscribe to config updates and exit app after 20 seconds
select {
case <-ctx.Done():
err = client.UnsubscribeConfigurationItems(context.Background(), DAPR_CONFIGURATION_STORE, subscriptionId)
//...
{
fmt.Println("App unsubscribed to config changes")
}

데모

Configuration API quickstart를 시연하는 영상을 시청해 봐요.

의견을 들려주세요!

우린 Quickstart 예제를 계속 개선하고 있고 여러분의 피드백을 소중히 여겨요. 이 quickstart가 도움이 됐나요? 개선할 제안이 있나요? Discord Server에 참여해서 의견을 공유해 주세요.

다음 단계

  • SDK 대신 HTTP로 Dapr Configuration을 사용해요.

    • Python
    • JavaScript
    • .NET
    • Java
    • Go
  • Configuration 빌딩 블록에 대해 더 알아봐요

Dapr tutorials 탐색하기 >>

더 알아보기 (Learn more)