前回はSpringBoot+Postgresqlで作成したクイズアプリをMac上で動作させましたが、今回はラズパイ上で動かしてみました。
SpringBootによるクイズアプリの作成は以下の記事に書いています。
クイズアプリをSpringBoot+PostgreSQL+MyBatis+Thymeleafで作る(3)【Controller,フロントエンド作成】
目次
環境
モデル:Raspberry Pi 3 Model B Rev 1.2
OS:Raspbian GNU/Linux 10 (buster)
SpringBootのJarファイルを作成
SpringBootプロジェクトのpom.xmlと同階層にてターミナルを開き、「mvn clean install」を実行する。
BUILD SUCCESSとなればビルド成功し、targetフォルダ内にjarファイル(デフォルト設定ではQuestionSample-0.0.1-SNAPSHOT.jarという名前)が作成される。
データベースのバックアップ
pg_dumpというコマンドでバックアップが可能。
最初はpostgresにログインしてpg_dumpを実行しましたが保存先がどこかわかりませんでした。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ psql -U postgres postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- mrs | mrs | UTF8 | C | C | postgres | postgres | UTF8 | C | C | quizdb | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | testdb | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | (6 rows) postgres=# pg_dump quizdb > backup.sql |
そこで、筆者の環境(Mac)では以下のようにpg_dumpを実施したところ、コマンドを実行したディレクトリにバックアップを取得できました。
1 2 3 |
$ /Library/PostgreSQL/12/bin/pg_dump quizdb > backup.sql -U postgres $ ls -la -rw-r--r-- 1 user staff 4683 1 18 21:46 backup.sql |
ラズパイにJavaとPostgreSQLをインストール
Javaインストール
Javaをインストールします。
1 |
sudo apt install openjdk-11-jdk -y |
インスール後、バージョンを確認します。
1 2 3 4 |
pi@raspberrypi:/ $ java -version openjdk version "11.0.9.1" 2020-11-04 OpenJDK Runtime Environment (build 11.0.9.1+1-post-Raspbian-1deb10u2) OpenJDK Server VM (build 11.0.9.1+1-post-Raspbian-1deb10u2, mixed mode) |
PostgreSQLインストール
PostgreSQLインストール前にupdateとupgradeを実施します。
筆者の環境では実施しないとインストールができませんでした。
1 2 |
sudo apt update sudo apt -y upgrade |
続いて、PostgreSQLを以下のコマンドを実行しインストールします。
1 |
sudo apt install postgresql -y |
インスール後、バージョンを確認します。
1 2 |
pi@raspberrypi:/ $ psql --version psql (PostgreSQL) 11.9 (Raspbian 11.9-0+deb10u1) |
PostgreSQLの設定
postgresql.confの設定
/etc/postgresql/11/main/postgresql.confのlisten_addressesを以下のように'*'に変更します。
1 |
listen_addresses = '*' ←デフォルトではコメントアウトされているので解除して'*'に変更 |
pg_hba.confの設定
/etc/postgresql/11/main/pg_hba.conf の権限を以下のようにtrustに変更します。
※セキュリティを考慮していない設定なので注意
1 2 3 4 5 6 7 8 9 10 11 |
# Database administrative login by Unix domain socket local all postgres trust # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust |
postgresql.conf及びにpg_hba.confの変更を反映させるためにpostgresqlを再起動します。
1 |
service postgresql restart |
iptablesにPostgreSQLのポートを設定
PostgreSQLのポート番号をファイアウォールから除外するために /etc/iptables/rules.v4 に設定を行います。
筆者の環境ではrules.v4が存在しなかったため新規作成しました。
1 |
-A INPUT -p tcp --dport 5432 -j ACCEPT |
設定後、iptablesを再起動します。
1 |
/etc/rc.d/init.d/iptables restart |
ラズパイにデータベースをリストア
バックアップしたbackup.sqlをリストアします。
FileZilla等を用いてラズパイにbackup.sqlを転送します。今回の転送先は/home/piとします。
ラズパイにSSHでアクセスして/home/pi配下で以下のコマンドを実行してリストアします。
1 |
pi@raspberrypi:~ $ psql quizdb < backup.sql -U postgres |
リストアされているか確認します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
pi@raspberrypi:~ $ su - postgres postgres@raspberrypi:~$ psql quizdb psql (11.9 (Raspbian 11.9-0+deb10u1)) "help" でヘルプを表示します。 quizdb=# \l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 -----------+----------+------------------+-------------+-------------------+----------------------- postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | quizdb | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (4 行) quizdb=# \d リレーション一覧 スキーマ | 名前 | 型 | 所有者 ----------+----------+----------+---------- public | my_quiz | テーブル | postgres public | question | テーブル | postgres (2 行) quizdb=# select * from question; id | question | choice1 | choice2 | choice3 | choice4 | answer | description | answered | misstake<br />----+------------------------------------------------------------------+----------------------------------+--------------------------+------------------+--------------------+--------+-----------------------------------------------------------+----------+----------<br />1 | 令和2年の今年の漢字はどれか? | 愛 | 災 | 密 | 離 | 3 | 令和2年の今年の漢字は「密」でした。 | f | t<br />2 | クラウドサービスのAWSを提供する企業はどれか? | MicroSoft | Google | Apple | Amazon | 4 | AWSはAmazon Web Serviceの略で、高いシェアを誇っています。 | f | t<br />3 | 日経平均株価に採用されている企業数は何社か? | 100社 | 225社 | 400社 | 1235社 | 2 | 225社となります。そのため日経225とも呼ばれます。 | f | t<br />4 | DX推進と言われるが、DXとは何の略か | デジタルトランスフォーメーション | デジタルトランザクション | デジタルトレース | デジタルデラックス | 1 | デジタルトランスフォーメーションです。 | f | t<br />5 | 日本の債務残高はGDPの何倍か? | 0.5倍 | 1倍 | 1.5倍 | 2.5倍 | 4 | 2.5倍と世界的に見ても高い水準になります。 | f | t<br />6 | 2020年の日本において総人口に占める65歳以上の人口の割合は何%か? | 21.4% | 25.6% | 28.7% | 30% | 3 | 2020年で28.7%になります。2025年には30%を超える予測です。 | f | t<br />7 | 2020年に新規上場した企業は何社あるか? | 54社 | 76社 | 86社 | 93社 | 4 | 2020年は93社でした。2019年の86社から増加しました。 | f | t<br />8 | 2019年に金融庁が発表した老後資金の不足額はいくらか? | 800万円 | 2000万円 | 2500万円 | 3000万円 | 2 | 2000万円問題と当時話題になりました。 | f | t<br />9 | 大ヒットアニメ"鬼滅の刃"の監督名を選べ | 外崎春雄 | 是枝裕和 | 新海誠 | 庵野秀明 | 1 | 監督は外崎春雄、作者は吾峠呼世晴です。 | f | t<br />10 | 2020年に東証システム障害が発生した日に結婚発表した有名人は誰か? | 石原さとみ | 戸田恵梨香 | 山本美月 | 清野菜名 | 1 | 10月1日でした。 | f | t<br />(10 行) |
ラズパイにリストアされていることが確認できました。
ラズパイでSpringBootアプリを実行
作成したQuestionSample-0.0.1-SNAPSHOT.jarをラズパイの/home/piに転送します。
/home/pi配下で以下のコマンドでSpringBootアプリを起動します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
pi@raspberrypi:~ $ java -jar QuestionSample-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 2021-01-19 20:58:37.743 INFO 16428 --- [ main] c.e.demo.QuestionSampleApplication : Starting QuestionSampleApplication v0.0.1-SNAPSHOT using Java 11.0.9.1 on raspberrypi with PID 16428 (/home/pi/QuestionSample-0.0.1-SNAPSHOT.jar started by pi in /home/pi) 2021-01-19 20:58:37.777 INFO 16428 --- [ main] c.e.demo.QuestionSampleApplication : No active profile set, falling back to default profiles: default 2021-01-19 20:58:52.364 INFO 16428 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8085 (http) 2021-01-19 20:58:52.462 INFO 16428 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-01-19 20:58:52.464 INFO 16428 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41] 2021-01-19 20:58:52.932 INFO 16428 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-01-19 20:58:52.933 INFO 16428 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 14375 ms 2021-01-19 20:58:59.151 INFO 16428 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-01-19 20:59:00.539 INFO 16428 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2021-01-19 20:59:04.454 INFO 16428 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8085 (http) with context path '' 2021-01-19 20:59:05.998 INFO 16428 --- [ main] c.e.demo.QuestionSampleApplication : Started QuestionSampleApplication in 34.172 seconds (JVM running for 40.866) |
ブラウザから<ラズパイのIPアドレス>:8085/questionにアクセスします。
筆者の環境ではラズパイのIPアドレスは192.168.100.80なので192.168.100.80:8085/questionとなります。
下図のようにトップページが表示されれば成功です。
出題の様子
以上がラズパイでSpringBoot+PostgreSQLを使ったクイズアプリの動作方法でした。
流石にMac上で動かした時に比べると動作が重い感じがしましたが、一応動くものなんだなと理解しました。
SpringBootはtomcatが内蔵されているのでWebサーバの設定無しで動かせて簡単だなと思いました。
今回は以上です。