Spring Data JPA应用程序连接到Google Cloud Run Postgres

vlju58qv  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(156)

谷歌有一个example连接云SQL-MYSQl从Spring JPA/Boot App(提交9ecdc1111e3da388a750ace41a125287d9620534被使用)。这个例子是使用Spring Data 和MySQL的工作很好。但它不工作时,配置文件被更改为postgress(后,启动正确的Postgres数据库在同一帐户和相同的步骤#2)

spring.profiles.active=postgres

并替换

<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
with 
  <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>

以及

replacing src/main/resources/application-mysql.properties 
with 
 src/main/resources/application-postgres.properties

但应用程序仍会因
创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为"entityManagerFactory"的Bean时出错:调用init方法失败;嵌套异常是org. hib. service. spi。服务异常:无法创建请求的服务[org. hib. engine. jdbc. env. spi. jdbc环境]
我找不到任何样本。
application-postgres.properties is appended to have

spring.profiles.active=postgres
spring.cloud.gcp.sql.instance-connection-name= xyzprj:us-central1:postgres-instance
spring.datasource.username=xyzuser
spring.datasource.password=password

application-postgres.properties is replaced as followes

spring.datasource.username=xyzuser
spring.datasource.password=passord
spring.sql.init.mode=always
spring.cloud.gcp.sql.database-name=petclinic
spring.cloud.gcp.sql.instance-connection-name=xyzprj:us-central1:postgres-instance

后来,这两个属性文件也被更改,以便
spring.数据源.用户名= root
以及
spring.数据源.密码=根
但是在Google云内的云 shell 上尝试相同的问题样本,
gcloud身份验证应用程序-默认登录
你正在Google Compute Engine虚拟机上运行。应用程序默认凭据将自动使用与此虚拟机关联的服务凭据,因此不必使用此命令。
如果仍决定继续,则可以访问此虚拟机的其他人可能会看到你的用户凭据。是否确实要使用你的个人帐户进行身份验证?
是否要继续(Y/n)?n
错误:(gcloud. auth. application-default. login)已被用户中止。

t40tm48m

t40tm48m1#

我尝试在我这边重现此问题,但我能够成功部署应用程序
以下是我遵循的步骤

**步骤1:**使用以下命令创建postgresql

gcloud sql instances create postgres-instance \
--database-version=POSTGRES_13 \
 --cpu=1 \
 --memory=4GB \
 --region=us-central \
 --root-password=root

**步骤2:**创建数据库使用

gcloud sql databases create petclinic --instance postgres-instance

**步骤3:**连接到PostgreSQL示例以验证连接是否已建立

gcloud sql connect postgres-instance

**步骤4:**按照application.properties中的操作替换以下内容

spring.profiles.active=postgres

并替换

<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
    with 
      <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>

以及

replacing src/main/resources/application-mysql.properties 
    with 
     src/main/resources/application-postgres.properties

**第5步:**除上述更改外

application.properties中更换

spring.cloud.gcp.sql.instance-connection-name= POSTGRESQL_CONNECTION_NAME

src/main/resources/application-postgres.properties中添加

spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD

pom.xml文件中添加以下依赖项

<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>postgres-socket-factory</artifactId>
    <version>1.1.0</version>
</dependency>

build.grable文件中添加

dependencies {
    compile 'com.google.cloud.sql:postgres-socket-factory:1.1.0'
}

***注意:***运行gcloud auth application-default login以访问默认凭据,从而与Cloud Sql API通信

有关明确的信息,请检查此document

相关问题