In this quickstart sample, you'll create a microservice with an output binding. You'll bind to commercetools, but note that there are a myriad of components that Dapr can bind to (see Dapr components).
This quickstart includes one microservice:
- Python microservice that utilizes an output binding
The binding connects to commercetools, allowing you to query or manipulate a commercetools projects using a provided GraphlQL query without having to know where the instance is hosted. Instead, connect through the sidecar using the Dapr API. See architecture diagram to see how the components interconnect locally:
Dapr allows us to deploy the same microservices from the local machines to Kubernetes. Correspondingly, this quickstart has instructions for deploying this project locally or in Kubernetes.
Clone this quickstarts repository to your local machine:
git clone https://github.com/harrykimpel/dapr-commercetools-sample.git
Next, run the Python microservice that uses output bindings
- Open a new CLI window and navigate to Python subscriber directory in your CLI:
cd pythonapp
- Install dependencies:
pip3 install requests
- Run Python quickstart app with Dapr:
dapr run --app-id bindings-pythonapp python3 app.py --components-path ../components
- Observe the Python logs, which show a successful output binding with commercetools:
[0m?[94;1m== APP == products found: 25
[0m?[94;1m== APP == - product name: miabag-handbag-14538-black
[0m?[94;1m== APP == - product name: miabag-handbag-15104-beige
[0m?[94;1m== APP == - product name: miabag-handtaschen-15117-pink
...
To cleanly stop the dapr microservices, run:
dapr stop --app-id bindings-pythonapp
- In your CLI window, in the bindings directory run:
kubectl apply -f ./deploy
This will deploy bindings-pythonapp microservices. It will also apply the commercetools bindings component configuration you set up in the last step.
Kubernetes deployments are asyncronous. This means you'll need to wait for the deployment to complete before moving on to the next steps. You can do so with the following command:
kubectl rollout status deploy/bindings-pythonapp
- Run
kubectl get pods
to see that pods were correctly provisioned.
Observe the Python app logs, which show a successful output binding with Kafka:
kubectl get pods
The output should look like this:
NAME READY STATUS RESTARTS AGE
bindings-pythonapp-644489969b-c8lg5 2/2 Running 0 4m9s
Look at the Python app logs by running:
kubectl logs --selector app=bindingspythonapp -c python --tail=-1
...
commercetoolsAPI: GraphQLQuery
HandleGraphQLQuery
...
Once you're done, you can spin down your Kubernetes resources by running:
kubectl delete -f ./deploy
This will spin down each resource defined by the .yaml files in the deploy
directory, including the commercetools component.
Now that you've run the quickstart locally and/or in Kubernetes, let's unpack how this all works. The app is composed of an output binding app:
Before looking at the application code, let's see the commercetools bindings component yamls(local, and Kubernetes), which specify API client settings
for commercetools connection.
See the howtos in references for the details on input and output bindings
This configuration yaml creates sample-project
component to set up commercetools an output binding through the commercetools project-key
project.
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: sample-project
namespace: commerce
spec:
type: bindings.commercetools
version: v1
metadata:
- name: Region
value: europe-west1
- name: Provider
value: gcp
- name: ProjectKey
value: project-key
- name: ClientID
value: client-id
- name: ClientSecret
value: client-secret
- name: Scopes
value: manage_project:project-key
Navigate to the pythonapp
directory and open app.py
, the code for the output bindings sample app. This sends POST request to Dapr http endpoint http://localhost:3500/v1.0/bindings/<output_bindings_name>
with the event payload every second. This app uses sample-project
bindings component name as <output_bindings_name>
. Then Dapr runtime will send the event to project-key
project which is specified in the above commercetools bindings component yaml.
dapr_url = "http://localhost:{}/v1.0/bindings/sample-project".format(dapr_port)
n = 0
while True:
n += 1
queryProductProjectionSearch = ''\
'query { '\
'productProjectionSearch(text: "bag", locale: "en", limit: 25) {'\
'total '\
'count '\
'results { '\
'id '\
'name (locale: "en") '\
'slug (locale: "en") '\
'}'\
'}'\
'}'
query = queryProductProjectionSearch
graphQLPayload = { "data": { "commercetoolsAPI": "GraphQLQuery", "query": query }, "operation": "create" }
print(payload, flush=True)
try:
response = requests.post(dapr_url, json=graphQLPayload)
dict = response.json()
print('products found: '+str(dict['productProjectionSearch']['count']), flush=True)
for key in dict['productProjectionSearch']['results']:
print ("- product name: "+key["slug"], flush=True)
except Exception as e:
print(e)
time.sleep(1)
- Learn more about bindings in the Dapr docs
- How to create an event-driven app using input bindings
- How to send events to external systems using Output Bindings
- Explore additional quickstarts.