简体   繁体   English

Google Cloud Platform - Cloud SQL 上 MySQL 的 Spring Boot 应用程序连接问题

[英]Google Cloud Platform - Spring Boot application connection issues with MySQL on Cloud SQL

So, I have a minimal Spring Boot Application which is supposed to connect to a MySQL database on GCP-Cloud SQL.所以,我有一个最小的 Spring Boot 应用程序,它应该连接到 GCP-Cloud SQL 上的 MySQL 数据库。

When I run my application on Google App Engine locally, it manages to connect to the remote MySQL database hosted on GCP.当我在本地 Google App Engine 上运行我的应用程序时,它设法连接到 GCP 上托管的远程 MySQL 数据库。 When I deploy the very same application on GAE standard, the application (apparently deployed successfully on GCP) doesn't manage to connect to the MySQL database.当我在 GAE 标准上部署完全相同的应用程序时,该应用程序(显然在 GCP 上成功部署)无法连接到 MySQL 数据库。

This is exactly what my application.properties file looks like:这正是我的 application.properties 文件的样子:

文本

Giving a look in the GCP logging section I see the following critical error:查看 GCP 日志记录部分,我看到以下严重错误:

Uncaught exception from servlet\njava.lang.RuntimeException:
org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'userRepository' defined in com.winery.backend.services.UserRepository 
defined in @EnableJpaRepositories declared onJpaRepositoriesRegistrar.
EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' 
while setting bean property 'mappingContext'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext':

and so on for a few thousand lines.等等几千行。 userRepository happens to be my custom @Repository annotated interface. userRepository 恰好是我的自定义 @Repository 注释界面。 Again, it works like a charm from local to cloud.同样,它就像从本地到云的魅力。

I've seen there are a few similar threads, but none of them actually provided a working solution.我看到有一些类似的线程,但实际上没有一个提供有效的解决方案。

When you are trying to connect using Cloud SQL's public IP address, you need to determine the public IP of your application and authorize it to connect to your instance.当您尝试使用 Cloud SQL 的公共 IP 地址进行连接时,您需要确定应用程序的公共 IP 并授权其连接到您的实例。 This won't apply for App Engine since it doesn't provide static IP address.这不适用于 App Engine,因为它不提供静态 IP 地址。

It's best practice for you use a Cloud SQL connector and include this dependency on your pom.xml:最佳做法是使用 Cloud SQL 连接器并将此依赖项包含在 pom.xml 中:

<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>mysql-socket-factory-connector-j-8</artifactId>
    <version>1.1.0</version>
</dependency>

Then create a JDBC URL like this :然后像这样创建一个 JDBC URL:

jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>

See the instance connection name on Cloud SQL in GCP console.在 GCP 控制台中查看 Cloud SQL 上的实例连接名称。 The format should be "project-id:region:instance-name".格式应为“project-id:region:instance-name”。

You can also improve this by creating a connection pool:您还可以通过创建连接池来改进这一点:


private static final String CLOUD_SQL_CONNECTION_NAME = System.getenv("CLOUD_SQL_CONNECTION_NAME");
private static final String DB_USER = System.getenv("DB_USER");
private static final String DB_PASS = System.getenv("DB_PASS");
private static final String DB_NAME = System.getenv("DB_NAME");

private DataSource createConnectionPool() {
    HikariConfig config = new HikariConfig();

    config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
    config.setUsername(DB_USER); // e.g. "root", "postgres"
    config.setPassword(DB_PASS); // e.g. "my-password"
    config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
    config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
    DataSource pool = new HikariDataSource(config);
    return pool;
  }

To see the full context, see this guide .要查看完整上下文, 请参阅本指南

Maybe it's the scope, if both (database and app) it's in the same host, maybe cant connect via https because cant resolve the host.也许是范围,如果(数据库和应用程序)都在同一主机中,则可能无法通过 https 连接,因为无法解析主机。

is it both in the same host/public IP?是否在同一个主机/公共 IP 中?

if you are using docker:如果您使用的是码头工人:

  • Try to use an alternative profile application-gcp.yml () a replace public_ip for docker service name (and be sure to create a network type bridge)尝试使用替代配置文件 application-gcp.yml() 将 public_ip 替换为 docker 服务名称(并确保创建网络类型桥接器)

if you not:如果你不是:

  • Try to use localhost instead of public IP尝试使用 localhost 而不是公共 IP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 java11 在谷歌云上使用 mysql 部署 Spring 启动应用程序 - Deploying a Spring boot application with mysql on google cloud with java11 连接 Spring Boot 应用程序和 google Cloud SQL 的推荐方法是什么? - What is the recomended way to connect spring boot application and google Cloud SQL? 如果我们已在Google Cloud Platform中部署Spring Boot应用程序,如何查看日志? - How to see the logs if we have deployed our Spring Boot Application in Google Cloud Platform? 无法让 Spring 引导应用程序在 Google Cloud Platform Flexible Environment 上工作 - Can't get Spring Boot application to work on Google Cloud Platform Flexible Environment Angular2前端在Google云平台上的Spring Boot后端 - Angular2 frontend Spring Boot backend on Google cloud platform Cloud Foundry Spring Boot应用程序中的连接被拒绝错误 - Connection Refused Error in Cloud Foundry Spring Boot application 如何将托管在 Google 云引擎上的 Spring Boot 应用程序连接到 mysql 数据库? - How to connect a spring boot application hosted on Google cloud engine to a mysql db? 带有云运行和云 Firestore 的 Spring Boot 应用程序 - Spring boot application with cloud run and cloud firestore Google Cloud Platform管道/容器构建器在Spring Boot Java Application中使用COPY或ADD命令构建docker映像时出现问题 - Google Cloud Platform pipeline/container builder issue building docker image using COPY or ADD command for Spring Boot Java Application 无法使用 Spring-Boot 应用程序从 GKE 连接到 Cloud SQL (MySQL) - Not able to connect to Cloud SQL (MySQL) from GKE using a Spring-Boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM