This project demonstrates a Spring Boot application containerized using Jib, connecting to Google Cloud SQL for PostgreSQL with IAM (passwordless) authentication.
- Spring Boot Application: A RESTful API built with Spring Boot.
- Containerization with Jib: Easily build optimized Docker images for the Spring Boot application without a Dockerfile.
- Cloud SQL for PostgreSQL Integration: Connects to a PostgreSQL database hosted on Google Cloud SQL.
- IAM (Passwordless) Authentication: Utilizes Google Cloud IAM for secure, passwordless authentication to Cloud SQL, enhancing security and simplifying credential management.
- RESTful API: Provides endpoints for managing employee data.
- Java 21
- Spring Boot (version 4.0.6)
- Spring Cloud GCP (version 8.0.2)
- Jib Maven Plugin
- PostgreSQL
- Lombok
- ModelMapper
- Java Development Kit (JDK) 21
- Maven
- Google Cloud SDK (gcloud CLI) configured with appropriate permissions to access Cloud SQL.
- A Google Cloud Project with Cloud SQL for PostgreSQL instance enabled.
- Ensure the service account or user running the application has the
Cloud SQL Clientrole.
To build the Spring Boot application, run:
mvn clean installJib is configured in the pom.xml to build a Docker image. To build the image and push it to a container registry (e.g., Google Container Registry or Docker Hub), use the following command:
mvn compile jib:buildBefore running this command, ensure you are authenticated to your container registry. For Google Container Registry (GCR) or Artifact Registry, you can authenticate using:
gcloud auth configure-dockerThe pom.xml specifies gcr.io/distroless/java21-debian12 as the base image for a minimal and secure runtime environment.
You can run the application locally using Maven:
mvn spring-boot:runFor local testing with a PostgreSQL database, you might need to configure application.properties or application.yaml with local database credentials.
The application uses spring-cloud-gcp-starter-sql-postgresql for seamless integration with Cloud SQL. For IAM authentication, ensure your application's environment (e.g., Google Kubernetes Engine, Cloud Run, or a VM with a service account) has the necessary permissions (Cloud SQL Client role).
The connection string in application.properties typically looks like this for IAM authentication:
spring.datasource.url=jdbc:postgresql:///database-name?cloudSqlInstance=project-id:region:instance-name&socketFactory=com.google.cloud.sql.postgres.SocketFactory&enableIamAuth=true
spring.datasource.username=service-account-email-or-user-emailReplace database-name, project-id, region, instance-name, and service-account-email-or-user-email with your actual Cloud SQL details.
The application exposes the following RESTful endpoints for managing employees:
-
GET
/employees/- Description: Retrieves a list of all employees.
- Response:
200 OKwith a list ofEmployeeDTOobjects.
-
GET
/employees/find/{id}- Description: Retrieves a single employee by their ID.
- Parameters:
id(Path Variable, Long): The ID of the employee to retrieve.
- Response:
200 OKwith anEmployeeDTOobject if found,404 Not Foundotherwise.
-
POST
/employees/add- Description: Adds a new employee to the database.
- Request Body:
EmployeeDTOobject containing employee details. - Response:
201 Createdwith the newly addedEmployeeDTOobject.
-
PUT
/employees/update/{id}- Description: Updates an existing employee's details.
- Parameters:
id(Path Variable, Long): The ID of the employee to update.
- Request Body:
EmployeeDTOobject with updated employee details. - Response:
200 OKwith the updatedEmployeeDTOobject.
-
DELETE
/employees/delete/{id}- Description: Deletes an employee from the database by their ID.
- Parameters:
id(Path Variable, Long): The ID of the employee to delete.
- Response:
204 No Contentif successful.
.
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── run
│ │ │ └── deploy
│ │ │ ├── controllers
│ │ │ │ └── EmployeeController.java
│ │ │ ├── dto
│ │ │ ├── exceptions
│ │ │ ├── models
│ │ │ ├── repository
│ │ │ ├── service
│ │ │ └── JibRunDeployApplication.java
│ │ └── resources
│ │ └── application.properties
│ └── test
├── pom.xml
└── README.md