Heroku is a platform as a service (PaaS) that help us to build, run, and operate applications entirely in the cloud. So in the tutorial, we show you how to deploy SpringBoot web application with PostgreSQL in Heroku.
Related posts:
– How to use Spring JPA with PostgreSQL | Spring Boot
– How to Deploy Angular application on Heroku hosting with Git repository
Contents
Technologies
– Heroku
– Maven
– SpringBoot
Goal
We deploy a SpringBoot with PostgreSQL to Heroku platform:
Practice
Setup Heroku CLI
Download & Install Heroku CLI
For detail go to the Download Heroku CLI
– Install Heroku CLI on Linux:
$sudo snap install --classic heroku
-> Details:
root@gkz-vps:~# sudo snap install --classic heroku 2018-12-14T07:04:55Z INFO Waiting for restart... heroku v7.19.3 from Heroku? installed
Login to Heroku
Use heroku login
cmd to login:
Add Heroku SSH keys
– To check your key was added, run heroku keys
– If your key isn’t there, you can add it manually by running heroku keys:add
Deploy simple SpringBoot Web Application on Heroku
Create SpringBoot Project
Use SpringToolSuite to create a SpringBoot project.
– Add web
dependency ->
org.springframework.boot spring-boot-starter-web
– Create a simple RestAPI ->
package com.grokonez.restapi.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RestAPIsController { @RequestMapping("/api/helloworld") public String findAll(){ return "Hello World!"; } }
Create a Git Repository
Go to Git Account, create a Git Repository:
– Add all of the code to Git by running these commands:
$git init $git add . $git commit -m "first commit" $git remote add origin https://github.com/grokonez/springbootapp.git $git push -u origin master
Create a new Heroku App
– Use the below commandline to create Heroku App:
heroku create
-> Details:
-> It creates a new Heroku App with random name: stark-depths-44428
– We can rename the app using the cmd ->
$ heroku apps:rename --app oldname newname
-> Details:
Deployment
– Now deploy your code by commandline:
git push heroku master
-> Logs:
root@gkz-vps:/home/grokonez/SpringBoot# git push heroku master Enumerating objects: 27, done. Counting objects: 100% (27/27), done. Compressing objects: 100% (18/18), done. Writing objects: 100% (27/27), 48.27 KiB | 3.02 MiB/s, done. Total 27 (delta 0), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Java app detected remote: -----> Installing JDK 1.8... done remote: -----> Executing: ./mvnw -DskipTests clean dependency:list install remote: [INFO] Scanning for projects... remote: [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.5.18.RELEASE/spring-boot-starter-parent-1.5.18.RELEASE.pom ... remote: [INFO] Installing /tmp/build_8db0d85a6115a0f83397c77646c3d027/target/SpringBootAPIs-0.0.1.jar to /app/tmp/cache/.m2/repository/com/grokonez/restapis/SpringBootAPIs/0.0.1/SpringBootAPIs-0.0.1.jar remote: [INFO] Installing /tmp/build_8db0d85a6115a0f83397c77646c3d027/pom.xml to /app/tmp/cache/.m2/repository/com/grokonez/restapis/SpringBootAPIs/0.0.1/SpringBootAPIs-0.0.1.pom remote: [INFO] ------------------------------------------------------------------------ remote: [INFO] BUILD SUCCESS remote: [INFO] ------------------------------------------------------------------------ remote: [INFO] Total time: 16.273 s remote: [INFO] Finished at: 2018-12-14T07:48:38Z remote: [INFO] ------------------------------------------------------------------------ remote: -----> Discovering process types remote: Procfile declares types -> (none) remote: Default types for buildpack -> web remote: remote: -----> Compressing... remote: Done: 63.1M remote: -----> Launching... remote: Released v3 remote: https://grokonez-springboot-app.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy... done. To https://git.heroku.com/grokonez-springboot-app.git * [new branch] master -> master root@gkz-vps:/home/grokonez/SpringBoot#
-> Due to the presence of a pom.xml
file, Heroku automatically detects the application as a Maven/Java app.
Heroku installed Java 8 by default, but you can easily configure this with a system.properties
. Details ->
– Create a file system.properties
in your root folder of application.
– Set a property java.runtime.version=11
in the file. Accepted major version values are 1.7
, 1.8
, 9
, 11
.
– The Java default is 1.8
-> Visit the app’s URL by running this command: heroku open
SpringBoot Logs
– Use heroku logs --tail
to see Logs ->
Deploy SpringBoot Web App with PostgreSQL on Heroku
Heroku Attach PostgreSQL Database
– Attach a PostgreSQL database to your application by cmd:
heroku addons:create heroku-postgresql
– List the configuration variables by cmd: heroku config
& heroku pg
SpringBoot JPA Project
Modify above SpringBoot project as below structure:
– Add JPA
& PostgreSQL
dependencies:
org.springframework.boot spring-boot-starter-data-jpa org.postgresql postgresql runtime
– Configure Spring JPA:
-> Open application.properties
, add configuration:
spring.datasource.url=postgres://shuqlwwtsyxrgt:7a57682ccb0387ae55a3d1c727ded24d0d4b88d062bc1b485db2d036e4af5ea1@ec2-174-129-41-12.compute-1.amazonaws.com:5432/d5d0b8ps2mag2n spring.datasource.username= spring.datasource.password= spring.jpa.generate-ddl=true
– Create a Customer.java
model:
package com.grokonez.restapi.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "customer") public class Customer{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(name = "firstname") private String firstName; @Column(name = "lastname") private String lastName; protected Customer() { } public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); } }
– Create Spring JPA Repository Interface:
public interface CustomerRepository extends CrudRepository{ List findByLastName(String lastName); }
– Update RestAPIsController
as below:
package com.grokonez.restapi.controller; import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.grokonez.restapi.model.Customer; import com.grokonez.restapi.repository.CustomerRepository; @RestController public class RestAPIsController { @Autowired CustomerRepository repository; @RequestMapping("/save") public String process(){ // save a list of Customers repository.save(Arrays.asList(new Customer("Jack", "Smith"), new Customer("Adam", "Johnson"), new Customer("Kim", "Smith"), new Customer("David", "Williams"), new Customer("Peter", "Davis"))); return "Done"; } @RequestMapping("/findall") public String findAll(){ String result = ""; for(Customer cust : repository.findAll()){ result += cust.toString() + "
"; } return result; } @RequestMapping("/findbyid") public String findById(@RequestParam("id") long id){ String result = ""; result = repository.findOne(id).toString(); return result; } @RequestMapping("/findbylastname") public String fetchDataByLastName(@RequestParam("lastname") String lastName){ String result = ""; for(Customer cust: repository.findByLastName(lastName)){ result += cust.toString() + "
"; } return result; } }
Deployment
– Add the change to Git Repository ->
$git add . $git commit -m "upadate commit" $git push -u origin master
-> Deploy code:
git push heroku master
->
remote: [INFO] ------------------------------------------------------------------------ remote: [INFO] BUILD SUCCESS remote: [INFO] ------------------------------------------------------------------------ remote: [INFO] Total time: 8.189 s remote: [INFO] Finished at: 2018-12-14T08:32:56Z remote: [INFO] ------------------------------------------------------------------------ remote: -----> Discovering process types remote: Procfile declares types -> (none) remote: Default types for buildpack -> web remote: remote: -----> Compressing... remote: Done: 74.6M remote: -----> Launching... remote: Released v4 remote: https://grokonez-springboot-app.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy... done. To https://git.heroku.com/grokonez-springboot-app.git 4b1dd40..17e6416 master -> master
– Results:
-> Logs Deployment:
-> PostgreSQL database:
-> RestAPIs requests:
Muchas gracias. ?Como puedo iniciar sesion?