[JAVA] Deploy the application created by Spring Boot to Heroku (public) ②

Deploy (publish) the application created by Spring Boot to Heroku (1) is a continuation.

④ Create an app on Heroku

Create an app on Heroku with the following command.

$ heroku create <App name>

If you run the app without a name, it will be named automatically. If you want the app name to be "myapp", it's "heroku create myapp".

You can also create it on the Heroku site without using commands.

・ Select "Create new app" from "new" at the top right of the site スクリーンショット 2020-10-29 16.46.29.png

・ Enter the app name and press "Create app" The region is currently only available in the United States or Europe.

スクリーンショット 2020-10-29 16.47.01.png

⑤ Add-on Postgres and add data

Heroku doesn't support H2, and if you're using H2 as your DB management system, your application won't work properly. Use Postgres instead of H2. Heroku's official website guarantees that Heroku and Postgres are compatible with each other because Heroku has been operating Postgres for many years and has the know-how cultivated through interaction with many users.

First, open the page of the application created in (4) and search for "Heroku Postgres" in "Add-ons" on the "Resources" tab.

スクリーンショット 2020-10-30 12.51.04.png

When you select "Heroku Postgres", a pop-up like the one below will open. Press "Submit Order Form". スクリーンショット 2020-10-30 13.05.52.png

Since you will operate the database by entering Postgres commands in the terminal, install postgresSQL on the OS.

$ brew install postgresql

Edit bash.profile and pass it through.

bash.profile


export PATH=/usr/local/Cellar/postgresql@12/12.4_1/bin:$PATH

Enter the following command to get the DB connection information.

$ heroku pg:credentials:url --app [APP-NAME]

Connection information for default credential.
Connection info string:
   "dbname=hogehogehoge host=hogehoge123.amazonaws.com port=5432 user=mrhoge password=hogehoge1964 sslmode=require"
Connection URL:
   postgres://USER-NAME:PASS@HOST:5432/DBNAME

Then connect to the database.

$ heroku pg:psql -a [APP-NAME]
--> Connecting to postgresql-XXXXXX-XXXXXX
psql (10.6)
SSL connection(protocol: TLSv1.2, encryption method: ECDHE-RSA-AES256-GCM-SHA384, bit length:256, compression:off)
"help"Get help with.
[APP-NAME]::DATABASE=>

Create a table for your application in the connected database.

Example:

[APP-NAME]::DATABASE=> CREATE TABLE IF NOT EXISTS food (
  foodID INT PRIMARY KEY,
  name VARCHAR(100),
  price VARCHAR(500),
  description VARCHAR(3000)
);

CREATE TABLE IF NOT EXISTS userData (
	userId VARCHAR(50) PRIMARY KEY,
	mailAddress VARCHAR(100),
	password VARCHAR(100),
	role VARCHAR(50)
);

By the way, the table name cannot be "user" due to the restrictions of postgresql. Let's give it a different name.

We will add data to the created table. Example:

[APP-NAME]::DATABASE=> INSERT INTO food(foodID, name, price, description) VALUES
('1', 'Roll cake', '1000 yen', 'Roll cake専門店で、手作りしている為、1日80本限定販売です。'),
('2', 'Strawberry cake', '900 yen', 'Strawberry cake専門店で、手作りしている為、1日80本限定販売です。'),
('3', 'Chocolate cake 3', '1000 yen', 'Since it is handmade at a chocolate cake specialty store, it is limited to 80 pieces a day.'),
('4', 'Cheesecake 4', '800 yen', 'Since it is handmade at a cheesecake specialty store, it is limited to 80 bottles a day.'),
('5', 'Matcha cake 5', '1200 yen', 'Since it is handmade at a matcha cake specialty store, it is limited to 80 bottles a day.');

When adding multiple records, it is recommended to write the INSERT SQL statement as above! When I executed it with the SQL statement below, I couldn't insert ... Example:

INSERT INTO food(foodID, name, price, description) VALUES
('1', 'Roll cake', '1000 yen', 'Roll cake専門店で、手作りしている為、1日80本限定販売です。');
INSERT INTO food(foodID, name, price, description) VALUES
('2', 'Strawberry cake', '900 yen', 'Strawberry cake専門店で、手作りしている為、1日80本限定販売です。');
INSERT INTO food(foodID, name, price, description) VALUES
('3', 'Chocolate cake 3', '1000 yen', 'Since it is handmade at a chocolate cake specialty store, it is limited to 80 pieces a day.');
INSERT INTO food(foodID, name, price, description) VALUES
('4', 'Cheesecake 4', '800 yen', 'Since it is handmade at a cheesecake specialty store, it is limited to 80 bottles a day.');
INSERT INTO food(foodID, name, price, description) VALUES
('5', 'Matcha cake 5', '1200 yen', 'Since it is handmade at a matcha cake specialty store, it is limited to 80 bottles a day.');

