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

修改后再打包启动

页面可以正常访问

猜你喜欢

转载自www.cnblogs.com/gtblog/p/11237070.html