У меня есть загрузочный проект Spring, который должен соединиться с экземпляром Cloud SQL с spring-cloud-gcp-starter-sql-postgresql
, чтобы избежать явного использования IP-адреса в проекте.
Пока что он хорошо соединяется но он сильно задержался (около 30 секунд до запуска), потому что он пытается соединиться через сокет SSL и после многих попыток он соединяется.
В журналах есть строка, которая говорит:
2020-02-19 00:10:09.809 INFO 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Default POSTGRESQL JdbcUrl provider. Connecting to jdbc:postgresql://google/test?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=XXXXXX:us-central1:test&useSSL=false with driver org.postgresql.Driver
Насколько я знаю, параметр useSSL=false
не будет работать для postgresql. Правильным является ssl=false
, но когда я пытаюсь перезаписать JDB C Url с помощью application.yml, он печатает следующий журнал:
2020-02-19 00:10:09.816 WARN 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Ignoring provided spring.datasource.url. Overwriting it based on the spring.cloud.gcp.sql.instance-connection-name.
Я подозреваю, что задержка из-за SSL подключение. Итак, у меня есть два вопроса:
- Как я могу избежать использования SSL-соединения? Поскольку я не устанавливаю URL-адрес JDB C явно, я не могу использовать
ssl=false
в параметре. - Я подозреваю, что происходит задержка, поскольку не установлен SSL-сертификат клиента. Если это так, как я могу установить его? У меня уже есть .pem, но я не знаю, как его реализовать
Я добавляю свой application.yml
с конфигурациями и указанным журналом:
spring:
cloud:
gcp:
project-id: xxxxxxx
config
sql:
instance-connection-name: xxxxxxx:us-central1:test
database-name: test
enabled: true
datasource:
username: test
password: 123456
initialization-mode: always
jpa:
hibernate:
ddl-auto: update
Учетные данные задаются в переменной среды "GOOGLE_APPLICATION_CREDENTIALS", вызывающей учетную запись службы json
2020-02-19 00:10:09.692 INFO 6779 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-19 00:10:09.699 INFO 6779 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-02-19 00:10:09.699 INFO 6779 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-02-19 00:10:09.767 INFO 6779 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-02-19 00:10:09.767 INFO 6779 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 790 ms
2020-02-19 00:10:09.809 INFO 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Default POSTGRESQL JdbcUrl provider. Connecting to jdbc:postgresql://google/test?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=XXXXXXX:us-central1:test&useSSL=false with driver org.postgresql.Driver
2020-02-19 00:10:09.816 WARN 6779 --- [ main] o.s.c.g.a.s.GcpCloudSqlAutoConfiguration : Ignoring provided spring.datasource.url. Overwriting it based on the spring.cloud.gcp.sql.instance-connection-name.
2020-02-19 00:10:09.885 INFO 6779 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-02-19 00:10:09.940 INFO 6779 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.10.Final}
2020-02-19 00:10:10.039 INFO 6779 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-02-19 00:10:10.109 INFO 6779 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-02-19 00:10:10.193 INFO 6779 --- [ main] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:10.193 INFO 6779 --- [ main] c.g.cloud.sql.core.CoreSocketFactory : First Cloud SQL connection, generating RSA key pair.
2020-02-19 00:10:13.690 INFO 6779 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-02-19 00:10:13.711 INFO 6779 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2020-02-19 00:10:13.791 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:15.042 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:16.333 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:17.653 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:19.314 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:20.643 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:21.938 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:23.227 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:24.561 INFO 6779 --- [onnection adder] c.g.cloud.sql.core.CoreSocketFactory : Connecting to Cloud SQL instance [XXXXXXX:us-central1:test] via SSL socket.
2020-02-19 00:10:35.164 INFO 6779 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-02-19 00:10:35.173 INFO 6779 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-02-19 00:10:35.294 WARN 6779 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-02-19 00:10:35.507 INFO 6779 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-19 00:10:35.611 INFO 6779 --- [ main] o.s.c.g.core.DefaultCredentialsProvider : Default credentials provider for service account cloud-code@XXXXXXX.iam.gserviceaccount.com
2020-02-19 00:10:35.612 INFO 6779 --- [ main] o.s.c.g.core.DefaultCredentialsProvider : Scopes in use by default credentials: [https://www.googleapis.com/auth/pubsub, https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data, https://www.googleapis.com/auth/datastore, https://www.googleapis.com/auth/sqlservice.admin, https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write, https://www.googleapis.com/auth/cloudruntimeconfig, https://www.googleapis.com/auth/trace.append, https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/cloud-vision, https://www.googleapis.com/auth/bigquery]
2020-02-19 00:10:35.612 INFO 6779 --- [ main] o.s.c.g.a.c.GcpContextAutoConfiguration : The default project ID is XXXXXXX
2020-02-19 00:10:35.730 INFO 6779 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-02-19 00:10:35.733 INFO 6779 --- [ main] c.r.c.CloudSqlTestApplication : Started CloudSqlTestApplication in 27.027 seconds (JVM running for 27.464)
Кроме того, на всякий случай это помогает, я добавляю pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ramonparis</groupId>
<artifactId>cloud-sql-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud-sql-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
</dependency>
<dependency>
........
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>