在本文中,我将如何在外部Tomcat上运行Spring Boot应用程序。对我来说,这是一个现实的场景,我必须解决这个问题,因此也请教了一下优锐课老师,得到了很多帮助。也希望当你遇到类似问题时,能为你提供一些有用的信息。

让我们看看从头开始一个项目时可能会遇到的一些常见问题。

Spring Boot启动方法

使用Spring Boot的主要优点之一是可以使用内置的嵌入式Tomcat轻松设置Web应用程序。默认情况下,自动配置器使用Tomcat设置你的项目。你只需点击运行按钮,你的Web服务器就会启动你的应用程序。在application.yaml或属性文件中,你可以像下面这样轻松地配置此Tomcat服务器:

 server:
port: 9091 //set the port number of our running application
servlet:
context-path: /springapplication /set the context path of our application

要运行我们的应用程序,我们只需要一个主方法:

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

外部Tomcat的问题

可以期望应用程序在外部Tomcat服务器上运行。通常,它的发生是由于运营基础架构。此时,出现了一些问题。你的Spring Boot应用程序不会仅仅通过将其部署到网络服务器来启动。有以下几个原因:

  • 默认情况下,Spring Boot创建一个.jar文件作为输出。Tomcat需要一个.war文件。
  • 在Tomcat环境中,你不想使用嵌入式服务器。
  • Tomcat服务器不会使用你的主要方法。因此,你需要以普通的Spring应用程序启动你的应用程序。

Spring Boot是一项尖端技术,主要支持在容器化环境中运行。如果你想以标准方式操作应用程序,该怎么办?

首先,你可以返回通常不想要的本机Spring。其次,你可以进行一些修改,以使你的应用程序准备在老式Tomcat服务器上运行。 所以,我会告诉你如何。

使用外部Tomcat运行Spring Boot

首先,你需要在pom.xml中进行一些修改:

为artifact设置war packaging:

 <packaging>war</packaging>

设置Tomcat服务器依赖项以提供:

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

如果你的某些Spring Boot库默认包含它,请排除它:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

最后,这就是窍门:修改应用程序的默认类:

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

SpringBootServerInitializer类执行以下操作(从原始类头中获取):

  * An opinionated {@link WebApplicationInitializer} to run a {@link SpringApplication} from a traditional WAR deployment. Binds {@link Servlet}, {@link Filter} and {@link ServletContextInitializer} beans from the application context to the server.
* To configure the application either override the {@link #configure(SpringApplicationBuilder)} method (calling {@link SpringApplicationBuilder#sources(Class...)}) or make the initializer itself a {@code @Configuration}. If you are using {@link SpringBootServletInitializer} in combination with other {@link WebApplicationInitializer WebApplicationInitializers} you might also want to add an {@code @Ordered} annotation to configure a specific startup order.
* Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it. If you prefer to run an embedded web server then you won't need this at all.

完成这些修改后,你将能够从传统的war部署中运行你的应用程序。

Tomcat上的Spring Boot App的外部配置

通常可以从IT部门可以轻松处理它的外部来源读取配置文件。

为此,你需要在主类中覆盖上述类中的configure方法。

在下面的示例中,我带来了一个实时示例。在这里,我需要从环境变量中读取配置路径。他们在Tomcat的服务器setenv.sh文件中进行设置。

 @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
String configLocation = System.getProperty("global.appconf.dir"); //get the default config directory location
String configPath = configLocation + File.separator + "springApplication" + File.separator + "application.yml"; //set the configpath of this application instance exclusively
log.info("Configpath: " + configPath);
log.info("Starting to run Spring boot app...");
if(configLocation != null && !configLocation.isEmpty()) {
Properties props = new Properties();
props.setProperty("spring.config.location", configPath); //set the config file to use
application.application().setDefaultProperties(props);
}else{
log.info("No global.appconf.dir property found, starting with default on classpath");
}
return application.sources(SpringApplication.class);
}

请注意,在外部Tomcat上运行时,配置文件中服务器节点下的设置不会生效。这些属性仅影响嵌入式Tomcat。

