diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..a774b3749 --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9b9b815df..a9efe2cf6 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,11 @@ mysql-connector-j runtime + + com.google.cloud.sql + postgres-socket-factory + 1.21.2 + org.postgresql postgresql @@ -155,6 +160,7 @@ + ${project.artifactId} org.apache.maven.plugins diff --git a/src/main/resources/application-cloudsql.yml b/src/main/resources/application-cloudsql.yml new file mode 100644 index 000000000..57d87736d --- /dev/null +++ b/src/main/resources/application-cloudsql.yml @@ -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 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6ed985654..c2854e786 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -23,3 +23,6 @@ logging.level.org.springframework=INFO # Maximum time static resources should be cached spring.web.resources.cache.cachecontrol.max-age=12h + +# In the last line, add mysql to the spring.profiles.active property +spring.profiles.active=cloudsql \ No newline at end of file