Sunday, 7 May 2017

How to Deploy Spring Boot to Heroku

Heroku is a popular PaaS platform.

For a window OS, installing Heroku CLI first; download it from Heroku homepage.
Starting Windows cmd, type in
$heroku login; 
input login email and pwd;
Switching to your working directory.

Create a new Git repository
Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a yardnbud

Deploy the App
(1) create an app on Heroku, which prepares Heroku to receive your source code. 
$heroku create   
or create a named app. 
$heroku apps:create myapp

(2) deploy your code in the heroku
$git push heroku master

ensure that at least one instance of the app is running
$heroku ps:scale web=1
meaning that, right now your app is running on a single web dyno. 

a handy shortcut to open the website as follows: 
$heroku open

View Logs 
$heroku logs --tail  
stop streaming the logs  by control +c

Define a Procfile
Creating a Procfile, a text file without any extension, in the root directory of your application, to tell heroku how to start your app. 

 You must configure your application to listen on the correct port.
web: java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar

Spring Boot makes -D arguments available as properties accessible from a Spring Environment instance. The server.port configuration property is fed to the embedded Tomcat, Jetty or Undertow instance which then uses it when it starts up. The $Port environment variables is assigned to us by the Heroku Paas.  

web is important here. it declares that this process type will be attached to the HTTP routing stack of Heroku and receive web traffic when deployed

Declare app dependencies
Heroku recognizes an app as Java by the existence of a pom file in the root directory. It specifies the dependencies that need to be installed with you application. When an app is deployed, Heroku reads this pom file and install the dependencies.

Heroku by default use Java 8; so Maven or Gradle build need to use the same version(Maven can use the java.version property). If requiring JDK 7, creating a new file adjacent to pom.xml and Procfile, called system.properties, where adding the following:

java.runtime.version=1.7

The most common workflow for Heroku deployments is to git push the code to the production. 
$git push heroku master

Scale the app
A dyno is considered as a lightweight container that runs the command specified in the Procfile
check how many dynos running using the command
$heroku ps

scale number of dyno up
$heroku ps:scale web=1 

Run app locally
$heroku local web 

heroku local examines the Procfile to determine what command to run

Using Database

The HikariCP(ref. to A) database connection pool(ref. to B) is initialized with the configuration application.properties
spring.datasource.url: ${JDBC_DATABASE_URL:}
This sets spring.datasource.url to the value in the JDBC_DATABASE_URL environment variable, set by the database add-on, and establishes a pool of connections to the database.
Listing database provisioned for your app using the following command
$heroku addons 

Listing config vars for your app that will display the URL to the database. 
$heroku config 

Listing a lot more
$heroku pg


Deploy local changes

$git add .
$git commit -m "demo"
$git push heroku master
$heroku open 




A: What is HikariCP?
HikariCP is a “zero-overhead” production-quality connection pool.

B: What is Connection Pool?
Establishing JDBC connections is resource-expensive, especially when the JDBC API is used in a middle-tier server environment, such as when DataDirect Connect for JDBC or DataDirect SequeLink for JDBC is running on a Java-enabled web server. In this type of environment, performance can be improved significantly when connection pooling is used. Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.

C: Using SPRING_DATASOURCE_URL in Spring Boot APP
Heroku buildpacks for Java and Gradle will attempt to create SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD environment variables when a dyno starts up. The values of the variables will be identical to the values in the corresponding JDBC variables.
As long as your application has the proper JDBC driver defined as a dependency, these environment variables should allow your Spring Boot application to connect to the database without any other configuration.
If you need to override the predefined SPRING_DATASOURCE_* environment variables you can set them yourself with the heroku config:set command or in the dashboard. Alternatively, you can add the -Dspring.datasource.url property to your Procfile, which will take precedence over the OS-level environment variables.


D: Where define environment variables in Spring Boot APP?
environment variables(key = value) are defined in the .env text file located at the root directory. It may be configured from CLI, for instance as the following;
$heroku config:set ENERGY = "20 Gev."
In runtime, environment variables are exposed to application and then can be grabbed like the following;
String energy = System.getEnv().get("ENERGY"); 


No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...