目前微服務項目用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中的packaging
爲war
類型
<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項目爲前後端分離,後端發佈後,指定前端路徑不成功,前端頁面無法訪問。目前解決辦法爲:
- 後端項目存放於${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請求}