1

I'm trying to deploy a Spring Boot Maven app using jdbc with a MySQL database to a flexible Google App Engine project with a 2nd generation db. All functions as expected when both app and db are run locally (spring-boot:run & XAMPP). All still functions normally when app is run locally and db is on Google CloudSQL. But when both entities are deployed to Google Cloud, my app gets a 'Connection Link Failure' when trying to access the database. The application is deploying fine and delivers hardcoded responses. The app and db are located in the same Google Cloud Project, and the CloudSQL bucket is set to allow all project files access. However I have also tried separating them into separate projects and authorizing the app's project in the db's list. No change. It's behaving exactly like when I attempt to access the db from an unauthorized IP address.

I have followed directions advising to add elements to the app.yaml file.

runtime: java
env: flex

runtime_config:  # Optional
jdk: openjdk8

handlers:
   - url: /.*
   script: this field is required, but ignored

# I've tried implementing env_variables and beta setting
# both separately and in tandem.
env_variables:
   MYSQL_DSN: mysql:unix_socket=/cloudsql/jdbc:mysql://12.345.67.890;dbname=exam
   MYSQL_USER: username
   MYSQL_PASSWORD: password

beta_settings:
   cloud_sql_instances: mytestproject1-123456:europe-west1:mydb  

manual_scaling:
   instances: 1

I have followed directions advising to add elements to the application.properties file.

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://12.345.67.890/exam
spring.datasource.username=username
spring.datasource.password=password

# I've also tried this url formulation:
#spring.datasource.url=jdbc:mysql://google/exam?cloudSqlInstance=mytestproject1-123456:europe-west1:mydb&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=username&password=password&useSSL=false

I have followed directions advising to add elements to the POM.xml file.

http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.my</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>myproject</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud-gcp.version>1.0.0.M3</spring-cloud-gcp.version>
    <spring-cloud.version>Finchley.M9</spring-cloud.version>
    <appengine.maven.plugin>1.3.2</appengine.maven.plugin>


    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <INSTANCE_CONNECTION_NAME>mytestproject1-123456:europe-west1:mydb</INSTANCE_CONNECTION_NAME>
    <user>username</user>
    <password>password</password>
    <database>exam</database>
    <sqlURL>jdbc:mysql://google/${database}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=${user}&amp;password=${password}&amp;useSSL=false</sqlURL>

</properties>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.google.cloud.sql</groupId>
        <artifactId>mysql-socket-factory-connector-j-6</artifactId>
        <version>1.0.8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-dependencies</artifactId>
            <version>${spring-cloud-gcp.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.9.63</version>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>${appengine.maven.plugin}</version>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

Once again, this is all working fine from local deployments, and the Cloud MySQL bucket is serving its data to locally deployed apps fine. My question is, how do I get the Spring Boot application to connect to the Cloud MySQL instance when deployed to Google App Engine?

DeWinters
  • 11
  • 3
  • In which directory is the application.proprieties file? looking at this source[https://medium.com/@DazWilkin/google-cloud-sql-4-ways-spring-a4f13ad32714 ] and this similar case[https://stackoverflow.com/questions/48848198/spring-boot-on-google-cloud-sql-org-hibernate-hibernateexception-access-to-di#comment-85505728 ] it should be on src/main/resources. – gr7 May 03 '18 at 09:19
  • My file structure is already as you advised, but your links have shown me a few new formulations to try out. Thanks I'll let you know if any of them fix it. – DeWinters May 03 '18 at 19:31

1 Answers1

1

You might be interested in trying out the new Spring Cloud GCP integration for Cloud SQL MySQL. It automatically chooses the JDBC URL for you. You should be able to get your app running with less configuration than you currently have.

  • Thanks for the suggestion. Here is the documentation that links up with that spring-starter lib: https://cloud.spring.io/spring-cloud-gcp/multi/multi__spring_jdbc.html – Ian Newland Sep 29 '19 at 19:00