Reputation: 13
We deploy our app on Google app engine standard environment. We need to access Memorystore(redis) from our app engine.
Following the document, we create Serverless VPC access connector and configure the app engine:
<vpc-access-connector>
<name>projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME</name>
</vpc-access-connector>
and set the IAM permissions. But we still can not connect to redis instance at private IP like 10.0.0.4 using jedis:
Jedis jedis = new Jedis("10.0.0.4");
Upvotes: 1
Views: 834
Reputation: 1294
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:
appengine-web.xml
:<vpc-access-connector>
<name>projects/PROJECT_ID/locations/us-central1/connectors/CONNECTOR_NAME</name>
</vpc-access-connector>
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>
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);
}
}
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.
Upvotes: 2