【问题标题】:Connect from Spring boot app running on Google Cloud Run to a Cloudsql without GCP starter从在 Google Cloud Run 上运行的 Spring Boot 应用程序连接到没有 GCP 启动器的 Cloudsql
【发布时间】:2020-02-02 03:30:44
【问题描述】:

我有一个在 Google Cloud Run 上运行的 Spring Boot 应用程序,我想连接到在 Google Cloudsql 上运行的 postgres 实例。我可以使用 Spring Cloud GCP 入门项目进行连接。

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

但是我不想添加这个依赖。

我能够通过使用 postgressql 插件并在创建云代理后指定 spring.datasource.url=*** 从本地应用程序进行连接。

但是,当我在使用 gcloud beta run deploy --add-cloudsql-instances &lt;INSTANCE-ID&gt; 后尝试从云运行中执行相同操作时,它不起作用。根据https://cloud.google.com/run/docs/configuring/connect-cloudsql--add-cloudsql-instances会自动创建一个云代理。那么为什么它不起作用呢?

谢谢。

【问题讨论】:

    标签: spring-boot google-cloud-sql google-cloud-run cloud-sql-proxy


    【解决方案1】:

    这不起作用,因为 Java 不支持 Unix 域套接字。 Spring Cloud GCP 使用Cloud SQL JDBC Socket Factory 绕过了这一点。

    您可以直接使用 Socket Factory,方法是将其添加到您的 pom 中(并按照 README 中的说明更新您的 URL)。

    【讨论】:

    • 我已经尝试过这样做,但没有成功。这就是我的 application.properties 在使用 Socket Factory spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.username=${CLOUDSQL_USERNAME} spring.datasource.password=${CLOUDSQL_PASSWORD} spring.datasource.url=jdbc:postgresql://google/${DB}?cloudSqlInstance=${DB_INSTANCE}&amp;socketFactory=com.google.cloud.sql.postgres.SocketFactory&amp;user=${CLOUDSQL_USERNAME}&amp;password=${CLOUDSQL_PASSWORD} 时的样子
    • 是的,这应该是答案,但我也没有让它工作。我正在使用DriverManagerDataSource
    猜你喜欢
    • 2020-10-22
    • 2019-12-04
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多