It should work if you deploy it with gcloud beta app deploy target/SNAPSHOT
.
I prepared and uploaded a sample in Github.
How I did it in a new project:
- Enabled App Engine, selected region us-central (corresponds to us-central1)
- Created Memorystore instance, in region us-central1
- Created VPC Connector, in region us-central1 (At the moment no other region can be selected, so both App Engine and Memorystore have to be in us-central1)
- Added the VPC connector in
appengine-web.xml
:
<vpc-access-connector>
<name>projects/PROJECT_ID/locations/us-central1/connectors/CONNECTOR_NAME</name>
</vpc-access-connector>
- Modified
pom.xml
, adding the following dependency:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
- Modified the
servlet.java
file:
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import redis.clients.jedis.Jedis;
@WebServlet(
name = "Redis",
description = "Redis: Connect to Redis",
urlPatterns = "/redis"
)
public class RedisServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String s;
try{
Jedis jedis = new Jedis("10.0.0.4");
jedis.set("myKey", "It's alive");
s = "Life test: "+ jedis.get("myKey");
}catch(Exception e){s = "Couldn't connect "; e.printStackTrace();}
resp.getWriter().write(s);
}
}
- Ran the following to package and deploy:
mvn package
(This will create a "target" folder)
gcloud beta app deploy target/ARTIFACT_ID-1.0-SNAPSHOT
Note that it's still in BETA and it might not work very reliably.