Dapr Python SDK 与 Flask 集成

如何创建基于 Python FastAPI 的 Dapr 虚拟 actor

Dapr Python SDK 使用 flask-dapr 扩展与 Flask 进行集成。

安装

你可以通过下面的方式下载和安装 Dapr Flask 扩展模块:

  1. pip install flask-dapr

Note

  1. The development package will contain features and behavior that will be compatible with the pre-release version of the Dapr runtime. 在安装 <code>dapr-dev</code> 包之前,请务必卸载任何稳定版本的 Python SDK 扩展。
  1. pip install flask-dapr-dev

如何使用Dapr扩展来开发和运行Dapr应用程序

  1. from flask import Flask
  2. from flask_dapr.actor import DaprActor
  3. from dapr.conf import settings
  4. from demo_actor import DemoActor
  5. app = Flask(f'{DemoActor.__name__}Service')
  6. # Enable DaprActor Flask extension
  7. actor = DaprActor(app)
  8. # Register DemoActor
  9. actor.register_actor(DemoActor)
  10. # Setup method route
  11. @app.route('/GetMyData', methods=['GET'])
  12. def get_my_data():
  13. return {'message': 'myData'}, 200
  14. # Run application
  15. if __name__ == '__main__':
  16. app.run(port=settings.HTTP_APP_PORT)