Skip to content

Commit bf7aaed

Browse files
author
Vasilis Tsiolkas
committed
Adds example for grpc.aio App.
1 parent 3c31fce commit bf7aaed

File tree

7 files changed

+244
-0
lines changed

7 files changed

+244
-0
lines changed

examples/invoke-aio/Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM python:3.9-slim
2+
3+
WORKDIR /app
4+
5+
ADD requirements.txt .
6+
RUN pip install -r requirements.txt
7+
8+
COPY *.py /app/
9+
10+
CMD [ "python", "invoke-receiver.py" ]

examples/invoke-aio/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Example - Invoke a service
2+
3+
This example utilizes a receiver and a caller for the OnInvoke / Invoke functionality. It will create an aio gRPC server and bind the OnInvoke method to an async function, which gets called after a client sends a direct method invocation.
4+
5+
> **Note:** Make sure to use the latest proto bindings
6+
7+
## Pre-requisites
8+
9+
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started)
10+
- [Install Python 3.9+](https://www.python.org/downloads/)
11+
12+
## Install Dapr python-SDK
13+
14+
<!-- Our CI/CD pipeline automatically installs the correct version, so we can skip this step in the automation -->
15+
16+
```bash
17+
pip3 install dapr dapr-ext-grpc
18+
```
19+
20+
## Running in self-hosted mode
21+
22+
Run the following command in a terminal/command-prompt:
23+
24+
<!-- STEP
25+
name: Run receiver
26+
expected_stdout_lines:
27+
- '== APP == {"id": 1, "message": "hello world"}'
28+
- '== APP == {"id": 1, "message": "hello world"}'
29+
background: true
30+
sleep: 5
31+
-->
32+
33+
```bash
34+
# 1. Start Receiver (expose gRPC server receiver on port 50051)
35+
dapr run --app-id invoke-receiver --app-protocol grpc --app-port 50051 python3 invoke-receiver.py
36+
```
37+
38+
<!-- END_STEP -->
39+
40+
In another terminal/command prompt run:
41+
42+
<!-- STEP
43+
name: Run caller
44+
expected_stdout_lines:
45+
- '== APP == text/plain'
46+
- '== APP == INVOKE_RECEIVED'
47+
- '== APP == text/plain'
48+
- '== APP == INVOKE_RECEIVED'
49+
background: true
50+
sleep: 5
51+
-->
52+
53+
```bash
54+
# 2. Start Caller
55+
dapr run --app-id invoke-caller --app-protocol grpc --dapr-http-port 3500 python3 invoke-caller.py
56+
```
57+
58+
<!-- END_STEP -->
59+
60+
## Cleanup
61+
62+
<!-- STEP
63+
expected_stdout_lines:
64+
- '✅ app stopped successfully: invoke-caller'
65+
- '✅ app stopped successfully: invoke-receiver'
66+
name: Shutdown dapr
67+
-->
68+
69+
```bash
70+
dapr stop --app-id invoke-caller
71+
dapr stop --app-id invoke-receiver
72+
```
73+
74+
<!-- END_STEP -->
75+
76+
## Running in Kubernetes mode
77+
78+
1. Build docker image
79+
80+
```
81+
docker build -t [your registry]/invokesimple:latest .
82+
```
83+
84+
2. Push docker image
85+
86+
```
87+
docker push [your registry]/invokesimple:latest
88+
```
89+
90+
3. Edit image name to `[your registry]/invokesimple:latest` in deploy/*.yaml
91+
92+
4. Deploy applications
93+
94+
```
95+
kubectl apply -f ./deploy/
96+
```
97+
98+
5. See logs for the apps and sidecars
99+
100+
Logs for caller sidecar:
101+
```
102+
dapr logs -a invoke-caller -k
103+
```
104+
105+
Logs for caller app:
106+
```
107+
kubectl logs -l app="invokecaller" -c invokecaller
108+
```
109+
110+
Logs for receiver sidecar:
111+
```
112+
dapr logs -a invoke-receiver -k
113+
```
114+
115+
Logs for receiver app:
116+
```
117+
kubectl logs -l app="invokereceiver" -c invokereceiver
118+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2021 The Dapr Authors
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
apiVersion: apps/v1
13+
kind: Deployment
14+
metadata:
15+
name: invokecaller
16+
labels:
17+
app: invokecaller
18+
spec:
19+
replicas: 1
20+
selector:
21+
matchLabels:
22+
app: invokecaller
23+
template:
24+
metadata:
25+
labels:
26+
app: invokecaller
27+
annotations:
28+
dapr.io/enabled: "true"
29+
dapr.io/app-id: "invoke-caller"
30+
dapr.io/app-protocol: "grpc"
31+
spec:
32+
containers:
33+
- name: invokecaller
34+
image: invokesimple:latest # EDIT HERE: Replace the image name
35+
command: ["python"]
36+
args: ["/app/invoke-caller.py"]
37+
imagePullPolicy: Always
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2021 The Dapr Authors
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
apiVersion: apps/v1
13+
kind: Deployment
14+
metadata:
15+
name: invokereceiver
16+
labels:
17+
app: invokereceiver
18+
spec:
19+
replicas: 1
20+
selector:
21+
matchLabels:
22+
app: invokereceiver
23+
template:
24+
metadata:
25+
labels:
26+
app: invokereceiver
27+
annotations:
28+
dapr.io/enabled: "true"
29+
dapr.io/app-id: "invoke-receiver"
30+
dapr.io/app-protocol: "grpc"
31+
dapr.io/app-port: "50051"
32+
spec:
33+
containers:
34+
- name: invokereceiver
35+
image: invokesimple:latest # EDIT HERE: Replace the image name
36+
command: ["python"]
37+
args: ["/app/invoke-receiver.py"]
38+
ports:
39+
- containerPort: 3000
40+
imagePullPolicy: Always
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import json
2+
import time
3+
4+
from dapr.clients import DaprClient
5+
6+
with DaprClient() as d:
7+
req_data = {'id': 1, 'message': 'hello world'}
8+
9+
while True:
10+
# Create a typed message with content type and body
11+
resp = d.invoke_method(
12+
'invoke-receiver',
13+
'my-method',
14+
data=json.dumps(req_data),
15+
)
16+
17+
# Print the response
18+
print(resp.content_type, flush=True)
19+
print(resp.text(), flush=True)
20+
print(str(resp.status_code), flush=True)
21+
22+
time.sleep(2)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import asyncio
2+
from dapr.ext.grpc.aio import App, InvokeMethodRequest, InvokeMethodResponse
3+
4+
app = App()
5+
6+
7+
@app.method(name='my-method')
8+
async def mymethod(request: InvokeMethodRequest) -> InvokeMethodResponse:
9+
print(request.metadata, flush=True)
10+
print(request.text(), flush=True)
11+
12+
return InvokeMethodResponse(b'INVOKE_RECEIVED', 'text/plain; charset=UTF-8')
13+
14+
15+
asyncio.run(app.run(50051))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dapr-ext-grpc-dev >= 1.15.0.dev
2+
dapr-dev >= 1.15.0.dev

0 commit comments

Comments
 (0)