In this article I will show you how to deploy a spring boot application with a postgresql db on fly.io
Creating the Spring Boot project
On Spring Initializr we will need spring web, spring data jpa and postgresql driver for this project, you can also use jdbc if you want.
We will create a simple Product class
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
private Float price;
// getters and setters
}
With this repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
and finally the controller
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductRepository productRepository;
public ProductController(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@GetMapping
public List<Product> findAll() {
return productRepository.findAll();
}
@GetMapping("/{id}")
public Product findById(@PathVariable Long id) {
return productRepository.findById(id).orElseThrow(()->new RuntimeException("Product not found"));
}
@PostMapping
public Product createProduct(@RequestBody Product product)
{
return productRepository.save(product);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Long id) {
productRepository.deleteById(id);
}
}
The application.yml file, I don't use a migration tool so I let hibernate generate the tables, but in production you should not do that.
spring:
datasource:
url: jdbc:postgresql://${PGHOST}:${PGPORT}/${PGDATABASE}
username: ${PGUSER}
password: ${PGPASSWORD}
jpa:
hibernate:
ddl-auto: update
open-in-view: false
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
Deploying on fly.io
First you need to install flyctl here
Then create a dockerfile, I set the maximum ram to 512mb, you should adapt it to your app memory usage:
# Step 1: Create jar file that will be deployed
FROM maven:3.9.11-amazoncorretto-21 AS builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests
# Step 2: Create final image
FROM eclipse-temurin:21-jre
WORKDIR /app
# Copy jar file build during the builder step
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-XX:MaxRAM=512m","-XX:MaxMetaspaceSize=124m", "-jar", "app.jar"]
In cmd, run
fly auth login
then
fly launch
It will create the app on fly.io. However the app wont't start because we don't provide variables for db.
To do that, first run
fly postgres create
It will start a wizard in your terminal to create an unmanaged postgresql db. Follow it and at the end you will get some informations on the db you've created:
- Username
- Password
- Hostname
- Proxy port
Now go to your app -> secrets and add theses secrets from the previous infos:
PGUSER -> Username
PGPASSWORD -> Password
PGHOST -> Hostname (with the .internal)
PGPORT -> Proxy Port
PGDATABASE -> postgres
After doing that you can redeploy your app with fly deploy in your terminal. If it is successful, you can go to /api/products and see an empty array. If it is not working, go to the dashboard, in Logs & Errors tab to see what error is displayed.
Top comments (0)