|
| 1 | +``` |
| 2 | +:::-- rundoc |
| 3 | +email = ENV['HEROKU_EMAIL'] || `heroku auth:whoami` |
| 4 | +
|
| 5 | +Rundoc.configure do |config| |
| 6 | + config.project_root = "deploying-spring-boot-apps" |
| 7 | + config.filter_sensitive(email => "developer@example.com") |
| 8 | +end |
| 9 | +``` |
| 10 | +<!-- |
| 11 | + rundoc src: |
| 12 | + https://github.com/heroku/java-getting-started/blob/master/deploying-spring-boot-apps.md |
| 13 | +
|
| 14 | + Command: |
| 15 | + $ rundoc build --path deploying-spring-boot-apps.md |
| 16 | +--> |
| 17 | + |
| 18 | +The Spring Boot model of deploying standalone applications is a great |
| 19 | +fit for Heroku. You can use either Maven or Gradle to deploy a Spring application on Heroku, but for this guide we'll assume that you're using Maven and have [Maven 3](http://maven.apache.org/download.html) installed on your machine. |
| 20 | + |
| 21 | +To begin, create a [free Heroku account](https://signup.heroku.com/). |
| 22 | +Then download and install the Heroku CLI. |
| 23 | + |
| 24 | +<a class="toolbelt" href="https://cli.heroku.com">Download the Heroku CLI</a> |
| 25 | + |
| 26 | +Once installed, you can use the `heroku` command from the terminal to log in using the email address and password you used when creating your Heroku account: |
| 27 | + |
| 28 | +```term |
| 29 | +$ heroku login |
| 30 | +heroku: Press any key to open up the browser to login or q to exit |
| 31 | + › Warning: If browser does not open, visit |
| 32 | + › https://cli-auth.heroku.com/auth/browser/*** |
| 33 | +heroku: Waiting for login... |
| 34 | +Logging in... done |
| 35 | +Logged in as me@example.com |
| 36 | +``` |
| 37 | + |
| 38 | +To check that your key was added, run `heroku keys`. If your key isn’t there, you |
| 39 | +can add it manually by running `heroku keys:add`. For more information about SSH |
| 40 | +keys, see [Managing Your SSH Keys](https://devcenter.heroku.com/articles/keys). |
| 41 | + |
| 42 | +## Creating a Spring Boot app |
| 43 | + |
| 44 | +To create a new Spring Boot application, first install the Spring Boot CLI as described in the [Spring Boot documentation](https://docs.spring.io/spring-boot/docs/current/reference/html/cli-using-the-cli.html). This will add a `spring` command to your path. |
| 45 | + |
| 46 | +>note |
| 47 | +>You can also start with a working [sample app](https://github.com/heroku/java-getting-started) if you'd prefer. |
| 48 | +
|
| 49 | +Use the CLI to create a new application by running this command: |
| 50 | + |
| 51 | +```term |
| 52 | +:::>- $ spring init --dependencies=web demo |
| 53 | +``` |
| 54 | + |
| 55 | +Then move into the application directory: |
| 56 | + |
| 57 | +```term |
| 58 | +:::>- $ cd demo |
| 59 | +``` |
| 60 | + |
| 61 | +The application does not have any custom logic by default -- it's just an empty template. To add some behavior, open the `src/main/java/com/example/demo/DemoApplication.java` file and put the following code in it: |
| 62 | + |
| 63 | +```java |
| 64 | +:::>> file.write src/main/java/com/example/demo/DemoApplication.java |
| 65 | +package com.example.demo; |
| 66 | + |
| 67 | +import org.springframework.boot.SpringApplication; |
| 68 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 69 | +import org.springframework.web.bind.annotation.*; |
| 70 | +import org.springframework.stereotype.*; |
| 71 | + |
| 72 | +@Controller |
| 73 | +@SpringBootApplication |
| 74 | +public class DemoApplication { |
| 75 | + |
| 76 | + @RequestMapping("/") |
| 77 | + @ResponseBody |
| 78 | + String home() { |
| 79 | + return "Hello World!"; |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(String[] args) { |
| 83 | + SpringApplication.run(DemoApplication.class, args); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +This creates a simple request mapping that displayed "Hello World!" in the browser. You could run the application locally to confirm this, but we'll jump straight to running it on Heroku. |
| 89 | + |
| 90 | +## Preparing a Spring Boot app for Heroku |
| 91 | + |
| 92 | +Before you can deploy the app to Heroku, you'll need to create a Git repository for the application and add all of the code to it by running these commands: |
| 93 | + |
| 94 | +```term |
| 95 | +:::>- $ git init |
| 96 | +:::>- $ git add . |
| 97 | +:::>- $ git commit -m "first commit" |
| 98 | +``` |
| 99 | + |
| 100 | +You'll deploy the app by pushing this Git repo to Heroku. It's also possible to deploy using the [Heroku Maven plugin](deploying-java-applications-with-the-heroku-maven-plugin), but this guide will focus on using Git and the Heroku CLI. |
| 101 | + |
| 102 | +In order to deploy to Heroku, you'll first need to provision a new Heroku app. Run this command: |
| 103 | + |
| 104 | +```term |
| 105 | +:::>> $ heroku create |
| 106 | +``` |
| 107 | + |
| 108 | +This also creates a remote repository called `heroku` in |
| 109 | +your local git repo. Heroku generates a random name (in this case `nameless-lake-8055`) |
| 110 | +for your app. You can rename it later with the `heroku apps:rename` command. |
| 111 | + |
| 112 | +Now deploy your code: |
| 113 | + |
| 114 | +```term |
| 115 | +:::>- $ git push heroku master |
| 116 | +:::-> | $ (head -6; echo "..."; tail -18) |
| 117 | +``` |
| 118 | + |
| 119 | +Heroku automatically detects the application as a Maven/Java app due to the presence of a `pom.xml` file. It installed Java 8 by default, but you can easily configure this with a `system.properties` file as described in the [Specifying a Java version](https://devcenter.heroku.com/articles/java-support#specifying-a-java-version) Dev Center article. It will run your app using the [default command](https://devcenter.heroku.com/articles/java-support#default-web-process-type). |
| 120 | + |
| 121 | +All that said, the application is now deployed. You can visit the app's URL by running this command: |
| 122 | + |
| 123 | +```term |
| 124 | +:::>- $ heroku open |
| 125 | +``` |
| 126 | + |
| 127 | +You'll see the "Hello World!" text in the browser. |
| 128 | + |
| 129 | +You can view the logs for the application by running this command: |
| 130 | + |
| 131 | +```term |
| 132 | +:::>- background.start("heroku logs --tail", name: "tail", wait: "State changed from starting to up", timeout: 45) |
| 133 | +:::-> | tail -10 |
| 134 | +:::-- background.stop(name: "tail") |
| 135 | +``` |
| 136 | + |
| 137 | +Reload your application in the browser, and you’ll see another log message generated for that request. Press `Control+C` to stop streaming the logs. |
| 138 | + |
| 139 | +To learn more about the basics of deploying a Maven-based Java application on Heroku, try following the [Getting Started with Java on Heroku](getting-started-with-java) guide. This guide covers many steps that are not specific to Spring Boot. |
| 140 | + |
| 141 | +The remainder of this article provides a cursory overview of some of the most common settings you'll need to adjust. |
| 142 | + |
| 143 | +## Connecting to a database |
| 144 | + |
| 145 | +You can attach a PostgreSQL database to your app by running the following command from the CLI: |
| 146 | + |
| 147 | +```term |
| 148 | +:::>- $ heroku addons:create heroku-postgresql |
| 149 | +``` |
| 150 | + |
| 151 | +If you prefer to use MySQL or another database vendor, check out [Add-ons Marketplace](https://elements.heroku.com/addons) to see what is available. |
| 152 | + |
| 153 | +Now you can list the configuration variables for your app to display the URL needed to connect to the database, DATABASE_URL: |
| 154 | + |
| 155 | +```term |
| 156 | +:::>> $ heroku config |
| 157 | +``` |
| 158 | + |
| 159 | +Heroku also provides a `pg` command that shows a lot more: |
| 160 | + |
| 161 | +```term |
| 162 | +:::>> $ heroku pg |
| 163 | +``` |
| 164 | + |
| 165 | +This indicates a hobby database (free) is running Postgres 9.3.3, with a single row of data. |
| 166 | + |
| 167 | +Once the database add-on has been created, Heroku will automatically populate the environment variables `SPRING_DATASOURCE_URL`, `SPRING_DATASOURCE_USERNAME`, and `SPRING_DATASOURCE_PASSWORD`. These environment variables should allow your Spring Boot application to connect to the database without any other configuration as long as you add a PostgreSQL JDBC driver to your dependencies like so: |
| 168 | + |
| 169 | +```xml |
| 170 | +:::>> file.append("pom.xml#26") |
| 171 | +<dependency> |
| 172 | + <groupId>org.springframework.boot</groupId> |
| 173 | + <artifactId>spring-boot-starter-jdbc</artifactId> |
| 174 | +</dependency> |
| 175 | +<dependency> |
| 176 | + <groupId>org.postgresql</groupId> |
| 177 | + <artifactId>postgresql</artifactId> |
| 178 | +</dependency> |
| 179 | +``` |
| 180 | + |
| 181 | +You can customize your application's database configuration in your `application.properties`. For example: |
| 182 | + |
| 183 | + |
| 184 | +```properties |
| 185 | +:::>> file.write src/main/resources/application.properties |
| 186 | +spring.datasource.driverClassName=org.postgresql.Driver |
| 187 | +spring.datasource.maxActive=10 |
| 188 | +spring.datasource.maxIdle=5 |
| 189 | +spring.datasource.minIdle=2 |
| 190 | +spring.datasource.initialSize=5 |
| 191 | +spring.datasource.removeAbandoned=true |
| 192 | +``` |
| 193 | + |
| 194 | +Then you can then add a configuration bean to your app. |
| 195 | + |
| 196 | +```java |
| 197 | +:::>> file.write src/main/java/demo/DatabaseConfig.java |
| 198 | +package com.example.demo; |
| 199 | + |
| 200 | +import com.zaxxer.hikari.*; |
| 201 | +import org.springframework.beans.factory.annotation.Value; |
| 202 | +import org.springframework.context.annotation.*; |
| 203 | +import javax.sql.DataSource; |
| 204 | + |
| 205 | +@Configuration |
| 206 | +public class DatabaseConfig { |
| 207 | + |
| 208 | + @Value("${spring.datasource.url}") |
| 209 | + private String dbUrl; |
| 210 | + |
| 211 | + @Bean |
| 212 | + public DataSource dataSource() { |
| 213 | + HikariConfig config = new HikariConfig(); |
| 214 | + config.setJdbcUrl(dbUrl); |
| 215 | + return new HikariDataSource(config); |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +``` |
| 221 | +:::-- $ git add . |
| 222 | +:::-- $ git commit -m "database" |
| 223 | +:::-- $ git push heroku master |
| 224 | +:::-- $ cd .. |
| 225 | +:::-- $ mv demo deploying-spring-boot-apps |
| 226 | +``` |
| 227 | + |
| 228 | +For more information, see [Connecting to Relational Databases on Heroku with Java](articles/connecting-to-relational-databases-on-heroku-with-java). |
| 229 | + |
| 230 | +Now your application should be able to connect to the database. You can follow our guide on [Running Database Migrations for Java Apps](https://devcenter.heroku.com/articles/running-database-migrations-for-java-apps) to initialize the database. The [sample app](https://github.com/kissaten/spring-boot-heroku-demo) uses Liquibase. |
| 231 | + |
| 232 | +## Customizing the boot command |
| 233 | + |
| 234 | +You can override the default command used to run your app or define custom process types using a [`Procfile`](https://devcenter.heroku.com/articles/procfile). The correct command depends on what you need to do with your app. Common process types are used to [run a web process](https://devcenter.heroku.com/articles/java-support#default-web-process-type) or [run database migrations](https://devcenter.heroku.com/articles/running-database-migrations-for-java-apps). |
| 235 | + |
| 236 | +## Next steps |
| 237 | + |
| 238 | +For more complete examples of Spring Boot apps that run on Heroku see: |
| 239 | + |
| 240 | +* [Getting Started on Heroku with Java](articles/getting-started-with-java#introduction) |
| 241 | +* [Spring Petclinic Demo for Heroku](https://github.com/kissaten/spring-petclinic) |
| 242 | + |
| 243 | +Heroku provides a wide range of features for Spring applications. You can provision |
| 244 | +add-ons that introduce third-party cloud services like persistence, logging, monitoring and more. |
| 245 | +The [add-on marketplace](https://elements.heroku.com/addons/#data-stores) has a large |
| 246 | +number of data stores, from Redis and MongoDB providers, to Postgres and MySQL. |
| 247 | + |
| 248 | +You can read more about [How Heroku Works](https://devcenter.heroku.com/articles/how-heroku-works) |
| 249 | +to get a technical overview of the concepts you’ll encounter while writing, configuring, deploying and running applications. |
| 250 | +Then visit the [Java category on Dev Center](https://devcenter.heroku.com/categories/java-support) |
| 251 | +to learn more about developing and deploying Spring applications. |
| 252 | +If you experience any trouble with your application as you migrate to Heroku, reach out to any of our [Support channels](https://devcenter.heroku.com/articles/support-channels). |
| 253 | + |
| 254 | +For more information on deploying Spring apps, see the [Spring documentation](http://projects.spring.io/spring-boot/#quick-start) and the [Spring Boot documentation on deploy to Heroku](http://docs.spring.io/spring-boot/docs/current/reference/html/cloud-deployment.html#cloud-deployment-heroku). |
0 commit comments