Deploy (publish) the application created by Spring Boot to Heroku (1) is a continuation.
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
・ Enter the app name and press "Create app" The region is currently only available in the United States or Europe.
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.
When you select "Heroku Postgres", a pop-up like the one below will open. Press "Submit Order Form".
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.
pom.xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
Open the Heroku application page and click Heroku Postgres.
Under Settings, click View Credentials.
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.
Go back to the Heroku application page and set the settings you checked earlier in "Config Vars" on the "Settings" tab as environment variables.
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.
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".
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".
The figure below shows the state where the addition of environment variables is completed.
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
Open Eclipse and right click on the project. Execute "8 maven build" in "Run".
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"
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 /
$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.
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.
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