[英]Access configuration file mentioned in spring boot application.properties after creating the .jar for App engine deployment
I am facing big trouble in deploying a spring boot application in GCP AppEngine with cloud Postgres as a database.在使用云 Postgres 作为数据库的 GCP AppEngine 中部署 spring 启动应用程序时,我遇到了很大的麻烦。 Earlier I was using tcp connection jdbc with IP whitelisting to access the database, which worked fine during testing but after deploying into Appengine it didn't warmed up due to
sslsocket timed out
.早些时候,我使用 tcp 连接 jdbc 和 IP 白名单来访问数据库,这在测试期间工作正常,但在部署到
sslsocket timed out
后它没有预热。 So after a bit of digging, I found for standard appengine runtime to connect a cloud Postgres I have to use postgres socket factory因此,经过一番挖掘,我发现标准的 appengine 运行时要连接云 Postgres 我必须使用 postgres 套接字工厂
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.2.2</version>
</dependency>
with spring.datasource.url=jdbc:postgresql:///{REDACT}?cloudSqlInstance={REDACT}&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user={REDACT}&password={REDACT}
in application.properties. with
spring.datasource.url=jdbc:postgresql:///{REDACT}?cloudSqlInstance={REDACT}&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user={REDACT}&password={REDACT}
in application.properties.
But GCP needs to verify the connection from the key generated from the service account with specific privileges which has been added to the application.properties.但是 GCP 需要使用已添加到 application.properties 的具有特定权限的服务帐户生成的密钥来验证连接。
spring.cloud.gcp.credentials.location=classpath:gcloud.json
But in java the built jar file can't access the json, nor the deployed appengine instance.但是在 java 中,构建的 jar 文件无法访问 json,也无法访问部署的 appengine 实例。 Here's the error
这是错误
[Screenshot error in App engine][1] [应用引擎中的屏幕截图错误][1]
app.yaml app.yaml
runtime: java11
pom.xml pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.2.2</version>
</dependency>
Any reply would be of great help.任何回复都会有很大帮助。 Thanks [1]: https://i.stack.imgur.com/gEV2r.png
谢谢[1]: https://i.stack.imgur.com/gEV2r.png
With App Engine you don't need to use a service account key file.使用 App Engine,您无需使用服务帐户密钥文件。 You can rely on the metadata server that provides automatically App Engine service account credential.
您可以依赖自动提供 App Engine 服务帐户凭据的元数据服务器。 Service account is not customizable for now, but should change in 2021: It name is the following:
<projectID>@appspot.gserviceaccount.com
服务帐户目前不可自定义,但应在 2021 年更改:其名称如下:
<projectID>@appspot.gserviceaccount.com
Be sure that this service account has enough Cloud SQL permissions (by default, it has Editor role on the project, and have enough (and even too much!!) permissions)确保此服务帐户具有 足够的 Cloud SQL 权限(默认情况下,它对项目具有 Editor 角色,并且具有足够(甚至太多!!)权限)
So, use the Cloud SQL App Engine connector in the app.yaml
file to set up the connection with Cloud SQL and open a socket (to connect with socket factory) in your code因此,请使用
app.yaml
文件中的 Cloud SQL App Engine 连接器来设置与 Cloud SQL 的连接并在您的代码中打开一个套接字(使用您的代码连接)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.