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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everyone Dan Vega here and today we're continuing on in our series spring to production the idea behind this is that we talk a lot about how to build spring applications but we don't talk enough about how to get those applications into the promised land which is known as production so the idea is simple we've taken a simple application we're calling springblog it's a spring MVC app it connects to a database it has the actuator it has just a few features and what we're doing is we're taking that and deploying it to different platforms one this gives us some visibility into the platforms that are available to host our spring applications and two just kind of seeing how we get things into production I've mentioned this before but one of the things I really love about front-end development is the ability to just get something out there so you can deploy something to the web HTML CSS and JavaScript and it's very tangible it becomes very tangible at that point because you can see it share it interact with it and and that's what I'm really trying to get at here is we're building these applications let's get them out to production so today we are taking a look at Heroku most of you might have heard of Heroku some of you probably already used to Roku but what I really love about Heroku and some of the other platforms that we've taken a look at is that it takes kind of the Ops part out of it and just allows you to focus on building applications I'm not a devops engineer a lot of that stuff is confusing for me I just want to build my app and get it out there and that is one thing Heroku loves to do for us now I do want to get something out of the way you'll see down in the middle here it says the Heroku update starting November 28 2022 which is really in a couple weeks at the time of this recording the free Heroku dinos the free heroka postgres and free Heroku data ferretas are no longer available so there used to be a free tier for all this stuff there isn't going to be any more with that said I still believe Heroku is a great platform to choose we'll go into the pricing in a second but there are still some really great pricing options for you there so I like to kind of go through the website first let's take a look at what's available to us I like this kind of sliding animation that gives us some ability to kind of look at what the features are the one that really stands out for me is this and this is what I said before focus on your apps invest in apps not Ops Heroku handles the hard stuff all the patching the upgrading Etc let Heroku handle that for you let's just focus in on building our applications so we see there's a bunch of officially supported languages down here so if you're building polyglide applications and you're using things like Java and go and Scala enclosure that's great there's also a podcast by the folks who run Roku really great podcast I've listened to it for a while called kodish so check that off out if you have a chance and then you can just go through here and kind of look at all the different features built by Developers for developers we have the ability to scale code and data rollbacks the GitHub integration is a big one we looked at a platform called Railway previously Heroku gives us that same kind of ability where we can just hook up to a GitHub repo and have it deploy our code for us which is what I want there's a Heroku postgres so reliable and secure postgres as a service easy to set up that's what I'm looking for I don't want to like have to manually create a database and do all the kind configuring and patching none of that sounds like fun to me so this is right up my alley and I'm excited to get going so before we create our application on Heroku I just want to look at the pricing again because that free tier is going away it doesn't mean you can't get started for pretty easy so they have a free and hobby zero and up per month you can choose your dinos so this is the free tier which I imagine this one's kind of going away there's a hobby one though with a Max of seven dollars per month so if you're just like trying to learn something and want to get something out in production uh seven bucks a month not a bad idea um and then there's pricing on the postgres side again hobby basic Max of nine bucks per month so for 16 bucks a month you can have a spring app talking to a database as just a hobby so and then as your needs grow maybe this hobby turns into a business or something that's generating Revenue you can kind of just scale that up and move that over to a different plan as your needs grow so definitely worth it even with the free tier going away you'll see as we get into this just the ease of use for services for getting your project started so easy to get up and running that's why I continue to recommend Heroku so with that let's jump into the admin dashboard and create a new project all right we're going to start here over on GitHub so the repository that we're using for all of these different platforms that we're pushing to is on my GitHub repository at danvega Spring Dash blog if this is your first time here go ahead and take a look around the repository take a look at the application you can run it locally there's a Docker compose file to stand up a postgres database locally and just test it out again pretty simple basic application not much to it but one thing I want to point out is if we go into source and we go into Main and resources you'll see we have some different yaml files here for configuration one of which is a a prod so we're going to set the active profile to prod when we go to production and this is how we're going to configure the data source I each platform that we're going to we're using postgres so the URL looks the same jdbc postgres and then we have a DB host port and DB name and DB username and password so we're using environment variables there we don't want to store that as plain text that's bad because that would get checked in here to get as plain text and for the world to see so we're using environment variables we'll set those environment variables up in the different platforms and they'll get replaced at runtime I'm also setting the SQL init mode to always this means that it will grab the schema file in the project and run that for us so it creates the three tables that we need again not something you do in production but this is more of a demo to just see it like once we get it deployed we want to see the application up and running so that's the prod side of things and then if you go into the spring blog there's a readme a whole bunch of information in there and then there's specific information about each platform if I need to give you something for that platform you'll see it here in the readme the one thing I do want to point out that is specific to Heroku is this system.properties again I'll probably come back to this because without this you'll come across some errors we're adding the system.properties file so if you're creating your own project make sure you add this and you set the Java runtime to whatever you're using because we're using Java 17 I'm setting the runtime to 17 this will tell Heroku hey this is the runtime you need to use without it it will default to whatever the default is I want to say it was eight but now I'm thinking it's 11. so whatever the default is it's going to run on that and then if you are trying to use 17 and the defaults 11 you're going to have some problems so you'll need this system.properties to be able to run this example or run your own example on Heroku all right so with that I am logged into the heroka dashboard I have no projects here I'm going to create a new app so I'm going to click create app we're going to give it a name so this name needs to be unique so I'm going to say Dan Vega slash blog and that looks like it's available where in which region are we going to choose and then I'm going to go ahead and create that app okay so we have information about the pipeline here um what I want to do is check out the deployment method so the way that I'm going to do this is I'm going to connect to GitHub and once I do that it'll ask me to search this repository I'm going to look for spring blog and we're going to search and there it is so I want to connect to that specific Repository and now as you can see we have some information here about automatic deploys or manual deploys so just like we did in another application platform like Railway you can enable automatic deploys which is really great so you could say hey anytime something is committed to the master Branch I want you to go ahead and create a new artifact from that deploy it and let's get this new code out into production this may be your desired effect it may not be so you have to opt into this you have to say hey I want to enable this or you can do a manual deploy you could say hey I'm ready I've made my changes go ahead and deploy this I'm not going to deploy this yet because we need to set up a few things for this to work first if you go over to overview you will see that we have some installed add-ons Dyno info and collaborative activity here so what I want to do is configure some add-ons so if I want to go into add-ons I want to find an add-on that I can go ahead and add to this so I'm going to go ahead and look for Heroku postgres so I'm going to choose that and now what it's going to ask is okay by choosing one of these this is what we are going to go ahead and add to this particular project in this case I'm just choosing the hobby Dev which is free at this point now remember when this goes away basic will probably be the only option here which is up to nine dollars a month right so that would be a good place to start if this is just kind of a hobby project I'm going to choose free submit order form all right and then I'll come in here and we have this attached as database and we'll say yes and now it'll basically install this as an add-on so now we have this database that is a part of our application now if we go over to our settings and we reveal our config vars we'll see that we have this database URL and it gives us this URL well that's great we have our own settings that we need to set up right we have all of those environment variables that we've created in our production yaml configuration file and those are the ones we need to set up so I'm actually going to set those up but to do that I need to go back and get that database so let's go over to resources let's let's go over here and now it's going to give us information about that database okay so here we are we have that database we can go into settings here and then we can go to database credentials so we're going to view our credentials and this is all the information I need so now we're going to have to line that up with whatever we have in application Dash prod Dash yaml so we're going to need a DB host DB port and DB name so let's start there so I'm going to come back here and go into settings and in config vars I'm going to say DB host and that DB host is going to be this so we'll add and then again just to make sure we're getting the right ones here we're going to say DB port this should be 54.32 it is that is the default port for postgres and we're going to say add um the next is the prod DB name so let's go ahead and add that as a setting and then we'll copy whatever that DB name is here and then finally we'll need a username proddb username and we'll get the username here and finally we'll just say prod DB password and finally we'll get that password here so again I'm going to delete this after we're done okay and so that is all of the configuration I should need to do so with the configuration set up for the database and our we're set up here and under deploy we should be able to go ahead and deploy it now just remember if you're doing this with another application please make sure you have this system.properties lined up to with whatever whatever you're using so again I'm using Java 17 here so that's why I have the system properties the java.r runtime.version set to 17. so let's see if we've done everything correctly and let's go ahead and try to deploy this okay so it says deployed to Heroku was successful again you can kind of see the process here it went and got our GitHub code or when got our code from GitHub it built the master Brands it had a release page and a phase and deployed to Heroku so your app was successfully deployed let's go ahead and click View again if you've been following along this is a good thing here because we have this white label air page we don't have something set up for the root context but if we go to slash API slash posts we do have our post here so this is our first blog post with the correct author and a couple of comments on it again if you're new here the way that we're getting this is we set the SQL init mode that runs this schema which adds a author post and comment table and then here in the application main application class we have a command line runner for both Dev and prod that will create a new author create a new post and add two comments to it so that's how we get that default data in there again this is not something you will do in production but for this demo purpose to have something ready to go when we go ahead and launch it on a platform works out well so I think the takeaway from this is there isn't a lot that you have to do here in Heroku they've made it extremely easy and I've done this on other platforms where sometimes it works sometimes it doesn't it's very finicky this just seems to work every time for me they have a tried and tested platform simple but powerful and then you can come in here and again you can check out your resources if you need to add other resources you can if you can come in and get metrics so you can add a credit card here to install metrics to it you can get activity about what's going on you can have collaborators on this project and again there are some other settings that can come in here and kind of tweak so if you want an SSL if you want a specific domain name a custom domain name you can which is great so we get this nice custom this this kind of uh starter domain name which allows other people to go ahead and view this on the public internet which again is great we get that very tangible resource that we can pass around to someone to check out so just a big fan of Heroku they make you know they again they're in the business of letting you worry about building apps and not focusing in on Ops and that for me is that's my wheelhouse that's what I want to do I want to build an app I don't want to worry about having to configure all of this stuff so a big big fan of Heroku the pricing model is really good just makes everything so easy worry about focusing on building your apps and not all the other stuff that's what Heroku will give you so hey I hope you found some value in this if you did do me a favor give me a thumbs up hit subscribe to the channel and as always friends happy coding [Music]
Info
Channel: Dan Vega
Views: 11,532
Rating: undefined out of 5
Keywords: dan vega, spring boot, java, spring framework, heroku, spring boot heroku, spring boot heroku deploy, spring boot heroku postgres
Id: lGtTOLKuvqs
Channel Id: undefined
Length: 16min 37sec (997 seconds)
Published: Tue Nov 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.