基于tomcat发布springboot单应用项目

 

目前微服务项目用jar包发布的,鉴于以后有可能会jar包转为war包,于tomcat、websphere、weblogic等平台发布。

转换步骤如下:

一. 项目普遍配置:

1. 转化jar类型项目为可部署的war文件的第一步是提供一个SpringBootServletInitializer子类和覆盖它的configure方法。通常做法是,让应用程序的入口类继承SpringBootServletInitializer

@SpringBootApplication

public class SIFServicePlatform extends SpringBootServletInitializer {



    @Override

 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(SIFServicePlatform.class);

 }



    public static void main(String[] args) {

        SpringApplication.run(SIFServicePlatform.class, args);

 }

}

注意:不同版本继承的SpringBootServletInitializer类不同 
1.3.3版本为org.springframework.boot.context.web.SpringBootServletInitializer 
1.4.1以上版本为org.springframework.boot.web.support.SpringBootServletInitializer

2. 若项目使用maven并且pom.xml继承了spring-boot-starter-parent,

需要更改pom.xml中的packagingwar类型 

<packaging>war</packaging>

 

 

若使用Gradle:

apply plugin: 'war'

3. 确保嵌入servlet容器不干扰外部servlet容器部署war文件,若引入了内部tomcat需排除掉:

maven下:

 

<exclusions>

 <exclusion>

 <artifactId>spring-boot-starter-tomcat</artifactId>

 <groupId>org.springframework.boot</groupId>

 </exclusion>

</exclusions>

 

需要标记嵌入servlet容器的依赖为provided

<dependency>

 <groupId>org.apache.tomcat</groupId>

 <artifactId>tomcat-servlet-api</artifactId>

 <version>7.0.42</version>

 <scope>provided</scope>

</dependency>

war在部署到容器中时遇到Project facet Cloud Foundry Standalone Application version 1.0 is not supported.错误; 
解决办法: 项目右键Build Path -> Configure Build Path -> Project facet -> 勾掉Cloud Foundry Standalone Application

 

. 项目内部部分, 目前sif项目为前后端分离,后端发布后,指定前端路径不成功,前端页面无法访问。目前解决办法为:

  1. 后端项目存放于${tomcat_path}\webapps目录下,解压缩后包名起为api, 修改${tomcat_path}\conf\server.xml,添加以下信息:
    <Context path="/" reloadable="true" debug="0" docBase="静态资源路径" />
    例如:
三. tomcat部分, tomcat需要配置运行文件,windows下为catalina.bat

    添加-Dspring.profiles.active=dev-tomcat 如下

    set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dspring.profiles.active=dev-tomcat"

    linux下为windows下为catalina.sh

    启动tomcat,此时项目访问路径为:http://{ip}:{port}/

注: 后端项目包名不能跟path中名称相同,前端访问后端时,前缀为 后端项目包名+api

例如:保险项目中前端访问后端时 URL请求为http:{IP}:{port}/api/v1/{api请求},此时war包解压至{tomcat}/webapps下

        名称为api,后端访问配置为/v1/{api请求}

 

猜你喜欢

转载自blog.csdn.net/ccc7574/article/details/84069181