3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Bootで作ったRESTfulなAPIをTomcat8にデプロイしてみる

Last updated at Posted at 2019-10-27

久々の投稿。今回はSpring Boot製アプリをTomcat8にデプロイ、起動してみたお話です
所々間違ったことを言っているかもしれないので、コメントで教えてくれると助かります
あと、今回の記事で扱うTomcatのバージョンは8です。9ではないのでご注意ください
また、Apache2との連携(連動?)はせず、Tomcat単体での運用になります

余談だけど、はてなブログに書いたサーバー関連のメモ記事も、こっちに書いた方がいいのかなぁ

動作環境

  • Linux mint 19.1(Tomcat8実行マシンOS)
  • JDK8
  • Spring Boot 2.2.0.RELEASE
  • Maven

パッケージのインストール

sudo apt update && sudo apt install -y tomcat8
updateする必要はないかもしれないけど一応。パッケージはこれだけで大丈夫なはず
(別途Apacheと連携させたいとかそういう場合はApache2もインストールが必要かも)

Tomcatの設定

設定ファイルは/var/lib/tomcat8/conf以下にあります
ポートの設定とかデプロイディレクトリとかの設定はserver.xmlに記述しますが、今回の記事ではコンテキストパスの設定も同ファイルで行ってしまいます
(本来ならcontext.xmlでやるのだろうけど...)

ポートの設定

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
               ↓
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

ほかの記事とかサイトを見ていると別のConnectorの方を書き換えているけど、僕の環境では上記の様に変更し8081にアクセスするとちゃんと適用できていたので大丈夫だと思いますが、間違っていたら教えてください。

コンテキストパスの設定

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
       <!--  Sampleアプリのパス設定  -->
       <Context path="/gn5rapl/sample/api/" docBase="sample-api.war" />
</Host>

1つわからないんですが、なんでコンテキスパスを設定した後にリロードすると、gn5rapl#sample#apiっていうディレクトリが作られるのに、warのfinalNameをgn5rapl#sample#apiにしてパッケージング、docBaseにgn5r#sample#api(.war)を直接指定しても動かないんですかね?心優しい人教えてください...
そういうのもあって、今回の例ではsample-api.war
という名前でパッケージングしています。後ほどpomの記述も紹介します

コンテキストパスの設定を初期設定のままtomcat8が起動してる状態で、gn5rapl#sample#api.warでwebappディレクトリへデプロイ、自動展開したのを確認後、少し待った後にIPアドレス:ポート/gn5rapl/sample/api/swagger-ui.htmlをブラウザで確認してみると動いているのを確認しました。下手にコンテキストを弄らないほうが良さそう...1

Spring Bootアプリの設定

順番が逆かもしれませんが、Spring Bootアプリの設定になります
2019年10月28日(日)現在では、Spring Bootアプリの新規作成の段階でServletInitializer.classが生成されており、pom.xmlにもspring-boot-starter-tomcatが追加されています
無い場合は以下を参考にクラスを作成、またはApplicationクラスに継承させます

ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SampleApiApplication.class);//適宜、Applicationクラス名を変更してください
	}
}
pom.xml
  <groupId>gn5r.gn5rapl</groupId>
  <artifactId>sample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <!-- packagingをwarにする -->
  <packaging>war</packaging>
  <name>Sample-api</name>
  <description>Qiita投稿用サンプルAPIプロジェクト</description>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>

<build>
  <finalName>sample-api</finalName> <!-- ここで設定した名前.warになる -->
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
</build>

Spring Bootアプリをパッケージング&&デプロイ

EclipseやStSなら実行→Maven cleanとMavenビルドのゴールをpackageにして実行
コマンドならmvn clean packageを実行します
するとtargetディレクトリにsample-api.warが生成されているのでscpなどでサーバマシンへファイルを送信します
(/var/libはroot権限じゃないとファイル転送できないと思うので、$HOME/Documentsに配置しました)

sudo cp $HOME/Documents/sample-api.war /var/lib/tomcat8/webapp
mvでもいいかもしれないけど、コピーにしてます

Tomcatの設定ファイルを弄っているので再起動する必要があるので以下を実行
sudo systemctl restart tomcat8

実行例

swagger.png

Sample-apiにSwaggerを導入させているので、動作確認も兼ねて表示テスト
IPアドレスをHostsファイルで弄っているのでgn5r.tomcat.jpは各自のサーバーマシンのIPアドレスなどに変更して下さい

後ほど今回紹介したSpring BootアプリをGitHub(リンクになっています)に置く予定なので、そちらも参考程度にしてみてください2

  1. 2020/01/20(月) 素のtomcat8で動かしたらできたので訂正しました

  2. GitHubにリポジトリを作成、コミットしました

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?