简体   繁体   中英

Spring Data JPA app coonection to Google Cloud Run Postgres

Google have an example to connect Cloud SQL-MYSQl from Spring JPA/Boot App ( commit 9ecdc1111e3da388a750ace41a125287d9620534 is used). The example is uses Spring Data and works fine with MySQL. But It does not work when profile is changed to postgress ( after starting right Postgres Database in same account and with same steps in #2 )

spring.profiles.active=postgres

and replacing

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

and

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

but still applications fails with

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

I could not find any sample.

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

later both of these properties files were also changed so that

spring.datasource.username=root

and

spring.datasource.password=root

but same issue sample is tried on Cloud Shell within Google Cloud,

gcloud auth application-default login

You are running on a Google Compute Engine virtual machine. The service credentials associated with this virtual machine will automatically be used by Application Default Credentials, so it is not necessary to use this command.

If you decide to proceed anyway, your user credentials may be visible to others with access to this virtual machine. Are you sure you want to authenticate with your personal account?

Do you want to continue (Y/n)? n

ERROR: (gcloud.auth.application-default.login) Aborted by user.


I tried to reproduce the issue on my side, but I was able to deploy application successfully

Here are the steps I followed

Step1: Created postgresql using below command

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

Step2: Created database using

gcloud sql databases create petclinic --instance postgres-instance

Step3: Connected to the PostgreSQL instance to verify the connection established or not

gcloud sql connect postgres-instance

Step4: replaced the following as you did In application.properties

spring.profiles.active=postgres

and replacing

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

and

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

Step5: In addition to above changes

In application.properties replaced

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

In src/main/resources/application-postgres.properties added

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

In pom.xml file added following dependency

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

In build.grable file add

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

Note: run gcloud auth application-default login to access default credential to communicate with Cloud Sql API

For clear information check this document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM