运行 spring-在 Liberty 服务器上启动应用程序

Run spring-boot application on Liberty Server

我开发了一个 独立 spring 引导应用程序 来提供休息服务,我将其打包为 jar 并且它 运行s 正确。 为了在生产服务器(Websphere)上发布它,我必须将它转换成 war.

我更新了我的 pom.xml 添加了这些行:

<properties>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
</properties>
<packaging>war</packaging>
<build>
    <finalName>${artifactId}</finalName>
</build>

然后,我启动了 maven clean package,我得到了 war。 因此,在 Tomcat 服务器上发布它工作正常,但在 Liberty 上我收到错误:

Error 404: SRVE0190E: File not found /

我还修改了我的入口点,如下所示:

@SpringBootApplication
@PropertySource("classpath:alerts.properties")
public class WebApplication extends SpringBootServletInitializer 
{

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }
}

没有成功,所以...我如何在 Liberty 服务器上 运行 spring 引导 war?

更新

正如 @Anjum Fatima 所建议的,我还添加了 server.xml

中的功能
<feature>jsp-2.3</feature>
<feature>springBoot-2.0</feature>
<feature>servlet-3.1</feature>

之前,在该文件中我还添加了:

<library>
    <file id="alerts.properties" name="${shared.resource.dir}/alerts.properties"/>
</library>
<webApplication id="alerts" location="alerts.war" name="alerts">

并且在 wlp18\usr\shared\resources 中我复制了属性文件。

但我仍然有同样的错误。

alert.properties 文件:

spring.datasource.url=jdbc:h2:file:~/alertsdb;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.username=admin
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.h2.Driver

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true

spring.mvc.view.prefix: /
spring.mvc.view.suffix: .jsp
spring.messages.basename=validation

更新 2

服务器快启动了,但是找不到像com.ibm.ws.kernel.boot.nls_1.0.22.jar这样的文件,在Liberty的lib文件夹中有文件com.ibm.ws.kernel.boot_1.0.22.jar(没有.nls)这里是控制台输出,我错过了什么?

