Hey, guys welcome back to yet another interesting post. In this post, we will discuss deploying the Spring Boot application to the production server using Railway.app service.
If you are following my youtube channel, then you probably know that previously I was deploying my Spring Boot application to Heroku and AWS, but after Heroku changed its pricing plan, I was looking for a platform to Host my Spring boot applications. I recently came across this service, you can deploy your Spring Boot application in just a few minutes. You don’t have to provide any credit/debit card details to create an account. Once you create the Account, you will be under the Standard plan, where you will get 500Hrs execution time each month, after 500Hrs your application will shut down automatically and restart next month. This is more than enough for your hobby projects.
Read More:
- Check the Complete JUnit 5 Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Spring Boot JdbcTemplate Tutorials
- Check the Complete Spring Boot and Data JPA Tutorials
- Check the Complete Spring MVC Tutorials
- Check the Complete JSP Tutorials
- Check the Complete Spring Boot Tutorials [100+ Examples]
- Check the Complete Spring Boot and Thymeleaf Tutorial
- Check the Complete AWS Tutorial
- Check the Complete JavaServer Faces (JSF) Tutorial
- Check the Complete Spring Data JPA Tutorial
- Check the Complete Spring Security Tutorial
- Check the Javascript Projects for Beginners
Watch the Video
Create Spring Boot Project
There are many different ways to create a spring boot application, you can follow the below articles to create one –
>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA
Add maven dependencies
Open pom.xml
and add the following dependencies –
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.bushansirgur</groupId>
<artifactId>image-upload-download</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>image-upload-download</name>
<description>Spring boot image upload and download project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Create a Controller
Create a class HomeController.java
under the default package and add the following content
package in.bushansirgur.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@GetMapping({"/", "/home", "/status"})
public String getStatus() {
return "Application is up and running";
}
}
Run the app
Run the application using the below maven command –
mvn spring-boot:run
Create Github Repository
Login to your GitHub account, create a new repository
Once you create the repository, it will show you the instructions to push your code to Github.
Push the source code to GitHub
Open the terminal, go to the root of the project and execute the following commands
Note: Make sure that you installed git on your system before executing these commands
git init
This will initialize the git on your project
git add .
This will add all your files to the staging area
git commit -m "initial commit"
This will commit all your changes locally.
git remote add origin https://github.com/scbushan05/my-first-app.git
This will add the remote url to your project
git push origin master
This will push all your changes to GitHub, now your source code is available on GitHub.
Create an Account in Railway.app
You need to create a new account in Railway.app in order to deploy your Spring boot application. Don’t worry, it will not ask you to enter your credit/debit card details. Once you create an account, login to your account, your dashboard that look like this
Create a new project
Click on the New Project option to create a new project
You can choose Empty Project, it will create an empty project with a random project name.
Create a new Service
A project may contain multiple services, so go inside the project and create a new Service
For now, click on empty service, this will create a new service with a random service name.
Generate a Domain
Once you create a service, we need to generate the domain for our service, click on the service, go to settings, under the domains section, click on Generate Domain, it will create random domain for us.
Deploy the application to the service
Under the service settings, you will see the service source option, you can click Connect Repo option
It will ask you to choose the repository that you want to deploy, choose the repository which we created
As soon as you select the repository, it will start deploying automatically, you can see that under the deployments in the settings
For the first time, it will take few minutes, wait till it finishes the deployment. Once the deployment is done, you will see the deployment completed status. You can visit the generated URL milky-oatmeal-production.up.railway.app to see the application
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.