[英]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:如果您使用的是码头工人:
if you not:如果你不是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.