This document describes how to create an Actor(DemoActor) and invoke its methods on the client application.
-
The actor interface(demo_actor_interface.py). This contains the interface definition for the actor. Actor interfaces can be defined with any name. The interface defines the actor contract that is shared by the actor implementation and the clients calling the actor. Because client may depend on it, it typically makes sense to define it in an assembly that is separate from the actor implementation.
-
The actor service(demo_actor_service.py). This implements FastAPI service that is going to host the actor. It contains the implementation of the actor,
demo_actor.py
. An actor implementation is a class that derives from the base typeActor
and implements the interfaces defined indemo_actor_interface.py
. -
The actor service for flask(demo_actor_flask.py). This implements Flask web service that is going to host the actor.
-
The actor client(demo_actor_client.py) This contains the implementation of the actor client which calls DemoActor's method defined in Actor Interfaces.
You can install dapr SDK package using pip command:
pip3 install -r ./demo_actor/requirements.txt
Or, you can use the current repo:
cd <repo root>
pip3 install -r ./dev-requirement.txt
export PYTHONPATH=`pwd`
- Run Demo Actor service in new terminal window
$ cd ./demo_actor
$ dapr run --app-id demo-actor --app-port 3000 -- uvicorn --port 3000 demo_actor_service:app
...
== APP == Activate DemoActor actor!
== APP == 127.0.0.1 - - [01/Mar/2020 18:50:27] "POST /actors/DemoActor/1 HTTP/1.1" 200 -
== APP == 127.0.0.1 - - [01/Mar/2020 18:50:27] "PUT /actors/DemoActor/1/method/GetMyData HTTP/1.1" 200 -
== APP == 127.0.0.1 - - [01/Mar/2020 18:50:27] "PUT /actors/DemoActor/1/method/GetMyData HTTP/1.1" 200 -
== APP == 127.0.0.1 - - [01/Mar/2020 18:50:27] "PUT /actors/DemoActor/1/method/SetMyData HTTP/1.1" 200 -
== APP == 127.0.0.1 - - [01/Mar/2020 18:50:27] "PUT /actors/DemoActor/1/method/GetMyData HTTP/1.1" 200 -
...
For Flask web service
dapr run --app-id demo-actor --app-port 3000 python3 demo_actor_flask.py
- Run Demo client in new terminal window
$ cd ./demo_actor
# Run actor client
$ dapr run --app-id demo-client python3 demo_actor_client.py
...
== APP == b'{"data":"default","ts":"2020-03-02T02:50:27.381Z"}'
== APP == {'data': 'default', 'ts': datetime.datetime(2020, 3, 2, 2, 50, 27, 386000, tzinfo=tzutc())}
== APP == {'data': 'new_data', 'ts': datetime.datetime(2020, 3, 2, 2, 50, 27, 395000, tzinfo=tzutc())}
...
- Build and push docker image
$ cd examples/demo_actor/demo_actor
$ docker build -t [docker registry]/demo_actor:latest .
$ docker push [docker registry]/demo_actor:latest
For example, [docker registry] is docker hub account.
-
Follow these steps to create a Redis store.
-
Once your store is created, add the keys to the
redis.yaml
file in thedeploy
directory.Note: the
redis.yaml
file provided in this sample takes plain text secrets. In a production-grade application, follow secret management instructions to securely manage your secrets. -
Apply the
redis.yaml
file:kubectl apply -f ./deploy/redis.yaml
and observe that your state store was successfully configured!
component.dapr.io "statestore" configured
-
Update docker image location in
./deploy/demo_actor_client.yaml
and./deploy/demo_actor_service.yaml
-
Deploy actor service and clients
cd deploy
kubectl apply -f ./deploy/demo_actor_service.yml
kubectl apply -f ./deploy/demo_actor_client.yml