Added support of GCP CLoud SQL

This commit is contained in:
Igor Lakhtenkov 2025-01-15 12:15:56 +01:00
parent 6148ddd967
commit 3781fa6276
No known key found for this signature in database
GPG key ID: 3E0265E0E5387C66
4 changed files with 49 additions and 0 deletions

21
Dockerfile Normal file
View file

@ -0,0 +1,21 @@
# First stage: Build the application with Maven
FROM maven:3.8.4-openjdk-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests
# Second stage: Download New Relic Java APM agent
FROM alpine:3.14 AS newrelic
WORKDIR /newrelic
RUN wget https://download.newrelic.com/newrelic/java-agent/newrelic-agent/current/newrelic-java.zip && \
unzip newrelic-java.zip
# Third stage: Copy artifacts to the distroless JRE image and run them
FROM gcr.io/distroless/java17-debian11
WORKDIR /app
USER root
COPY --from=build /app/target/spring-petclinic.jar /app/spring-petclinic.jar
COPY --from=newrelic /newrelic/newrelic/ /newrelic/
ENV NEW_RELIC_APP_NAME=petclinic
ENTRYPOINT ["java", "-javaagent:/newrelic/newrelic.jar", "-jar", "/app/spring-petclinic.jar"]

View file

@ -88,6 +88,11 @@
<artifactId>mysql-connector-j</artifactId> <artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.21.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
@ -155,6 +160,7 @@
</dependencies> </dependencies>
<build> <build>
<finalName>${project.artifactId}</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>

View file

@ -0,0 +1,19 @@
database: postgres
spring:
sql:
init:
mode: always
cloud:
gcp:
sql:
database-name: ${DB_NAME}
enable-iam-auth: true
instance-connection-name: ${INSTANCE_CONNECTION_NAME}
sslmode: disable
datasource:
url: jdbc:postgresql:///${DB_NAME}?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&enableIamAuth=true&user=${DB_USER}&password=dummy&sslmode=disable
username: ${DB_USER}
jpa:
hibernate:
ddl-auto: update
show-sql: true

View file

@ -23,3 +23,6 @@ logging.level.org.springframework=INFO
# Maximum time static resources should be cached # Maximum time static resources should be cached
spring.web.resources.cache.cachecontrol.max-age=12h spring.web.resources.cache.cachecontrol.max-age=12h
# In the last line, add mysql to the spring.profiles.active property
spring.profiles.active=cloudsql