spring boot 部署到线上tomcat

第一步:
将这个 Spring Boot 项目的打包方式设置为 war

    <version>0.0.1</version>
    <packaging>war</packaging>

第二步
去掉springboot中内嵌的tomcat模块,修改pom文件

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

第三步

写一个SpringBootServletInitializer 子类,并覆盖它的 configure 方法,可以把应用的主类改为继承 SpringBootServletInitializer,或另外写,我这里用继承的方法


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import java.util.Locale;

@SpringBootApplication
public class RenProApplication extends SpringBootServletInitializer {


    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        return application.sources(RenProApplication.class);
    }

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

    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver slr = new CookieLocaleResolver();
        slr.setDefaultLocale(Locale.CHINA);
        slr.setCookieMaxAge(3600);//设置cookie有效期.
        return slr;
    }
}

第四步
打包

mvn clean package -Dmaven.test.skip=true

上传部署

scp -P 434 target/renpro-0.0.1.war root@112 .12.31.222:/data

补充:
可以通过jar -xf renpro-0.0.1.war 解压,或者直接放到tomcat Host规定的目录中自动解压

    原文作者:asoren
    原文地址: https://segmentfault.com/a/1190000007462819
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