2020-03-18 13:44:38.950  INFO 34004 --- [ecutor-thread-4] it.mycompany.alerts.WebApplication       : Started WebApplication in 10.915 seconds (JVM running for 24.544)
[WARNING ] The XML schema [web-jsptaglibrary_2_1.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_5.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [jsp_2_1.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_web_services_1_2.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_web_services_client_1_2.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-app_3_0.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-fragment_3_0.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-common_3_0.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_6.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [jsp_2_2.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_web_services_1_3.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_web_services_client_1_3.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-app_3_1.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-fragment_3_1.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-common_3_1.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_7.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_web_services_1_4.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_web_services_client_1_4.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-app_4_0.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-fragment_4_0.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [web-common_4_0.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] The XML schema [javaee_8.xsd] could not be found. This is very likely to break XML validation if XML validation is enabled.
[WARNING ] Failed to scan [file:/C:/lang/appserver/wlp/wlp18/lib/com.ibm.ws.kernel.boot.nls_1.0.22.jar] from classloader hierarchy
C:\lang\appserver\wlp\wlp18\lib\com.ibm.ws.kernel.boot.nls_1.0.22.jar (File not found)

谢谢

您不需要 运行 Spring 启动 jar 作为 war 自由。这是关于如何在 liberty 中部署 Spring Boot jar 的 liberty 指南:https://openliberty.io/guides/spring-boot.html There is another great article about running Spring Boot applications in liberty here: https://developer.ibm.com/articles/modernize-and-optimize-spring-boot-applications/

您可以在 https://github.com/anjumfatima90/dojo.samples 中查看示例 Spring Boot war 应用程序。它具有部署自由的步骤。希望对您有所帮助。

更新: 有两种方法可以在 open-liberty 中部署 spring 引导 war,您将其部署为普通的旧 WAR,不需要添加 springBoot-2.0 功能到 server.xml。我的 github 示例展示了另一种方法,您可以在 server.xml 中添加 springBoot-2.0 功能,同时更改 <application> 标签,如 https://www.ibm.com/support/knowledgecenter/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_dep_springboot.html

考虑您的方式,即作为普通旧方式部署 WAR。 Scott 已克隆您的应用程序并进行了一些更改以在他的存储库 https://github.com/scottkurz/springboot-liberty-1 中添加 liberty-maven-plugin 以帮助您完成此操作。

工作步骤:

  1. 使用 git clone https://github.com/scottkurz/springboot-liberty-1.git.
  2. 克隆存储库
  3. 运行 mvn clean install
  4. 运行 mvn liberty:run
  5. 您会发现在应用程序的目标文件夹中创建了一个 liberty 文件夹。您可以将您的服务器与 target/liberty/wlp/usr/servers/defaultServer.
  6. 进行比较

注意:斯科特在申请的src/main/resources中添加了alerts.properties

如果您将应用程序打包为真正的 war 而不是转换后的 jar 文件,那么您所需要的只是 servlet 功能和 dropins 文件夹。

我的server.xml:

<server description="Tester">
    <featureManager>
        <feature>servlet-4.0</feature>
    </featureManager>

    <httpEndpoint host="*" httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
</server>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ibm.example</groupId>
    <artifactId>SpringbootWeb</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>

    <parent>
        <groupId>io.openliberty.tools</groupId>
        <artifactId>liberty-maven-app-parent</artifactId>
        <version>3.2</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <packaging.type>minify,runnable</packaging.type>
    </properties>

    <profiles>
        <profile>
            <id>usr-package</id>
            <properties>
                <packaging.type>usr</packaging.type>
            </properties>
        </profile>
    </profiles>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <packagingExcludes>pom.xml</packagingExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>io.openliberty.tools</groupId>
                <artifactId>liberty-maven-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <assemblyArtifact>
                        <groupId>io.openliberty</groupId>
                        <artifactId>openliberty-runtime</artifactId>
                        <version>[18.0.0.1,)</version>
                        <type>zip</type>
                    </assemblyArtifact>
                    <serverName>${project.artifactId}Server</serverName>
                    <include>${packaging.type}</include>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.5.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

当您 运行 在 Maven 中达到 package 目标时,它将创建一个 war 和一个包含 liberty 运行time 包的 jar 格式的 jar。您可以将 war 放入当前应用程序的 dropins 文件夹中。确保 Liberty messages.log 文件中没有其他错误。

我在 GitHub 上的例子 -布莱恩

终于成功了,我开始了一个新项目。 这里的pom.xmlliberty-maven-plugin):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>it.mycompany</groupId>
    <artifactId>alerts</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>com.ibm.mq.allclient</artifactId>
            <version>9.1.4.0</version>
        </dependency>
    </dependencies>

    <packaging>war</packaging>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>io.openliberty.tools</groupId>
                <artifactId>liberty-maven-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <appsDirectory>apps</appsDirectory>
                    <mainClass>${start-class}</mainClass>
                </configuration>
            </plugin> 
        </plugins>
    </build>
</project>

入口点class:

@SpringBootApplication
@PropertySource("classpath:alerts.properties")
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }
}

我用 mvn clean install 创建了 WAR 然后我将它从目标目录复制到 wlp/servers/myserver/apps (不是 dropins

最后我将 server.xml 设置如下:

<server description="Tester">
    <featureManager>
        <feature>servlet-4.0</feature>
        <feature>localConnector-1.0</feature>
        <feature>jsp-2.3</feature>
        <feature>springBoot-2.0</feature>
    </featureManager>

    <httpEndpoint host="*" httpPort="9080" httpsPort="9443"
        id="defaultHttpEndpoint" />

    <library>
        <file id="alerts.properties"
            name="${shared.resource.dir}/alerts.properties" />
    </library>
    <webApplication id="alerts" location="alerts.war"
        name="alerts" contextRoot="/" />
</server>

Liberty 服务器启动正常,应用程序工作,唯一的问题是响应 localhost:9080/login 而不是 localhost:9080/alerts/login。 但是问题的目标已经实现,我创建了一个 war 文件并在 Liberty 服务器上运行。

感谢所有人,尤其感谢@ScottKurz 和@Anjum Fatima。