带有外部Tomcat的Spring Boot的更多相关文章

  1. SpringBoot之外部Tomcat运行Spring Boot项目

    内置tomcat8.5.28 外置的要高于此版本才OK spring boot1.5是访问不了jsp页面的 以后要以2.0版本为主流的

  2. Tomcat部署spring boot项目

    Tomcat部署spring boot项目   需要在启动类做修改

  3. 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】

    1.前言 spring boot 转成war包 后用tomcat发布的具体操作在我另一篇随笔有详细记载,不论是window系统还是Linux系统,tomcat的发布配置都是一样的,所以这里不具体讲这个 ...

  4. 【Tomcat】使用Tomcat部署Spring Boot项目生成的jar包

    介绍 简单来说,Tomcat是一个免费的,用于Java Web应用以及其它Web应用的一个Web服务器.(简单地概括一下,可能有误) 下载与安装 本文章目标是把Spring Boot Web项目生成的 ...

  5. docker在配置tomcat和spring boot远程调试

    服务器部署项目后又时可能与本地开发效果不一致,怎么实现远程调试配置? docker中怎么进行配置? docker中tomcat实现远程调试配置 1. 配置docker-compose.yml CATA ...

  6. Centos 7 redis、tomcat、Spring Boot添加开机自启服务

    一.redis添加开机自启 1.添加服务配置文件 [root@test system]# vim /etc/systemd/system/redis-server.service 2.服务配置文件内容 ...

  7. tomcat 启动Spring boot 项目

    SpringBoot 项目如何在tomcat容器中运行 1.相关连接: https://blog.csdn.net/u010598360/article/details/78789197/ 2.修改打 ...

  8. Spring Boot 实用MyBatis做数据库操作

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  9. spring boot 打包war后 部署到外部 tomcat 的具体正确操作【包括修改端口 与 去除请求路径的工程名】

    1.前言 工程做好了,总不能放在idea运行吧?不然怎么把项目放到云服务器呢?[这一篇随笔不讲解发布的云服务器的操作,在其他随笔有详细记载.] 解决的方案是把springboot 工程 打包成war文 ...

随机推荐

  1. 新浪微博API错误代码说明对照表 http://open.weibo.com/wiki/Error_code

    http://open.weibo.com/wiki/Error_code 这篇文章资料是从新浪微博开发平台分享过来,一方面是博主自己开发过程遇到问题对错误代码的快捷查询,不用每次都得到官方找:另一方 ...

  2. 2018-9-1-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199

    title author date CreateTime categories win10 uwp 轻量级 MVVM 框架入门 2.1.5.3199 lindexi 2018-09-01 16:24: ...

  3. Python 3.7.0 For Mac版软件安装教程附下载地址

    https://www.jianshu.com/p/f02d6f01eba7

  4. HDU 1114 完全背包问题

    题意:有一个存钱罐,空罐时的重量是e,满罐时的重量是f,现在有n种硬币,每一种有无限个,现在给出每一种硬币的价值p和重量w,问存钱罐中最少钱,输出最小钱,否则输出... 思路:变形的完全背包问题,只是 ...

  5. 条件随机场(CRF) - 2 - 定义和形式

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xueyingxue001/article/details/51498968声明: 1,本篇为个人对& ...

  6. const(每个对象中的常量), static const(类的编译时常量)

    1 每个对象中的常量 --- const数据成员 const限定,意味着“在该对象生命周期内,它是一个常量”. 关键字const 使被限定的量为常量 在该类的每个对象中,编译器都为其const数据成员 ...

  7. 安装scipy失败提示lapack not found

    从python库网站下载numpy+mkl合集包通过pip安装在下载scipy安装包通过pip安装即可

  8. C# 7.2 通过 in 和 readonly struct 减少方法值复制提高性能

    在 C# 7.2 提供了一系列的方法用于方法参数传输的时候减少对结构体的复制从而可以高效使用内存同时提高性能 在开始阅读之前,希望读者对 C# 的值类型.引用类型有比较深刻的认知. 在 C# 中,如果 ...

  9. H3C根桥的选举

  10. 2018-8-10-win10-uwp-win2d-离屏渲染

    title author date CreateTime categories win10 uwp win2d 离屏渲染 lindexi 2018-08-10 19:17:19 +0800 2018- ...