Springboot項目部署在Jboss上的一些問題


前言


由於公司的問題,重構后的微服務必須要部署在Jboss上, 版本為Jboss EAP 7.1,Springboot 2.1.3.RELEASE。部署時候遇到了一些問題,在這記錄下來

 

一、修改Jboss根目錄為應用訪問目錄


首先將jboos的默認歡迎頁修改為空,否則會沖突,打開jboss-eap-7.1\standalone\configuration\standalone.xml, 找到

<subsystem xmlns="urn:jboss:domain:undertow:4.0">
            <buffer-cache name="default"/>
            <server name="default-server">
                <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
                <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
                <host name="default-host" alias="localhost">
                    <location name="/" handler="welcome-content"/>
                    <filter-ref name="server-header"/>
                    <filter-ref name="x-powered-by-header"/>
                    <http-invoker security-realm="ApplicationRealm"/>
                </host>
            </server>
            <servlet-container name="default">
                <jsp-config/>
                <websockets/>
            </servlet-container>
            <handlers>
                <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
            </handlers>
            <filters>
                <response-header name="server-header" header-name="Server" header-value="JBoss-EAP/7"/>
                <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
            </filters>
        </subsystem>

修改為

<subsystem xmlns="urn:jboss:domain:undertow:4.0">
            <buffer-cache name="default"/>
            <server name="default-server">
                <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
                <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
                <host name="default-host" alias="localhost">
                    <!--<location name="/" handler="welcome-content"/>-->
                    <filter-ref name="server-header"/>
                    <filter-ref name="x-powered-by-header"/>
                    <http-invoker security-realm="ApplicationRealm"/>
                </host>
            </server>
            <servlet-container name="default">
                <jsp-config/>
                <websockets/>
            </servlet-container>
            <!--<handlers>
                <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
            </handlers>-->
            <filters>
                <response-header name="server-header" header-name="Server" header-value="JBoss-EAP/7"/>
                <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
            </filters>
        </subsystem>

修改前記得備份,jboss啟動后會將這些注釋的部分自動清除

此時的根路徑已經為空,我們需要將項目路徑映射到根路徑,

新建一個文件jboss-web.xml,文件內容如下

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>/</context-root>
</jboss-web>

將我們的war包用WinRAR打開,這個文件放到WEB-INF目錄下

此時啟動jboss訪問根路徑就是我們的項目路徑了

 

二、Springboot項目打包成war的注意事項


<!-- 打war包時加入此項, 告訴spring-boot tomcat相關jar包用外部的,不要打進去 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

 尤其在使用非tomcat容器時,必須加入將tomcat相關包exclusions,或者像上面一樣改為provided,否則啟動容器時會有類轉換異常

三、Jboss正常啟動,但沒有啟動SpringBoot項目


可以看到Jboss正常啟動了,但是沒有Springboot項目啟動的標識,這個時候是由於Jboss無法識別我們的Servlet

正常的Springboot項目啟動類是這樣的。

@EnableEurekaServer
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class EurekaApplication {

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

這種方式在tomcat容器中啟動可以,但是Jboss無法這樣啟動,需要改成

@EnableEurekaServer
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class EurekaApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(EurekaApplication.class);
    }

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

這樣Jboss啟動時才能查找到我們的Servlet

 

四、Eureka啟動成功,可以注冊服務但是訪問頁面空白


...

可以看到此時的Springboot項目已經加載進來,服務也可以正常注冊,但打開Eureka頁面時一片空白

此時打開控制台發現,訪問路徑404(這里我沒有修改根路徑為項目路徑,Jboss默認war包名字是訪問路徑)

這是由於Springboot使用freeMark導致的

解決方案:

yml文件中添加

這個配置的意思是 是否優先從文件系統加載template,默認值為true

修改后再打包啟動

頁面可以正常訪問

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM