Spring Boot应用程序无法连接到Google Cloud SQL

时间:2018-01-31 08:55:36

标签: mysql google-app-engine spring-boot

我无法将Spring Boot应用程序连接到Cloud MySQL。我在App Engine中部署我的应用程序,并且没有连接到Cloud SQL的网站正常工作。但是,只要我的应用程序尝试连接到云,我就会收到错误消息。我已经按照https://cloud.google.com/appengine/docs/flexible/java/using-cloud-sql?hl=it的灵活和标准环境的指南进行了操作,但它似乎不起作用。我尝试了以下宠物诊所教程,但没有用。我将以下依赖项放在文件pom.xml中:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.42</version>
        </dependency>
        <dependency>
            <groupId>com.google.cloud.sql</groupId>
            <artifactId>mysql-socket-factory-connector-j-6</artifactId>
            <version>1.0.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我的application.properties中有以下项目:

spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://google/DATABASENAME?cloudSqlInstance=PROJECTID:REGION:INSTANCE&socketFactory=com.google.cloud.sql.mysql.SocketFactory
spring.datasource.username=USER
spring.datasource.password=*********

我尝试打开尝试连接数据库的网站时收到的错误是: 出现意外错误(type = Internal Server Error,status = 500)。 无法打开JPA EntityManager进行交易;嵌套异常是javax.persistence.PersistenceException:org.hibernate.exception.JDBCConnectionException:无法获取JDBC连接

连接Google Cloud需要做什么?

1 个答案:

答案 0 :(得分:0)

尝试在同一属性上使用用户和密码

spring.datasource.url=jdbc:mysql://google/DATABASE?cloudSqlInstance=INTANCE_NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=USERNAME_HERE&password=PASSWORD_HERE

spring.jpa.hibernate.ddl-auto=none
spring.datasource.username=USERNAME_HERE
spring.datasource.password=PASSWORD_HERE
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

然后确保你有这些依赖

<dependency>
  <groupId>com.google.api-client</groupId>
  <artifactId>google-api-client</artifactId>
  <version>1.22.0</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</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</artifactId>
          <version>1.0.5</version>
     </dependency>

请记住为您的项目启用google cloud sql api

这适用于标准环境。 我使用下面的说明使我的项目与标准兼容 https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/appengine-standard-java8/springboot-appengine-standard

我没有灵活测试过 祝你好运