编程序

SpringBoot 应用部署于外置 Tomcat 容器

微信扫一扫,分享到朋友圈

SpringBoot 应用部署于外置 Tomcat 容器
收藏 0 58

1. 概述

SpringBoot 平时我们用的爽歪歪,爽到它自己连 Tomcat 都自集成了,我们可以直接编写 SBT 启动类,然后一键开启内置的 Tomcat 容器服务,确实是很好上手。但考虑到实际的情形中,我们的 Tomcat 服务器一般是另外部署好了的,有专门的维护方式。此时我们需要剥离掉 SBT 应用内置的 Tomcat 服务器,进而将应用发布并部署到外置的 Tomcat 容器之中,本文就实践一下这个。


2. 修改打包方式

修改项目的 pom.xml 配置,我们修改其打包方式为 war 方式,如:

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

3. 移除 SBT 自带的嵌入式 Tomcat

修改 pom.xml ,从 maven 的 pom 中移除 springboot 自带的的嵌入式 tomcat 插件

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

4. 添加 servlet-api 依赖

修改 pom.xml ,在 maven 的 pom 中添加 servlet-api 的依赖

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>3.1.0</version>
	<scope>provided</scope>
</dependency>

5. 修改启动类,并重写初始化方法

在 SpringBoot 中我们平常用 main 方法启动的方式,都有一个 SpringBootApplication 的启动类,类似代码如下:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

而我们现在需要类似于 web.xml 的配置方式来启动 spring 应用,为此,我们在 Application 类的同级添加一个 SpringBootStartApplication 类,其代码如下:

// 修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法
public class SpringBootStartApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里一定要指向原先用 main 方法执行的 Application 启动类
        return builder.sources(Application.class);
    }
}

6. 部署到外部的 Tomcat 容器并验证

  • 在项目根目录下(即包含pom.xml的目录)记性 maven 打包操作:
mvn clean package

等待打包完成,出现 [INFO] BUILD SUCCESS 即为打包成功

  • 然后我们把target目录下生成的war包放到 tomcat 的webapps目录下,启动 tomcat,即可自动解压部署。

最后在浏览器中验证:

http://YOUR_IP:[端口号]/[打包项目名]

浏览器验证

  • 也可以直接将项目命名为 ROOT,这样访问根目录即可访问 tomcat 中的 SpringBoot 应用
http://YOUR_IP:[端口号]

直接将项目命令为 ROOT

浏览器验证

我还没有学会写个人说明!

WordPress定时发布失败的几个解决办法

上一篇

再次解读javabean/ejb,以及各种POJO/DAO/DTO/PO等

下一篇

你也可能喜欢

58 条评论

  1. sildenafil vs tadalafil – tadalafil buy generic tadalafil online

  2. fincar vs propecia – http://finasteridepls.com/ propecia 1mg

  3. finasteride before and after – http://propechl.com/ what is propecia used for

  4. tadalafil 20 – http://xtadalafilp.com/ tadalafil 5 mg

  5. my favorite writer essay – buying term papers order research papers

  6. Wawtom – lasix 40mg Emvusv cslejy

  7. buy tadacip 10 – tadacip online can i buy tadacip without rx

  8. buy celebrex canada – celebrex 200 mg costs celebrex uk

  9. positive feedback loop heart rate community action kearney ne , positive feedback control. community aid camp hill pa lyrica anticonvulsant order lyrica , friends xmas shirt community policing synonym positive words psychology , community acquired pneumonia journal community definition psychology? community bridges hr department community action partnership branding, community action agency jackson mi community action agency lewiston idaho.
    community bridges payson , local community colleges near me friends key holder social yoga.

  10. office software free download for pc drawing program website to buy Autocad software radeon software adrenalin 2020 edition. autocad architecture 2020 software download 2020 software bug engeeeneringu#$sssaunnplus , silhouette 2020 software free download. microsoft office software free download, graphic design software free 2020 govt software jobs 2020 microsoft office like software for mac. quickbooks software 2020 tint tek 2020 software free download, 2020 interior design software pvt ltd.

  11. stromectol online pharmacy – ivermectin syrup stromectol south africa

  12. top web source for cialis https://cileve.com/ cialis without persriction

  13. yasmin pills medication – buy nexium tablets online how much is topamax in canada

  14. cialis 20mg no prescription http://asciled.com/ – cialis with dapoxetine viagra/cialis

  15. cost of 100mg sildenafil – is viagra professional better viagra 500mg price

  16. buying prescription drugs canada http://uspharmus.com/ Lariam

  17. viagra advertisement https://gensitecil.com/ viagra casero

  18. female viagra price in india – alternativas a viagra 2 sildenafil

  19. lyrica 75 mg price – buy lyrica cheap online lyrica 300 mg

  20. nexium 7.25 coupon – generic brand yasmin topamax weight loss depression

  21. cialisВ® https://cileve.com/ viagra no prescription cialis

  22. levitra comparisons to cialis and viagra https://uslevitraanna.com/ buy levitra singapore

  23. buy tadalafil cheap http://boxtadafil.com/ – tadalafil buy online super tadalafil with dapoxetine

  24. stromectol pill price – stromectol price uk ivermectin 5

  25. cialis overnight delivery http://asciled.com/ – cialis paypal pay with cialis sell

  26. Glucophage SR http://canadianeve21.com/ prescription drugs online without

  27. buy stromectol – ivermectin 1mg ivermectin price comparison

  28. cost of generic zofran – zofran gel ondansetron tablets cost

  29. online pharmacy http://xlnpharmacy.com/ canada pharmacy world

  30. generic cialis site accept mastercard https://rcialisgl.com/ – viagra vs levitra vs cialis reviews buy cialis in australia

  31. can my pcp prescribe viagra https://llviagra.com/ viagra by post

  32. nitroglycerin and viagra https://loxviagra.com/ viagra insurance

  33. buy ivermectin usa – ivermectin pills human stromectol uk

  34. name brand cialis http://cialisee.com/ – all cialis drug original prices buy cialis with american express

  35. buying drugs canada http://pharmacylo.com/ canada pharmacy no prescription

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片