学习

如何在 Heroku 上使用 Redis 构建基于 Python 的应用程序

Ajeet Raina
作者
Ajeet Raina, Redis 前开发者增长经理

步骤 1. 创建 Redis Cloud#

创建您的免费 Redis Cloud 账户。 点击此链接创建 Redis Cloud 订阅和数据库,如下所示:

保存数据库端点 URL 和密码以供将来参考。

步骤 2. 创建 Heroku 账户#

如果您是第一次使用 Heroku,请 通过此链接 创建您的新 Heroku 账户

步骤 3. 在您的系统上安装 Heroku CLI#

 brew install heroku

步骤 4. 登录 Heroku#

 heroku login
 heroku: Press any key to open up the browser to login or q to exit:
 Opening browser to https://cli-auth.heroku.com/auth/cli/browser/XXXXXXXXXXA
 Logging in... done
 Logged in as your_email_address

步骤 5. 将您的应用程序连接到 Redis Cloud#

为了演示,我们将使用一个 示例限速应用程序

克隆仓库#

 git clone https://github.com/redis-developer/basic-rate-limiting-demo-python

运行以下 CLI 命令,以获得一个包含简单应用程序和 package.json 文件的可用 Git 仓库。

$ heroku create
Creating app... done, ⬢ fast-reef-76278
https://fast-reef-76278.herokuapp.com/ | https://git.heroku.com/fast-reef-76278.git

步骤 6. 设置环境变量#

转到 Heroku 控制面板,点击“Settings”并在 Config Vars 下设置 REDIS_ENDPOINT_URI 和 REDIS_PASSWORD。参考步骤 1。

步骤 7. 部署您的代码#

Heroku 会为您的应用程序生成一个随机名称(此处为 fast-reef-76278),或者您可以传递参数来指定您自己的应用程序名称。现在部署您的代码

$ git push heroku
Enumerating objects: 512, done.
Counting objects: 100% (512/512), done.
Delta compression using up to 12 threads
Compressing objects: 100% (256/256), done.
Writing objects: 100% (512/512), 1.52 MiB | 660.00 KiB/s, done.
Total 512 (delta 244), reused 512 (delta 244)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Python app detected

emote: -----> Compressing...
remote:        Done: 59.3M
remote: -----> Launching...
remote:        Released v5
remote:        https://fast-reef-76278.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/fast-reef-76278.git
 * [new branch]      master -> master

步骤 8. 访问应用程序#

打开 https://fast-reef-76278.herokuapp.com/ 查看您的应用程序