在上一篇文章中,我描述了如何使用OpenLiberty和maven作为独立服务器或maven构建的一部分来启动服务器,以及如何创建fatjar包。
在这篇文章中,我正在研究如何使用Wildfly群。 我仍在尝试使MicroProfile在Wildfly full上运行,因此,到目前为止,该示例与OpenLiberty示例的工作方式不同。
我使用的是同一个示例项目 ,其中包含更多的maven配置文件以运行不同的部署选项。
(请参阅https://github.com/phillip-kruger/javaee-servers-parent )
示例项目
我想包含一些MicroProfile功能,因此这是一个“每日报价”应用程序,而不是基本的“ Hello world”。 我的应用程序使用工厂加载报价提供程序(目前只有一个)。 当前提供者从forismatic.com获得报价。 我使用MicroProfile Configuration API配置诸如URL和要加载的提供程序之类的东西。 我使用MicroProfile Fault Tolerance API来确保在提供程序源不可用时我们能够生存。
您可以在此处获取完整的示例项目: https : //github.com/phillip-kruger/quote-service
作为Maven构建的一部分运行
您可以使用wildfly-swarm-plugin运行( mvn wildfly-swarm:run
)作为构建一部分的wildfly swarm实例。 该插件将执行“分数检测”,这意味着它将查看您需要的应用服务器的哪些部分,并且仅创建包含这些分数的部署。 因此,您仍然可以将伞状API包含在依赖关系中,并针对这些依赖关系进行编码,但是在部署时,您将获得正确的大小分布。 好酷!
<dependencies><!-- Java EE --><dependency><groupId>javax</groupId><artifactId>javaee-api</artifactId><version>${java-ee.version}</version><scope>provided</scope></dependency><!-- MicroProfile --><dependency><groupId>org.eclipse.microprofile</groupId><artifactId>microprofile</artifactId><version>${microProfile.version}</version><type>pom</type><scope>provided</scope></dependency></dependencies>
在包含引用webjars的 HTML文件时,我总是使用过滤,但是似乎插件在应用过滤器之前使用了原始源文件,因此我不得不寻找一种替代方法。
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.0.0</version><configuration><webResources><resource><directory>${basedir}/src/main/webapp</directory><filtering>true</filtering><includes><include>**/*.css</include><include>**/*.jsp</include></includes></resource></webResources></configuration></plugin>
在此示例中,我使用语义UI来构建显示当天报价的网页:
我将maven属性用于HTML中CSS和JS版本,并且在构建时需要用实际值替换它们:
<link rel="stylesheet" type="text/css" href="webjars/semantic-ui/${semantic-ui.version}/dist/semantic.min.css"><script type="text/javascript" src="webjars/jquery/${jquery.version}/dist/jquery.min.js" /><script type="text/javascript" src="webjars/semantic-ui/${semantic-ui.version}/dist/semantic.min.js"></script>
作为替代,我使用包目标,然后使用exec-maven-plugin
运行jar。
这也使我可以传递standalone.xml
进行任何其他配置:
<plugin><groupId>org.wildfly.swarm</groupId><artifactId>wildfly-swarm-plugin</artifactId><executions><execution><id>1</id><phase>pre-integration-test</phase><goals><goal>package</goal></goals></execution></executions></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.6.0</version><executions><execution><id>1</id><phase>post-integration-test</phase><goals><goal>exec</goal></goals></execution></executions><configuration><executable>java</executable><arguments><argument>-jar</argument><argument>${project.build.directory}${file.separator}${project.artifactId}-swarm.jar</argument><argument>-c</argument><argument>${project.build.directory}${file.separator}standalone.xml</argument></arguments></configuration></plugin>
在我的情况下, standalone.xml
仅包含日志记录配置,但是您现在可以包括任何其他配置。
<server xmlns="urn:jboss:domain:4.0"><profile><subsystem xmlns="urn:jboss:domain:logging:3.0"><periodic-rotating-file-handler name="FILE" autoflush="true"><file path="${wildfly-swarm.logfile}"/><suffix value=".yyyy-MM-dd"/><append value="true"/></periodic-rotating-file-handler><root-logger><level name="INFO"/><handlers><handler name="FILE"/></handlers></root-logger><logger category="${log.name}"><level name="${log.level}"/></logger></subsystem></profile></server>
因此,在qoute-service
示例中,您可以执行此操作(与OpenLiberty示例相同):
mvn clean install -P wildfly-swarm-fatjar
Hollowjar
Wildfly群可让您创建空心罐。 (请参阅本文 )也就是说,没有应用程序的胖子,仅仅是应用程序服务器的一部分。 然后,您可以将应用程序作为命令行输入提供:
java -jar myapp-hollow-swarm.jar myapp.war
因此,如果我们能找到一种方法来重新加载应用程序部分,那么我们可以拥有与完整应用程序相同的开发模型(热部署)。
部署扫描器
Wildfly群有一个称为Deployment Scanner的部分,您可以将其包含在您的发行版中(胖或空心)。
分数检测将不会自动检测到此(因为在代码中没有对此的引用)。 幸运的是,您可以在Maven中定义其他分数:
<plugin><groupId>org.wildfly.swarm</groupId><artifactId>wildfly-swarm-plugin</artifactId><executions><execution><phase>pre-integration-test</phase><goals><goal>package</goal></goals></execution></executions><configuration><hollow>true</hollow><additionalFractions>scanner</additionalFractions></configuration></plugin>
为了使扫描程序工作,将其添加到standalone.xml
<subsystem xmlns="urn:jboss:domain:deployment-scanner:2.0"><deployment-scanner scan-enabled="true"scan-interval="5000" path="/tmp/quote-service/wildfly-swarm/deployments" name="quote-service" auto-deploy-xml="false"/> </subsystem>
如果现在将应用程序的更新版本移至定义的路径,则可以进行热部署。
在引用示例中,这意味着您可以:
- mvn clean install -P wildfly-swarm-start(启动服务器)
- mvn clean install -P wildfly-swarm-deploy(热部署到正在运行的服务器)
- mvn clean install -P wildfly-swarm-stop(停止正在运行的服务器)
您还可以创建一个胖子:
- mvn全新安装-P软件包
节约时间
建立和启动胖子大约需要10秒钟 。 热部署大约需要2.7秒 。
这节省了大量时间,从而使更改之间的周转时间更快。
翻译自: https://www.javacodegeeks.com/2018/01/hollowjars-deployment-scanner-wildfly-swarm-cool.html