Spring Boot application in Google App Engine can't connect to Cloud SQL

霸气de小男生 提交于 2019-12-05 04:43:09

As documented here:

App Engine standard enviroments do not support connecting to the Cloud SQL instance using TCP. Your code should not try to access the instance using an IP address (such as 127.0.0.1 or 172.17.0.1) unless you have configured Serverless VPC Access.

First, you'll need to remove the beta_settings entry in your app.yaml file since:

  • it is intended for App Engine Flexible (and actually instruct the platform to spin up a sidecar container running the CloudSQL proxy, which you don't need with Socket Factory for JDBC)
  • the syntax you used with =tcp:5432 would actually instructs the CloudSQL proxy to connect via TCP port.

So your Base JDBC url should look like jdbc:postgresql://google/recruiters_wtf

Your full datasource url would be (note the removed =tcp:5432 part):

jdbc:postgresql://google/recruiters_wtf?cloudSqlInstance=recruiters-wtf:europe-west2:recruiters-wtf&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=${DB_USER}&password=${DB_PASS}

Finally, note that, as mentioned in the link I provided above:

The PostgreSQL standard requires the Unix socket to have a .s.PGSQL.5432 suffix in the socket path. Some libraries apply this suffix automatically, but others require you to specify the socket path as follows: /cloudsql/INSTANCE_CONNECTION_NAME/.s.PGSQL.5432.

I'm seeing that you are using DB_NAME:recruiters_wtf, shoudn't that be DB_NAME?

Attempt to use: spring.cloud.gcp.sql.database-name if not.

Also please make sure to deploy with beta

Let me know.

It's likely that one of two things is occurring that allow your k8s cluster to connect: 1. You are using a Private IP 2. You are using a Public IP, and added your k8s cluster to the whitelist

If you are using a private IP to connect, you'll have to configure Serverless VPC Access to allow App Engine to connect.

If you are using a Public IP, you won't be able to add App Engine to your whitelist because there is no guarantee what IP it connects from. Instead, I suggest using the Cloud SQL JDBC Socket Factory to authenticate your connections instead.

Ensure that the service account for your service has one of the following IAM roles:

Cloud SQL Client (preferred) Cloud SQL Editor Cloud SQL Admin Or, you can manually assign the following IAM permissions:

cloudsql.instances.connect cloudsql.instances.get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!