If the data is added successfully, the number of rows of the added record will be displayed. Example: Add 5 rows of records

INSERT 0 5

Use the command below to disconnect from the database.

[APP-NAME]::DATABASE=> \q 

reference About installing postgresSQL: https://qiita.com/yorokobi_kannsya/items/f77d074e382a88dae971 Getting connection information and connecting to the database: https://sysrigar.com/2019/01/20/heroku-postgres%E3%82%92%E3%83%AD%E3%83%BC%E3%82 % AB% E3% 83% AB% E7% 92% B0% E5% A2% 83% E3% 81% 8B% E3% 82% 89% E6% 89% 8B% E5% 8B% 95% E3% 81% A7 % E6% 93% 8D% E4% BD% 9C% E3% 81% 99% E3% 82% 8B% E6% 96% B9% E6% B3% 95% E3% 81% AB% E3% 81% A4 / What to do if "heroku pg: psql -a [appname]" doesn't work: https://qiita.com/Puerto/items/5b886d0ed202761c4cc1

⑥ Add "postgresql" to pom.xml

Add "postgresql" to pom.xml.

pom.xml


<dependency>
	<groupId>org.postgresql</groupId>
	<artifactId>postgresql</artifactId>
	<scope>runtime</scope>
</dependency>

⑦ Set environment variables related to Postgres

Open the Heroku application page and click Heroku Postgres. スクリーンショット 2020-10-30 14.47.32.png

Under Settings, click View Credentials. スクリーンショット 2020-10-30 14.48.25.png

Heroku's postgresql settings will be displayed in a list, so copy and paste them in a memo. In the image, it is "AAA" and "BBB", but since it is not information that should be disclosed, the text is pasted later.

スクリーンショット 2020-10-30 14.57.24.png

Go back to the Heroku application page and set the settings you checked earlier in "Config Vars" on the "Settings" tab as environment variables. スクリーンショット 2020-10-30 15.03.48.png

You can add an environment variable by entering the variable name in "KEY" and the value in "VALUE" and clicking "Add". Variable names are often different from those shown in "View Credentials".

Add the values of "Host", "Database", "User" and "Password" from "View Credentials" to the environment variables. スクリーンショット 2020-10-30 15.15.19.png

Then add the environment variables to your IDE. I'm using Eclipse, so I'll show you how to do it in Eclipse.

Right-click on the project and open "Run", "Configure Run".

スクリーンショット 2020-10-30 15.21.11.png

In Maven Build, open the one with the same name as the project you are working on. (Example: Project name "Snitch3") Enter the same name as the one set in "Config Vars" earlier, enter the value, and press "OK". スクリーンショット 2020-10-30 15.27.17.png

The figure below shows the state where the addition of environment variables is completed. スクリーンショット 2020-10-30 15.24.24.png

Be sure to press Apply last! I forgot to apply it and was worried about the phenomenon that it could not be deployed for a day. If you're worried that it's applied successfully, you can reopen the Environment tab.

Finally, edit "application.properties". Since the contents of the file will change significantly, save the contents of the file that was used when running in the local environment with a different name.

Please paste the following contents as they are. Looking at the contents described, the variable names set in "View Credentials" and Eclipse are described. The value set earlier is applied here, and the database is exchanged normally. This allows you to keep information such as passwords private while allowing it to function.

application.properties


spring.datasource.url=jdbc:postgresql://${host}/${database}?sslmode=require
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASS}
logging.level.jdbc=OFF
logging.level.jdbc.sqltiming=DEBUG

⑧ Build maven and create jar file

Open Eclipse and right click on the project. Execute "8 maven build" in "Run".

スクリーンショット 2020-10-31 12.20.52.png

If it works properly, "BUILD SUCCESS" will be displayed on the console.

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ Snitch ---
[INFO] Building jar: /Applications/Eclipse_2020-06.app/Contents/workspace/Snitch3/target/Snitch-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.3.RELEASE:repackage (repackage) @ Snitch ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- maven-dependency-plugin:3.1.2:copy (default) @ Snitch ---
[INFO] Configured Artifact: com.heroku:webapp-runner:9.0.30.0:jar
[INFO] com.heroku:webapp-runner:9.0.30.0:jar already exists in /Applications/Eclipse_2020-06.app/Contents/workspace/Snitch3/target/dependency
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  19.166 s
[INFO] Finished at: 2020-10-31T12:04:02+09:00
[INFO] ------------------------------------------------------------------------

If you check the "target" folder directly under the root directory, the "arbitrary character string -0.0.1-SNAPSHOT.jar" explained in the previous article is created.

Example: App name "Snitch"

スクリーンショット 2020-10-31 12.28.26.png

⑨ Deploy to Heroku

