Spring Boot Railway: How to deploy a Spring Boot application to Railway

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone Dan Vega here and today we are continuing on with our series spring 2 production the idea behind this is we talk a lot about how to build applications in Spring we don't talk enough about how to get those applications into production so what I've done is I've created a simple project called the Spring blog and I'm going to take that and push it to production using a variety of platforms we've previously done a video on how to push this code into Microsoft azure's cloud more specifically Azure spring apps today I want to take a look at kind of the opposite end of the spectrum not the Enterprise side more of the personal project side I have this personal project I'm working on I want to go ahead and push it up somewhere maybe even want a free tier that I can just get started with uh so Railway was was suggested I threw something out on Twitter I asked a bunch of people what what are your favorite platforms to push your spring boot apps to and Railway come up a couple times and so I thought this would be a good chance to check it out so here we are on railway.app bring your code we will handle the rest uh what I like about this made for any language for projects big and small Railway is the cloud that takes the complexity out of shipping software so right away I really love the home page here beautifully built a whole lot of information about what you're going to get here on whale Railway um some features first class features I really love again starter extender infrastructure with one click you can choose a template from a selection of production ready applications there's 75 plus and there's a whole bunch of really nice things said about it on here so at the top what I want to focus on first is pricing and with the pricing you'll see that there is a starter so there's a trial fitted with the platform Sans credit card so you get 512 Megs of ram a gigabyte of disk space and then five dollars or 500 hours of usage whatever comes first and there is Community Support with this documentation and a Discord community you can actually move up to the developer platform which is still free ditch the limits still free under five dollars you can see those limits kind of go up um and then 20 a seat and you get a whole bunch of you know uh limits so for twenty dollars a seat that's not a lot of money for what you get there I'd be interested in starting to compare that with other ones as we go through this process but that seems like a really good one what I love is just the free tier of the developer tier if you have a project like a Blog that maybe is not going to make a ton of money and you need somewhere to host it this seems like a very viable solution and you know on top of the pricing as you see as we'll go throughout this tutorial here it's very easy to use and so we like you know things like being able to connect a GitHub account and I don't have to deploy anything it's actually going to take my source code and create an artifact for me and deploy it to production so all around are really great platforms so far so what I want to do now is Jump On In and in case you are not aware here is the repository for the code that we're using so Dan Vega springblog we are constantly trying to kind of improve this application really just started out as a you know a small rest API we're not trying to do anything fancy here just kind of having a bunch of different features and we want to just use this to deploy to production so if you want to go ahead and check out the repo you can do so there so one thing I want to point out here if you were following along with the last one under Source main resources we had a application Dash Azure yaml file and this was to kind of host all of the configuration for the Azure database I'm going to add I've since added an application Dash prod so this is going to be our production environment I think I was going to have a different configuration for each platform we kind of moved to and I since thought about that and decided that there's really no point in it what is changing from environment to environment or platform to platform is just the data source settings right we're connecting to a database so we need to change those settings so we have one prod file this Azure one will probably go away in the future but this is the one prod one that will change in every different environment um so with that if you want to see the code and want a little bit more of a deeper dive into the code you can check out the first episode again I just want to focus on Railway today and how to get this code into production all right so once you have signed up for an account on Railway you can log in and head over to the dashboard you can see here I am on that starter plan I've used a little bit of it not much but here is the dashboard I have no projects in my system right now and we're going to start fresh and create a new project so it says here create a new project you can deploy a GitHub repository provision a database or create an empty project to start from local so I'm actually going to start with an empty project because I want both a database and a GitHub repo so I'm going to start with the empty project and I'm going to start with the database first so we want to First create a new service so I like this you come in here and you can add a service you can do command K to get this little command man palette so we are going to say a new service here and we are going to create a database and we are going to create a postgres SQL database and so you'll see that this is getting created so once the database is created we can go into it you will come back to this but you have the ability to go ahead and create your tables here we're actually going to use the schema generation in springboot to go ahead and generate the schema for us but once that's created you can see the tables you can alter them here you can also come in and go ahead and query against them which is nice you can go ahead and look at the connection information here you have some logs variables this will point you to all the things that you need to know so what I want to do is actually copy some of this stuff right now so the first thing I'm going to need is the host so I'm going to come in and copy this I'm gonna head over to the repo in the repo I just want to show you this so we have this application Dash prod this is our production environment these are the things we need out of it so we need a host a port a database name a database username and a database password these are the different environment variables that we're going to need so we don't store these directly in here as plain text that's a bad idea because that will get committed to get and then write in plain text for everyone to see so we set those up as environment variables and then we can substitute them at runtime with the appropriate values and the way that we're doing that here is if you look in the readme and go down to railway we have this uh these different properties that we're going to set here these are all the environment variables that we need to set in Railway now you could do these one by one or you can just copy whatever's in here so I'm going to fill in these values and then we can copy whatever's in here and there's a basically an environment variable editor so that you can copy and paste one big chunk of settings instead of doing them one at a time which I find really nice so what else do we need so we have our profile which is going to switch us to the production profile to use this yaml file so we have the host we need a port so let's check out what the port is we're going to copy that and we'll put our Port here we know that by default the proddb name is Railway I haven't figured out a way to change this yet I wish there was a way to customize this I would like to call this spring blog but no big deal for now we need a password so we're going to come in here we're going to look at our password and we're going to copy this and again I'm going to change I'm just going to delete this after I'm done recording this so no big deal and then the username should be postgres user postgres right so we have everything we need here we can go ahead and copy this and I'll show you where we're going to need this in a second so we have our postgres database here the next thing we need to do is add a new service we need to come up here and say new and we're going to say from a GitHub repo you can go ahead and connect to your GitHub repository and figure out which repo you're going to use I'm going to use that spring blog repository that we talked about and it's going to go ahead and set up this project I'm going to go in here quickly and it's going to build this so it's going to build this and deploy it without the environment variables it's not going to work right so we want to come into variables I told you you can go and do this one by one or you can come into the raw editor here and just paste this in so we are setting the active Pro profile to prod here is the host the port the database name the password and the username if we go ahead and update these variables and come back to deployments you'll see that a new one is going to kick off I'm kind of rendering the old one obsolete so we don't really care about this one we care about this one so what I'm going to do is through the magic of video editing we are going to jump ahead to when this one is done building and we'll see if it worked okay so let's look at this we have a success here let's jump into it it should start to show us the logs so you can go through the details of the actual uh what happened here here's the build logs everything looks like it went fine we have the deploy logs you could see that it's starting up it's using the following profile prod because again we set that environment variable for the active profile to be set to prod and you can kind of see anything that's uh going on here so we can kind of scroll over where can we I don't think so there we go and so we should see that this application is started up now with this as a success if you had problems you would see them in here so it's a good place to kind of start if we go over to settings we have some things here that I want to look at so first off in the environment we have this automatic deployments so this is great in the previous one that we did with Azure spring apps we had a we had to basically set up a GitHub action uh to kind of handle automatic deployments for us here Railway is doing this for us and it's saying hey anytime you make a commit to master or maybe a PR and you merge that PR in we are going to automatically deploy that new version for you if you don't want that to be the case and you want to do it manually then you'll want to disable this trigger uh the next is hey we need to expose this service to the public internet so you can generate a quick domain they'll just kind of generate one for you or you can set up a custom one obviously if this was a real project you would set up a custom domain but I like this ability to go ahead and quickly generate a domain so that we can just test this thing out and we'll come back to this in a second so then you have your service here this is the service name this is the repo it's pulling from you have information about your build so this is using something called Nix packs I was not aware of Nix packs until I got into this uh so there there are some deprecated features here for like hiroga build packs or paquito build packs but it's using Nix packs and it's first time I've used it I found it to work very well I haven't I don't have to change anything it just figures out that I'm using Java and spring and it just works so that's really great to see then you have some information about your deploy and then danger if you want to go ahead and remove that and then you also have metrics here and we looked at deployments and variables already so now that I have this domain name I should be able to click on this and we should be able to go visit our application so this is a good sign we're getting a white label layer page this is because we haven't set up a path for the root if you are having issues and you didn't configure something right maybe you didn't set up the data source correctly you would come to a railway error page and then you can go basically jump into the logs that we saw before and maybe get some information from the council to see what's going on this for me is encouraging this tells me that we're on the right path so we're going to go to slash API slash posts and there we go so we have a post in here we have a couple of comments we have a particular author and the way that this is coming up is in our application.prod you can see that I have the SQL init mode turned on to always again you don't want to do this in production I'm just doing this for demo purposes uh we have a an issue in this particular repository to add Flyway and so that will kind of clean some things up uh give us database revisioning a whole bunch of things so we'll get to that at some point but right now I'm just admitting this using this schema so that I don't have to connect to this database and then set the schema manually which you could do but again this is more of a demo so I don't mind turning this on and then in the application we have a command line Runner this is just for Devon prod and I did this in prod because again I don't want to just go manually enter in some data I just want to make sure this is working when we go ahead and launch it on a new service so I add a new uh author I had a new post and a couple of comments and that is the data that you're seeing here so that tells me everything worked uh again pretty easy to get going uh we added a postgres database we added a project if these are the kind of things that you're building and especially I think because of that free tier if it's kind of personal projects that you're working on and just want to get something out into production Railway is a really great option for you and even then I don't have anything running on their paid tier yet but the the tier looks generous it looks like you get a lot with it and for not a lot of money so I think this is a really good option I'd be interested to hear from you guys out there if anyone is running their projects their spring projects on Railway I'd be interested to hear how your experience has been but again just wanted to introduce you to another service another platform to push your projects to we're going to keep doing this we're going to go through a bunch of them just seen how he can take these simple projects and get them out to production so hey I hope you found some value in this if you did please go ahead and leave me a thumbs up subscribe to the channel and as always friends happy coding [Music]
Info
Channel: Dan Vega
Views: 26,131
Rating: undefined out of 5
Keywords: dan vega, spring boot, spring boot tutorial, spring boot for beginners, java spring boot, spring boot project, how to use spring boot, spring production, learn spring boot, spring boot tutorial for beginners, java
Id: 5sVxvF47dcU
Channel Id: undefined
Length: 15min 28sec (928 seconds)
Published: Mon Nov 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.