spring boot 部署到線上tomcat

asoren發表於2016-11-12

第一步:
將這個 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規定的目錄中自動解壓

相關文章