Open Liberty是IBM发布的开源的Java微服务运行时,精简版的WAS服务器。精简程度和地位类似于WildFly(原Jboss服务器)的全功能版与Servlet版,全功能版不是太重了么,搞个精简的可以跑一些基本的微服务和Servlet。一定程度上说是对开发者友好。能否将Open Liberty这个精简版的WAS服务器放到生产环境还有待观察,官方似乎也没给个教程啥的。
D:.
└─ServletSampleServer
│ .gitignore
│ pom.xml
│ README.md
│
└─src
└─main
├─java
├─liberty
│ └─config
├─resources
└─webapp
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.wasdev.wlp.maven.parent</groupId>
<artifactId>liberty-maven-app-parent</artifactId>
<version>2.0</version>
</parent>
<groupId>io.openliberty.guides</groupId>
<artifactId>ServletSample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<!-- Add the rest of the POM below here. -->
<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>
<app.name>${project.artifactId}</app.name>
<testServerHttpPort>9080</testServerHttpPort>
<testServerHttpsPort>9443</testServerHttpsPort>
<warContext>${app.name}</warContext>
<package.file>${project.build.directory}/${app.name}.zip</package.file>
<packaging.type>usr</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<packagingExcludes>pom.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<assemblyArtifact>
<groupId>io.openliberty</groupId>
<artifactId>openliberty-runtime</artifactId>
<version>17.0.0.3</version>
<type>zip</type>
</assemblyArtifact>
<serverName>${project.artifactId}Server</serverName>
<stripVersion>true</stripVersion>
<configFile>src/main/liberty/config/server.xml</configFile>
<packageFile>${package.file}</packageFile>
<include>${packaging.type}</include>
<bootstrapProperties>
<default.http.port>${testServerHttpPort}</default.http.port>
<default.https.port>${testServerHttpsPort}</default.https.port>
<app.context.root>${warContext}</app.context.root>
</bootstrapProperties>
</configuration>
<executions>
<execution>
<id>package-server</id>
<phase>package</phase>
<goals>
<goal>package-server</goal>
</goals>
<configuration>
<outputDirectory>target/wlp-package</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
mvn clean
mvn install
mvn package
mvn liberty:start-server
mvn liberty:stop-server
D:.
└─ServletSample
│ .gitignore
│ pom.xml
│ README.md
│
└─src
├─main
│ ├─java
│ │ └─io
│ │ └─openliberty
│ │ └─guides
│ │ └─hello
│ │ HelloServlet.java
│ │
│ ├─liberty
│ │ └─config
│ │ server.xml
│ │
│ ├─resources
│ └─webapp
│ │ index.html
│ │
│ └─WEB-INF
│ web.xml
│
└─test
└─java
└─io
└─openliberty
└─guides
└─hello
└─it
EndpointIT.java
<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>
<parent>
<groupId>net.wasdev.wlp.maven.parent</groupId>
<artifactId>liberty-maven-app-parent</artifactId>
<version>2.0</version>
</parent>
<!-- Add the rest of the POM below here. -->
<groupId>io.openliberty.guides</groupId>
<artifactId>ServletSample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<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>
<app.name>${project.artifactId}</app.name>
<testServerHttpPort>9080</testServerHttpPort>
<testServerHttpsPort>9443</testServerHttpsPort>
<warContext>${app.name}</warContext>
<package.file>${project.build.directory}/${app.name}.zip</package.file>
<packaging.type>usr</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<packagingExcludes>pom.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<assemblyArtifact>
<groupId>io.openliberty</groupId>
<artifactId>openliberty-runtime</artifactId>
<version>17.0.0.3</version>
<type>zip</type>
</assemblyArtifact>
<serverName>${project.artifactId}Server</serverName>
<stripVersion>true</stripVersion>
<configFile>src/main/liberty/config/server.xml</configFile>
<packageFile>${package.file}</packageFile>
<include>${packaging.type}</include>
<bootstrapProperties>
<default.http.port>${testServerHttpPort}</default.http.port>
<default.https.port>${testServerHttpsPort}</default.https.port>
<app.context.root>${warContext}</app.context.root>
</bootstrapProperties>
</configuration>
<executions>
<execution>
<id>package-server</id>
<phase>package</phase>
<goals>
<goal>package-server</goal>
</goals>
<configuration>
<outputDirectory>target/wlp-package</outputDirectory>
</configuration>
</execution>
</executions>
</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>
<configuration>
<includes>
<include>**/it/**</include>
</includes>
<systemPropertyVariables>
<liberty.test.port>${testServerHttpPort}</liberty.test.port>
<war.name>${warContext}</war.name>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package io.openliberty.guides.hello;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/servlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Hello! How are you today?\n");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<server description="Sample Servlet server">
<featureManager>
<feature>servlet-3.1</feature>
</featureManager>
<httpEndpoint httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint" host="*" />
<webApplication id="ServletSample" location="ServletSample.war" contextRoot="${app.context.root}" />
</server>
<html>
<body>
<h2>Welcome to Hello Servlet</h2>
<p>
<a href="servlet">Click here</a> to get a greeting from the Hello Servlet.
</p>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Hello Servlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
package io.openliberty.guides.hello.it;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
public class EndpointIT {
private static String URL;
@BeforeClass
public static void init() {
String port = System.getProperty("liberty.test.port");
String war = System.getProperty("war.name");
URL = "http://localhost:" + port + "/" + war + "/" + "servlet";
}
@Test
public void testServlet() throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(URL);
try {
int statusCode = client.executeMethod(method);
assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);
String response = method.getResponseBodyAsString(1000);
assertTrue("Unexpected response body", response.contains("Hello! How are you today?"));
} finally {
method.releaseConnection();
}
}
}
mvn install
mvn package
mvn liberty:start-server
mvn liberty:stop-server