This tutorial will guide you through the process of creating, securing, and deploying a REST API using Spring Boot and Google Cloud SQL. By the end of this tutorial, you will have a production-ready REST API hosted on Google Cloud Platform (GCP).
Prerequisites
- Google Cloud Account: Create an account at Google Cloud if you don't have one.
- Java Development Kit (JDK): Install JDK 17 or later.
- Maven: Ensure you have Maven installed.
- Cloud SDK: Install the Google Cloud SDK.
- Database Client: Use tools like MySQL Workbench or DBeaver to test your database connection.
Step 1: Initialize Your Spring Boot Project
Go to Spring Initializr.
Configure your project:
Project: Maven
Language: Java
Spring Boot: Latest stable version
Dependencies: Select Spring Web, Spring Data JPA, MySQL Driver, and Spring Security.
Click Generate to download the project and unzip it.
Step 2: Set Up Google Cloud SQL
Create a Cloud SQL Instance:
Go to the Cloud SQL page.
Click Create Instance.
Select MySQL and configure the instance.
Set a secure password for the root user.
Create a new database (e.g., rest_api_db).
Enable Connections:
Go to the Connections tab of your Cloud SQL instance.
Enable the Public IP and whitelist your development machine's IP address.
Note the Connection String:
It will look like YOUR_INSTANCE_IP:3306/rest_api_db.
Step 3: Configure Your Spring Boot Application
Open the application.properties or application.yml file and add the following:
spring.datasource.url=jdbc:mysql://YOUR_INSTANCE_IP:3306/rest_api_db
spring.datasource.username=root
spring.datasource.password=YOUR_PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
Replace YOUR_INSTANCE_IP, YOUR_PASSWORD, and rest_api_db with your actual values.
Step 4: Develop Your REST API
Create an Entity Class:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Getters and setters
}
Create a Repository Interface:
public interface UserRepository extends JpaRepository<User, Long> {
}
Create a REST Controller:
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public List<User> getAllUsers() {
return userRepository.findAll();
}
@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}
Test the API locally using tools like Postman or cURL.
Step 5: Add Security with Spring Security
Add the following to your application.properties:
spring.security.user.name=admin
spring.security.user.password=securepassword
Test the API by accessing it in your browser. You should see a login prompt.
Step 6: Deploy to Google Cloud
Enable Required APIs:
Enable the Cloud SQL Admin API in the API & Services page.
Build the Application:
Run mvn clean package to generate a JAR file.
Deploy with Cloud Run:
Containerize your application by creating a Dockerfile:
FROM openjdk:17-jdk-slim
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
Build the Docker image:
docker build -t gcr.io/YOUR_PROJECT_ID/rest-api .
Push the image to Google Container Registry:
docker push gcr.io/YOUR_PROJECT_ID/rest-api
Deploy the container to Cloud Run:
gcloud run deploy rest-api \
--image gcr.io/YOUR_PROJECT_ID/rest-api \
--platform managed \
--region us-central1 \
--allow-unauthenticated
Connect to Cloud SQL:
Update the spring.datasource.url in application.properties to use the Cloud SQL socket.
spring.datasource.url=jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory
Step 7: Test Your Deployment
- Access your REST API via the URL provided by Cloud Run.
- Test endpoints using Postman or your preferred tool.
You have successfully deployed a secure REST API with Spring Boot and Google Cloud SQL. Hope this is helpful, and I apologize if there are any inaccuracies in the information provided.
Post a Comment for "Deploy a Secure REST API with Spring Boot and Google Cloud SQL"