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

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

 

Spring Boot启动方法

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

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

 

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

1 @SpringBootApplication
2 public class App 
3 {
4     public static void main( String[] args )
5     {
6     SpringApplication.run(App.class, args);
7     }
8 }

 

 

 

 

外部Tomcat的问题

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

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

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

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

 

 

 

 

使用外部Tomcat运行Spring Boot

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

为artifact设置war packaging:

1 <packaging>war</packaging>


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

1        <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-tomcat</artifactId>
4             <scope>provided</scope>
5         </dependency>

 

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

 1 <dependency>
 2 <groupId>org.springframework.boot</groupId>
 3 <artifactId>spring-boot-starter-web</artifactId>
 4             <exclusions>
 5                 <exclusion>
 6                     <groupId>org.springframework.boot</groupId>
 7                     <artifactId>spring-boot-starter-tomcat</artifactId>
 8                 </exclusion>
 9             </exclusions>
10 </dependency>


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

1 @SpringBootApplication
2 public class App extends SpringBootServletInitializer
3 {
4     public static void main( String[] args )
5     {
6     SpringApplication.run(App.class, args);
7     }
8 }

 

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

1  * 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.
2  * 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.
3  * 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文件中进行设置。

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

 

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

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/youruike-/p/12171284.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!