Deploying with an Embedded Web Server5:50 with Chris Ramacciotti
This video demonstrates the quickest deployment method, and that is to use Gradle to run the `bootRun` task, which comes from the Gradle 'spring-boot' plugin.
Configuring the Embedded Server
When launching a Spring Boot application as a standalone app with an embedded web server, such as Tomcat or Jetty, you can configure various properties, like changing the port or enabling SSL.
Below is an example of enabling SSL on a specified port using a self-signed certificate.
Note that in production, you'd want to have a certificate signed by a third-party so that users don't get browser warnings.
To generate a self-signed certificate, you can use the JDK's
keytool utility as follows:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
This will ask you a handful of questions, and upon finishing will generate a file named keystore.p12. Move this file to src/main/resources/keystore.p12.
Then, in application.properties, add the following properties:
server.port: 8443 server.ssl.key-store: classpath:keystore.p12 server.ssl.key-store-password: PASSWORD_USED_WITH_KEYTOOL server.ssl.keyStoreType: PKCS12 server.ssl.keyAlias: tomcat
Now, when you run the app with
./gradlew bootRun, you can visit https://localhost:8443 and see your app served via HTTPS. You'll likely see a browser warning since you're using a self-signed certificate, but if you accept the warning, you'll be able to proceed.
Using Github With This Course
You can complete this course by using the code I've made available to you in two ways:
- Use the Github repository I've made available (recommended), or
- Use the project files linked at the bottom of this page
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse-projects/spring-deploy-weather.git
The project we're going to deploy is a spring boot weather app written 0:00 with spring MVC. 0:03 To get started, I'll pull down this repo from GitHub. 0:04 So let me copy the project URL here. 0:08 And I'll switch to my terminal window here. 0:10 And I'll do a git clone, paste that URL. 0:13 Excellent, now that I have the source pulled down I'm gonna open IntelliJ 0:19 to import the project. 0:23 So I'll open the project, I'll navigate to code Screencast and 0:25 spring-deploy-whether. 0:31 And it detects that this is a Gradle project, so I will click OK. 0:35 And let IntelliJ import all of those assets. 0:40 Great, I'm going to add my remote url there for the GitHub repository. 0:47 And if I open the project navigator here, I will see that under the source 0:53 directory I have both main application code, as well as test code. 0:57 I'm not gonna go through all the code in this application. 1:01 I'll let you do that if you so choose. 1:05 There's a handful of things though that I'll be pointing out throughout the course 1:07 of this workshop, as it pertains to our discussion along the way. 1:10 For now, suffice it to say that this is a spring MVC app running with Thymeleaf, 1:12 Hibernate, and H2 and written using the service DAL pattern. 1:17 And you might recognize this project from another one of our spring workshops. 1:21 If you've spent any time in our library learning spring. 1:25 Then you're definitely familiar with our first form of deployment. 1:28 We do this by running the gradle task called boot run 1:31 which comes from the spring boot gradle plugin. 1:34 Let me show you that in the gradle file. 1:36 Towards the top of the file here, you'll see this apply plugin spring boot and 1:41 it says plugin that gives us the boot run task. 1:45 If I open the Gradle tool window, and I expand Tasks and application. 1:47 There it is, right there. 1:52 Now what this task does, is execute your Byte code, directly, 1:54 without creating any sort of artifact like a JAR. 1:57 You can execute this Gradle task, either here or through the Gradle tool window, 2:00 when Intellij, or whatever IDE you're using. 2:03 Or you can use a Gradle wrapper to execute the task via the command line. 2:06 The Gradle wrapper's like a portable mini version of Gradle that you can 2:10 distribute with your application. 2:13 It's a nice option because it allows other developers to build and deploy quickly. 2:15 Let's check out the command line now. 2:19 I'm going to collapse my project navigator, Gradle tool window, and 2:21 pop open a terminal window here. 2:25 Let me give myself some more space to work with. 2:27 All right, excellent. 2:32 Now with this terminal in the project root, 2:33 we can see all the files with the ls command. 2:35 And it's this gradlew bash script, in the current directory that we want to execute, 2:38 passing the name of the task, as a command line argument. 2:44 If you're on Windows, you're going to use gradlew.bat. 2:47 So to run the boot run task we type ./ for the current directory and 2:51 then gradlew for the gradle wrapper. 2:55 Again gradlew.bat if you're on Windows, 2:57 followed by the name of the gradle task that we want to run here it is bootRun. 3:01 The boot run task depends on the compiled Java task, 3:07 which will download any dependencies that aren't available. 3:09 Again, this task does not create any sort of archive like a JAR file. 3:13 We'll do that in the next video. 3:17 This simply launches our application directly 3:19 with an embedded web server called Tomcat. 3:21 If you want to verify that this application is indeed up and running, 3:24 you can switch to a browser and open localhost. 3:28 And there's our weather app there. 3:34 Now back to my build file here, let me pull this down. 3:38 If I scroll down to the dependencies, 3:44 you'll see the spring -boot-starter-thymeleaf dependency and 3:46 included in that is the spring-boot-starter-web dependency. 3:50 Now this includes Tomcat as a web server. 3:54 If instead you'd like to include Jeti, 3:58 you can exclude the Tomcat module in the Gradle build file like this. 4:01 So what I can do here is surround this with parentheses. 4:06 And then inside curly braces I can choose to exclude 4:13 the module named spring-boot-starter-tomcat. 4:18 And then I can add the jetty dependency. 4:25 So I can say compile org.springframework.boot and 4:28 spring-boot-starter-jetty, just like that. 4:35 Now for test environments, Jetti often boots faster and has a smaller footprint 4:41 so this might be your motivation to use Jetti instead of Tomcat. 4:45 Okay let's kill our currently running app and rerun the boot run task. 4:49 So I'm gonna go back down to my terminal and 4:53 with that in focus I'm gonna hit Ctrl+C, that should kill the current task. 4:56 I'm gonna clear my console. 5:01 And then again, I'm gonna arrow up to gradlew bootRun. 5:03 Now as the output flies by you'll eventually see that Jetti, 5:08 as opposed to Tomcat, has successfully started. 5:11 Let's wait to see that that was indeed the case. 5:14 Now if we scroll up a little bit above this hibernate stuff, 5:22 we see that Jetty started on port 8080. 5:26 And if we flip back to Chrome and 5:29 refresh the page, we'll see that our weather app is still running. 5:31 Now if you're looking to run a spring application in production with 5:37 an embedded web server. 5:40 There are a bunch of configuration options available 5:41 to you with the boot run task including enabling HTTPS. 5:44 Check the teacher's notes for more. 5:48
You need to sign up for Treehouse in order to download course files.Sign up