Memo

メモ > 技術 > フレームワーク: SpringBoot > Vagrantでアプリケーション(JAR)を起動

■Vagrantでアプリケーション(JAR)を起動
■プログラムの準備 「プロパティの上書き(src/main/resources/application-xxx.properties)」を参考に、環境ごとの設定ファイルを用意しておく ここでは、Vagrant用に application-test.properties を作成したものとする ■基本設定
# localectl set-locale LANG=ja_JP.UTF-8 # timedatectl set-timezone Asia/Tokyo # yum install -y https://cdn.azul.com/zulu/bin/zulu-repo-1.0.0-1.noarch.rpm # yum install -y zulu17-jdk # java -version openjdk version "17.0.5" 2022-10-18 LTS OpenJDK Runtime Environment Zulu17.38+21-CA (build 17.0.5+8-LTS) OpenJDK 64-Bit Server VM Zulu17.38+21-CA (build 17.0.5+8-LTS, mixed mode, sharing)
CentOS 7にOpenJDK 17をインストール(Azul Zulu Builds of OpenJDK) - Qiita https://qiita.com/witchcraze/items/014da8ee2c638737e9bc Javaのサポートについてのまとめ2018 - Qiita https://qiita.com/nowokay/items/edb5c5df4dbfc4a99ffb ■プログラムの実行(MySQLなし) 同期フォルダである /var/www に demo-0.0.1-SNAPSHOT.jar を配置
$ nohup java -jar /var/www/demo-0.0.1-SNAPSHOT.jar &
以下でアクセスできることを確認(確認できたら終了しておく) ■プログラムの実行(MySQLあり)
# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash # yum -y install MariaDB-server MariaDB-client # systemctl start mariadb # systemctl enable mariadb # mariadb-secure-installation $ mysql -u root -p mysql> CREATE DATABASE spring DEFAULT CHARACTER SET utf8; mysql> CREATE USER webmaster@localhost IDENTIFIED BY 'abcd1234'; mysql> GRANT ALL PRIVILEGES ON spring.* TO webmaster@localhost; mysql> QUIT $ mysql -u webmaster -p mysql> USE spring mysql> CREATE TABLE ... (必要なテーブルを作成する) mysql> QUIT
■サービスの設定
# vi /etc/systemd/system/spring.service
[Unit] Description = Spring Boot Application [Service] ExecStart = /usr/bin/java -jar /var/www/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=test … 実行したいjarファイルの場所とその環境を指定(jarファイルとjavaは絶対パスで指定) Restart = always Type = simple User = vagrant … 環境に合わせてユーザ名を指定する Group = vagrant … 環境に合わせてユーザ名を指定する SuccessExitStatus = 143 [Install] WantedBy = multi-user.target
UserとGroupに専用のユーザを指定する場合、「useradd xxxxx」のようにしてユーザを作成しておく また、このファイルがjarファイルを読み取りできるようにしておく
# systemctl list-unit-files -t service | grep spring … サービスの状態を確認(Unitがサービスとして認識されたか確認) spring.service disabled # systemctl enable spring … サービスの自動起動を有効化 Created symlink from /etc/systemd/system/multi-user.target.wants/spring.service to /etc/systemd/system/spring.service. # systemctl list-unit-files -t service | grep spring … サービスの自動起動を確認 spring.service enabled # systemctl start spring … サービスを起動 # systemctl status spring … サービスの起動を確認 ● spring.service - Spring Boot Application Loaded: loaded (/etc/systemd/system/spring.service; enabled; vendor preset: disabled) Active: failed (Result: start-limit) since 金 2023-01-06 14:47:44 JST; 1s ago Process: 3212 ExecStart=/var/www/demo-0.0.1-SNAPSHOT.jar (code=exited, status=203/EXEC) Main PID: 3212 (code=exited, status=203/EXEC) 1月 06 14:47:43 localhost.localdomain systemd[1]: spring.service: main process exited, code=exited, status=203/EXEC 1月 06 14:47:43 localhost.localdomain systemd[1]: Unit spring.service entered failed state. 1月 06 14:47:43 localhost.localdomain systemd[1]: spring.service failed. 1月 06 14:47:44 localhost.localdomain systemd[1]: spring.service holdoff time over, scheduling restart. 1月 06 14:47:44 localhost.localdomain systemd[1]: Stopped Spring Boot Application. 1月 06 14:47:44 localhost.localdomain systemd[1]: start request repeated too quickly for spring.service 1月 06 14:47:44 localhost.localdomain systemd[1]: Failed to start Spring Boot Application. 1月 06 14:47:44 localhost.localdomain systemd[1]: Unit spring.service entered failed state. 1月 06 14:47:44 localhost.localdomain systemd[1]: spring.service failed. # systemctl stop spring … サービスを停止させる場合
サービスとしての稼働については、 Dropbox\サーバ\Etcetera.txt 内にある「バックグラウンドで処理を実行する(サービス)」も参照 ■Nginxの導入 ※あらかじめ、SELinuxを無効化しておく
# yum -y install epel-release # yum -y install nginx # vi /etc/nginx/nginx.conf
server { listen 80 default_server; listen [::]:80 default_server; server_name _; #root /usr/share/nginx/html; … 公開ディレクトリをコメントアウト # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://localhost:8080; … ブロックに内容を追加 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }
# systemctl start nginx # systemctl enable nginx
http://192.168.33.10/ http://192.168.33.10/now ■トラブルシューティング 起動時のログやエラー時のログは /var/log/message に出力される 例えば「--spring.profiles.active=test」による環境の指定を行った場合、以下が出力されていた
2023-01-19T12:18:53.234+09:00 INFO 3207 --- [ main] com.example.demo.DemoApplication : The following 1 profile is active: "test"
データベースに接続できないとき、以下が出力されていた
Jan 19 11:45:41 localhost mariadbd: 2023-01-19 11:45:41 3 [Warning] Access denied for user 'root'@'localhost' Jan 19 11:45:42 localhost java: 2023-01-19T11:45:42.454+09:00 ERROR 3011 --- [nio-8888-exec-3] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization. Jan 19 11:45:42 localhost java: java.sql.SQLException: Access denied for user 'root'@'localhost'
データベースにテーブルが作成されていないとき、以下が出力されていた Windows環境ではテーブルの大文字と小文字は区別されないようだが、Linux(Vagrant)環境では区別されるので注意
2023-01-18T17:51:21.182+09:00 ERROR 3560 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [SELECT * FROM tasks]] with root cause java.sql.SQLSyntaxErrorException: Table 'spring.tasks' doesn't exist

Advertisement