This is a test Spring Boot app meant to be a base for future deploys on Railway
Not ready for production
?Could be ready for production
**
There's an application-prod.yml
file used to save basic production config data. This will have different info depending on the platform you're making the deploy. Check it for more context. What's in there however is specifically for railway.
- Login or setup new railway account.
- Recommended to be setup from github for easier access to repos.
- Railway.app
- Create a new project
- Either from the top right icon or center action area
- I recommend an empty project
Services work as plugins you can add to your project. They vary from Database services such as Mysql, PostgreSQL, MongoDB
all the way to Django, Laravel, DiscordJS bot, Kotlin, Vite, Vite + Vue + TS
, etc... For this project we'll be using Mysql
and our own service (Spring Boot App)
fetched directly from GitHub
.
-
Your service might take some time to setup
-
Once created, click on your service. It will have several info tabs.
-
You can create tables directly from
railway
but we don't want that right now.
Check this out
- Travel to your Variables tab (might be in other tabs since this updates regularly). Once there, you'll realize that's all the info to make your connection from your API to your DB.
- Keep it private
These properties define your DB/Service environment variables. They're necessary info for a proper connection between your APP and the DB.
- Copy your info from railway given by the
MySQL
service and paste them in the format down bellow here, although it's not safe for your app because it will be visible from github. - All these environment variables are to be pasted in your service Variables before deploying
- Save the bellow information in a .txt file or somewhere it won't get lost
- Keep
spring-profiles_active
ANDPROD_DB_NAME
unmodified. - Watch out. Dont forget the first line.
spring_profiles_active=prod
PROD_DB_HOST=HOST_HERE
PROD_DB_PORT=POST_HERE
PROD_DB_NAME=railway
PROD_DB_PASSWORD=PASSWORD_HERE
PROD_DB_USERNAME=USER_HERE
-
Check your app structure. It should look similar (although not quite the same) as the image bellow
-
Navigate untill you reach
src/main/resources
-
Once there, create a new file inside
/resources
calledapplication-prod.yml
-
Verify the extension
-
Copy and paste in there the following data
spring:
datasource:
url: jdbc:mysql://${PROD_DB_HOST}:${PROD_DB_PORT}/${PROD_DB_NAME}
username: ${PROD_DB_USERNAME}
password: ${PROD_DB_PASSWORD}
name: ecommerceDB
sql:
init:
mode: always # you won't do this in prod, I'm just doing this for demo purposes
- Check tabulations.
YAML
files are weird
- If you won't work locally, the following is server config for the
application.properties
file. - If you're still working both locally and remotely, this step can be skipped.
- Update
src/main/resources/application.properties
and verify info.
application.properties
# spring.datasource.url= jdbc:mysql://localhost:3306/ecommercedb
# The previous line is an example of a diff type of config for this file.
spring.datasource.url= jdbc:mysql://${{ MYSQLUSER }}:${{ MYSQLPASSWORD }}@${{ MYSQLHOST }}:${{ MYSQLPORT }}/${{ MYSQLDATABASE }}
spring.datasource.username=yourUser
spring.datasource.password=yourPassword
logging.level.org.hibernate.SQL=DEBUG
This is a werid one.
- On the root directory of your app. Create a new file without extension called
Procfile
- Triple check the name
- Check the name and extension again
Paste the following line inside
web: java -jar -Dserver.port=$PORT build/libs/ecommerceDB-0.0.1-SNAPSHOT.jar
- This is basic info for railway to identify the
.jar
file and the Port it should be working on. - It's important that the version
0.0.1-SNAPSHOT.jar
matches your project's - You can check inside
build.gradle
version = '0.0.1-SNAPSHOT'
- One of the few last steps is to add our App on railway as a service
- Click on new service inside your railway project
- Select GitHub repo and look for your Spring Boot App
- Once clicked it will begin a build and then a deploy
- This first deploy will most likely fail due to environment variables not being Setup
- Click on your service and select the variables tab.
- Once there, click on Raw Editor on the right handside
- And paste our saved Environment Variables from MySLQ Already edited and filled with your info
spring_profiles_active=prod
PROD_DB_HOST=HOST_HERE
PROD_DB_PORT=POST_HERE
PROD_DB_NAME=railway
PROD_DB_PASSWORD=PASSWORD_HERE
PROD_DB_USERNAME=USER_HERE
It should look something like this:
- Click on update Variables
- Wait a bit and a new build should begin.
- On your app menu inside railway, click on
Settings
- You'll see a button to generate a Domain.
- Click on it
- Now you can access you app from the web by accessing this url
- All endpoints should be made after this initial URL