LoginSignup
24
41

More than 5 years have passed since last update.

SpringBoot アプリをサービスとして動かす方法

Posted at

概要

  1. Fully Executable Jarの作成
  2. confファイルの作成
  3. サービス登録

1. Fully Executable Jarの作成

Fully Executable Jarという形式のJarにするとJarの先頭にスクリプトをつけて出力してくれる。
このスクリプトがそのままサービス起動用のスクリプトとして使用できる。

作成の仕方は、pom.xmlの<build>タグの中身を次のようにする

pom.xml
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

<executable>true</executable>がキモ

2. confファイルの作成

実行するJarファイルと同名で拡張子だけ.confに変えたファイルを作成する。
内容は環境変数の定義とjavaに渡すオプションとjarに渡す実行時引数。

環境変数は普通にexport

export LANG="ja_JP.UTF8"

javaのオプションはJAVA_OPTS

JAVA_OPTS="-Xms1024M -Xmx1024M"

実行時引数はRUN_ARGS

RUN_ARGS="--spring.profiles.active=production"

サービス登録

CentOS7などのsystemdでは/etc/systemd/system/にserviceファイルを作成すればOK

/etc/systemd/system/hoge.service
[Unit]
Description = <サービスの説明>

[Service]
ExecStart = /home/application/hoge.jar
Restart = always
Type = simple
User = application
Group = application
SuccessExitStatus = 143

[Install]
WantedBy = multi-user.target

これで

$ sudo systemctl start hoge

とかができる

24
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
41