|
| 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 | + ``` |
0 commit comments