简体   繁体   English

Jetty maven插件在不同的端口上的多个webapps

[英]Jetty maven plugin multiple webapps on different ports

How can I run multiple webapps on different ports using the latest version of the jetty maven plugin? 如何使用最新版本的jetty maven插件在不同的端口上运行多个webapp?

org.eclipse.jetty:jetty-maven-plugin (version 9.2.2.v20140723 at the time of writing). org.eclipse.jetty:jetty-maven-plugin9.2.2.v20140723本文时为9.2.2.v20140723 )。

Eg, 例如,

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

The official documententation states this under httpConnector 官方文件记录httpConnector下说明了这一点

name: 
   The name of the connector, which is useful for configuring contexts to 
   respond only on particular connectors.

Great, so I configure a name but how do I bind that to a contextHandler ? 太好了,所以我配置了一个name但是如何将其绑定到contextHandler This is what I have so far 这就是我到目前为止所拥有的

<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>

This not yet migrated wiki suggests it can be done using the connectorNames property on the WebAppContext , but that's not available anymore. 这个尚未迁移的wiki表明它可以使用WebAppContext上的connectorNames属性WebAppContext ,但是不再可用。

Have a look at the documentation: 看看文档:

http://www.eclipse.org/jetty/documentation/current/serving-webapp-from-particular-port.html 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. 还可以使用具有命名连接器的虚拟主机机制的扩展来使某些Web应用程序仅可由特定连接器访问。 If a connector has a name "MyConnector" set using the setName method, then this can be referenced with the special virtual host name "@MyConnector". 如果连接器使用setName方法设置了名称“MyConnector”,则可以使用特殊虚拟主机名“@MyConnector”引用它。

You can then imply the context as it will contain the virtualhost: 然后,您可以隐含上下文,因为它将包含虚拟主机:

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

Using @ConnectorName: 使用@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). @ConnectorName连接器名称,它不是严格意义上的虚拟主机,而是仅匹配在具有与Connector.setName(String)匹配的名称的连接器上接收的请求。

The configuration in the links above is based on a separate jetty xml configuration file. 上面链接中的配置基于单独的jetty xml配置文件。 I haven't tested this but you can possibly insert this into your contextHandler (which has a setter): 我没有测试过这个,但你可以将它插入contextHandler(它有一个setter):

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

That should bind with the corresponding connector. 那应该与相应的连接器绑定。

You could also do this programmatically in Java. 您也可以在Java中以编程方式执行此操作。

Expanding on the answer of @BLuEGoD, here's the complete working configuration 扩展@BLuEGoD的答案,这是完整的工作配置

Plugin configuration: 插件配置:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.2.2.v20140723</version>
    <configuration>
        <jettyXml>path/to/jetty.xml</jettyXml>
    </configuration>
</plugin>

The jetty.xml configuration jetty.xml配置

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Set name="connectors">
    <Array type="org.eclipse.jetty.server.Connector">
        <Item>
            <New class="org.eclipse.jetty.server.ServerConnector">
                <Arg> <Ref refid="Server"/> </Arg>
                <Set name="port">8080</Set>
                <Set name="name">instance_8080</Set>
            </New>
        </Item>
        <Item>
            <New class="org.eclipse.jetty.server.ServerConnector">
                <Arg> <Ref refid="Server"/> </Arg>
                <Set name="port">8081</Set>
                <Set name="name">instance_8081</Set>
            </New>
        </Item>
    </Array>
</Set>

<New id="context-foo" class="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
    <Set name="contextPath">/</Set>
    <Set name="war">foo.war</Set>
    <Set name="virtualHosts">
        <Array type="java.lang.String">
            <Item>@instance_8080</Item>
        </Array>
    </Set>
</New>

<New id="context-bar" class="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
    <Set name="contextPath">/</Set>
    <Set name="war">bar.war</Set>
    <Set name="virtualHosts">
        <Array type="java.lang.String">
            <Item>@instance_8081</Item>
        </Array>
    </Set>
</New>

<Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
        <Set name="handlers">
            <Array type="org.eclipse.jetty.server.Handler">
                <Item>
                    <Ref refid="context-foo" />
                </Item>
                <Item>
                    <Ref refid="context-bar" />
                </Item>
                <Item>
                    <New class="org.eclipse.jetty.server.handler.DefaultHandler" />
                </Item>
            </Array>
        </Set>
    </New>
</Set>

I'd try to do it using jettyXml parameter instead of connector and contextHandlers . 我尝试使用jettyXml参数而不是connectorcontextHandlers Write an xml config file for each war and reference them in the jettyXml parameter in your pom. 为每个war写一个xml配置文件 ,并在你的pom中的jettyXml参数中引用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM