gpt4 book ai didi

java - Jetty Maven 在不同端口上插入多个 Web 应用程序

转载 作者:行者123 更新时间:2023-12-02 13:56:17 35 4
gpt4 key购买 nike

如何使用最新版本的 jetty maven 插件在不同端口上运行多个 Web 应用程序?

org.eclipse.jetty:jetty-maven-plugin(撰写本文时的版本9.2.2.v20140723)。

例如,

foo.war -> localhost:8080/
bar.war -> localhost:8081/
baz.war -> localhost:8082/

The official documententationhttpConnector

下说明这一点
name: 
The name of the connector, which is useful for configuring contexts to
respond only on particular connectors.

太好了,所以我配置了一个名称,但是如何将其绑定(bind)到contextHandler?这是我到目前为止所拥有的

<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.2.v20140723</version>
<configuration>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<name>instance_8080</name>
</connector>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8081</port>
<name>instance_8081</name>
</connector>
</connectors>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
<war>a.war</war>
<contextPath>/</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
<war>b.war</war>
<contextPath>/</contextPath>
</contextHandler>
</contextHandlers>
</plugin>

这个not yet migrated wiki建议可以使用 WebAppContext 上的 connectorNames 属性来完成此操作,但这已不再可用。

最佳答案

查看文档:

http://www.eclipse.org/jetty/documentation/current/serving-webapp-from-particular-port.html

It is also possible to use an extension to the virtual host mechanism with named to connectors to make some web applications only accessible by specific connectors. If a connector has a name "MyConnector" set using the setName method, then this can be referenced with the special virtual host name "@MyConnector".

然后您可以暗示上下文,因为它将包含虚拟主机:

http://www.eclipse.org/jetty/documentation/current/configuring-virtual-hosts.html#different-virtual-hosts-different-contexts

使用@ConnectorName:

@ConnectorName A connector name, which is not strictly a virtual host, but instead will only match requests that are received on connectors that have a matching name set with Connector.setName(String).

上面链接中的配置基于单独的 jetty xml 配置文件。我还没有测试过这个,但你可以将它插入到你的 contextHandler (它有一个 setter)中:

<virtualHosts>
<virtualHost>@instance_8080</virtualHost>
</virtualHosts>

这应该与相应的连接器绑定(bind)。

您也可以在 Java 中以编程方式执行此操作。

关于java - Jetty Maven 在不同端口上插入多个 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596030/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com