Search code examples
springgoogle-app-enginejdbcgoogle-cloud-platformgoogle-cloud-sql

Why App Engine can't connect to Cloud SQL with my Spring Boot app?


I'm trying to connect my App Engine application (in Java Spring Boot) with a Cloud SQL database. Deployment is working and app start but block on Connection to Cloud SQL :

main] c.g.cloud.sql.core.CoreSocketFactory     : First Cloud SQL connection, generating RSA key pair.
main] c.g.cloud.sql.core.CoreSocketFactory     : Connecting to Cloud SQL instance [test-api:europe-west1:test-db] via SSL socket.

// Nothing happens after that !

What I do ?

1) I create a user on my database with a username and a password (for example postgres and postgres).

2) I add this dependency to my project

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter</artifactId>
    <version>1.1.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
    <version>1.1.2.RELEASE</version>
</dependency>

3) I update application.properties with

# Gcp configuration
spring.cloud.gcp.sql.database-name=test
spring.cloud.gcp.sql.instance-connection-name=test-api:europe-west1:test-db

#Jdbc connection
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.url=jdbc:postgresql://google/test?cloudSqlInstance=test-api:europe-west1:test-db&socketFactory=com.google.cloud.sql.postgres.SocketFactory&useSSL=false&user=postgres&password=postgres

What is the problem ?

The problem is that nothing happens after Connecting to Cloud SQL instance log. (see before)

In the log of launch I can see that informations :

main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 11980 ms
main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Default POSTGRESQL JdbcUrl provider. Connecting to jdbc:postgresql://google/test?socketFactory=com.google.cloud.sql.postgres.SocketFactory&socketFactoryArg=test-api:europe-west1:test-db&useSSL=false with driver org.postgresql.Driver
main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : spring.datasource.url is specified. Not using generated Cloud SQL configuration
main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
main] c.g.cloud.sql.core.CoreSocketFactory     : First Cloud SQL connection, generating RSA key pair.
main] c.g.cloud.sql.core.CoreSocketFactory     : Connecting to Cloud SQL instance [test-api:europe-west1:test-db] via SSL socket.

Other curious point is that I specify &useSSL=false but app is connecting via SSL socket according to log.


Solution

  • This sounds very similar to what was happening in github/cloud-sql-jdbc-socket-factory #148. The issue was a dependency conflict, where an outdated version of guava as causing problems. Can you check what version of guava you have specified?

    Also, no need to be concerned about using &useSSL=false - the JDBC SF always uses SSL, so this is intentional.