Deploy "Arbitrary string -0.0.1-SNAPSHOT.jar" under "target".

Set the root directory to the current directory in the terminal and create a new git repository with "git init".

$ cd /Applications/Eclipse_2020-06.app/Contents/workspace/Snitch3
$ git init
Reinitialized existing Git repository in /Applications/Ecl ...

Specify the application and associate it with the local repository. Then deploy with "heroku deploy: jar target / -0.0.1-SNAPSHOT.jar --app app name". Example: App name "Snitch1"

$heroku git:remote -a snitch1
$heroku deploy:jar target/Snitch-0.0.1-SNAPSHOT.jar --app snitch1

It will take some time, but if successful, you will see the following on the console.

Uploading Snitch-0.0.1-SNAPSHOT.jar
-----> Packaging application...
       - app: snitch1
       - including: target/Snitch-0.0.1-SNAPSHOT.jar
-----> Creating build...
       - file: slug.tgz
       - size: 47MB
-----> Uploading build...
       - success
-----> Deploying...
remote: 
remote: -----> heroku-deploy app detected
remote: -----> Installing JDK 14... done
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 114.1M
remote: -----> Launching...
remote:        Released v15
remote:        https://snitch1.herokuapp.com/ deployed to Heroku
remote: 
-----> Done

Try to open the app with the command "heroku open -a app name".

$heroku open -a snitch1

The application was displayed successfully.

スクリーンショット 2020-10-31 12.57.40.png

If you want to verify whether each function works normally from here, you can check the log with "View logs", so if there is something wrong with the operation, please check here.

スクリーンショット 2020-10-31 13.00.35.png

that's all! It has become quite a long sentence, but I hope it will be helpful.

reference: https://www.bedroomcomputing.com/2019/11/2019-1101-heroku-springboot-postgres/#%E6%B3%A8%E6%84%8F https://raishin.xyz/springboot-heroku-deploy/ https://codezine.jp/article/detail/8187?p=3 https://poppingcarp.com/heroku_postgres/ https://sun-blog.site/spring%E3%81%A7%E7%92%B0%E5%A2%83%E5%A4%89%E6%95%B0%E6%AF%8E%E3%81%AB%E7%95%B0%E3%81%AA%E3%82%8Bdb%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%E3%80%82/

Recommended Posts

Deploy the application created by Spring Boot to Heroku (public) ②
Deploy the application created by Spring Boot to Heroku (public) ①
Deploy the WEB application by Spring Boot to Tomcat server as WAR
Deploy Spring Boot applications to Heroku without using the Heroku CLI
[Java] Deploy the Spring Boot application to Azure App Service
Deploy the Spring Boot project to Tomcat on XAMPP
Introduction to Java development environment & Spring Boot application created with VS Code
The story of raising Spring Boot 1.5 series to 2.1 series
Deploy a Spring Boot application on Elastic Beanstalk
◆ Get API created by Spring Boot from React
[Spring Boot] How to refer to the property file
How to set environment variables in the properties file of Spring boot application
Steps to deploy to Heroku
Spring Boot 2.3 Application Availability
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
How to boot by environment with Spring Boot of Maven
A story that stumbled when deploying a web application created with Spring Boot to EC2
The story of raising Spring Boot from 1.5 series to 2.1 series part2
Try Spring Boot from 0 to 100.
About the function of Spring Boot due to different versions
Deploy Vapor Project to Heroku
How to deploy on heroku
[Spring Boot] Web application creation
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Deploy your application to WildFly
Introduction to Spring Boot Part 1
Deploy your application to EDAS using the Cloud Toolkit Maven plugin
Minimal configuration to run Spring Boot application on Azure Web Apps
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
I want to control the default error message of Spring Boot
I was able to deploy the Docker + laravel + MySQL app to Heroku!
Spring Boot application development in Eclipse
Spring Boot application code review points
Hot deploy with Spring Boot development
How to set Spring Boot + PostgreSQL
Inquiry application creation with Spring Boot
Implement Spring Boot application in Gradle
How to use ModelMapper (Spring boot)
Upgrade spring boot from 1.5 series to 2.0 series
Deploy Rails on Docker to heroku
Deploy SpringBoot application to AWS EC2
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
[Spring Boot] I investigated how to implement post-processing of the received request.
Examine the contents of the WAR file generated by the project created by Spring Initializr
Procedure to make the value of the property file visible in Spring Boot
Java beginner tried to make a simple web application using Spring Boot
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
I used Docker to solidify the template to be developed with spring boot.
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
[For internal use] For those assigned to the Spring Boot project (under construction)
Deploy a Node.js application to an ECS instance using the Cloud Toolkit
Processing at application startup with Spring Boot
[Introduction to Spring Boot] Form validation check
How to publish an application on Heroku
Web application scheduled to be created (editing)
Story when moving from Spring Boot 1.5 to 2.1