App Engine applications must be on the same authorized network as the Redis instance in order to access it.
Setup
If you have already installed the Google Cloud CLI and have created a Redis instance, you can skip these steps.
Install the gcloud CLI and initialize:
gcloud init
Follow the Quickstart Guide to create a Redis instance. Take note of the zone, IP address, and port of the Redis instance.
Sample application
This sample HTTP server application establishes a connection to a Redis instance from an App Engine flexible environment instance.
Clone the repository for your desired programming language and navigate to the folder that contains the sample code:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples
cd golang-samples/memorystore/redis
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/memorystore/redis
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/memorystore/redis
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/memorystore/redis
This sample application increments a Redis counter every time the /
endpoint
is accessed.
Go
This application uses the
github.com/gomodule/redigo/redis
client. Install it by running the following command:
go get github.com/gomodule/redigo/redis
Java
This application is Jetty 3.1 servlet-based.
It uses the Jedis library:
The AppServletContextListener
class is used to create a long-lived
Redis connection pool:
The VisitCounterServlet
class is a web servlet that increments
a Redis counter:
Node.js
This application uses the redis
module.
Python
This application uses Flask
for web serving and the redis-py
package to communicate with the Redis instance.
Preparing the application for deployment
To access the Redis instance, the App Engine instance must be deployed on the same authorized network as the Redis instance, and you must provide your Redis instance's connection details. You can find your Redis instance's authorized network, IP address, and port by running the following command:
gcloud redis instances describe [INSTANCE_ID] --region [REGION]
Create an App Engine application.
Update the app's configuration to specify the IP address, port, and network of your Redis instance:
Go
Update the
gae_flex_deployment/app.yaml
file:See Configuring your App with app.yaml for more details.
Java
Update the
gae_flex_deployment/app.yaml
file to specify your Redis instance's network:And update the
src/main/resources/application.properties
file with your Redis instance's IP address and port:For more information about configuring your app, see Configuring your App with app.yaml.
Node.js
Update the
gae_flex_deployment/app.yaml
file:See Configuring your App with app.yaml for more details.
Python
Update the
gae_flex_deployment/app.yaml
file:See Configuring your App with app.yaml for more details.
Deploying the application to the App Engine flexible environment
To deploy the application:
Copy necessary configuration files into the source directory:
Go
Copy the
app.yaml
file into the source directory:cp gae_flex_deployment/app.yaml .
Java
Copy the
app.yaml
file into the source directory:mkdir -p src/main/appengine cp gae_flex_deployment/app.yaml src/main/appengine/
Node.js
Copy the
app.yaml
file into the source directory:cp gae_flex_deployment/app.yaml .
Python
Copy the
app.yaml
file into the source directory:cp gae_flex_deployment/app.yaml .
Run the deploy command:
Go
gcloud app deploy
This might take a few minutes.
Java
mvn appengine:deploy
This might take a few minutes.
Node.js
gcloud app deploy
This might take a few minutes.
Python
gcloud app deploy
This might take a few minutes.
After the deployment is complete, visit your app at the following URL,
replacing [PROJECT_ID]
with your Google Cloud project ID:
https://[PROJECT_ID].appspot.com
The count on your Redis instance increases each time the app is visited.