Docker---(5)Docker 部署SpringBoot web項目__web

來源:互聯網
上載者:User

SpringBoot項目發布到伺服器,是一件很簡單的事情,但是發布到伺服器上docker啟動的tomcat容器中,有一些坑,需要注意。

下面完整的介紹一下:將SpringBoot web項目發布到docker啟動的tomcat容器中。

Spring項目發布,請參考:docker---(3)docker 發布spring web項目

SpringBoot入門,請參考:SpringBoot--從0開始學SpringBoot

Docker入門,請參考: Docker---從零開始學Docker

1.pom.xml

將打包方式改為war包

[java]  view plain  copy <packaging>war</packaging>   移除springBoot內建的tomcat,添加servlet-api

[java]  view plain  copy <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-web</artifactId>       <!--移除內建的tomcat-->       <exclusions>           <exclusion>               <groupId>org.springframework.boot</groupId>               <artifactId>spring-boot-starter-tomcat</artifactId>           </exclusion>       </exclusions>   </dependency>   <!--添加servlet-api-->   <dependency>       <groupId>org.apache.tomcat</groupId>       <artifactId>tomcat-servlet-api</artifactId>       <version>8.0.36</version>       <scope>provided</scope>   </dependency>   在build中添加項目訪問路徑 [java]  view plain  copy <finalName>tweb</finalName>  

(我第一次發布時,看了不下30篇文章,沒有提到這一點,發布了20多次都沒成功)

[java]  view plain  copy <build>       <!--打包後的項目訪問名稱-->       <finalName>tweb</finalName>       <plugins>           <plugin>               <groupId>org.springframework.boot</groupId>               <artifactId>spring-boot-maven-plugin</artifactId>           </plugin>       </plugins>   </build>   2.自訂一個Servletinitializer類,繼承SpringBootServletInitializer,用來啟動上下文

(和啟動類目錄同級)

[java]  view plain  copy package com.jd;      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;   import org.springframework.boot.builder.SpringApplicationBuilder;   import org.springframework.boot.web.support.SpringBootServletInitializer;   import org.springframework.context.annotation.ComponentScan;   import org.springframework.context.annotation.Configuration;      /**   * Date 2018/1/30   * Description:啟動spring的上下文   */     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.