更改Jetty默认端口

29
Jetty默认端口是8080,但我想将其更改为其他端口(9999)。我阅读了一些教程,它们都说默认情况下几乎所有的配置信息都在文件jetty.xml中维护,该文件位于$JETTY_HOME/etc/下。然后,将属性jetty.port更改为9999。然而,当我打开那个文件时,我在jetty.xml中找不到jetty.port属性。我目前正在使用Jetty-9.2.1,并且端口号为8080。
我在jetty-http.xml文件中找到了jetty.port属性。尽管我在jetty-http.xml文件中将端口更改为8090,但是jetty仍在端口8080上运行。 jetty.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- =============================================================== -->
<!-- Documentation of this file format can be found at:              -->
<!-- http://wiki.eclipse.org/Jetty/Reference/jetty.xml_syntax        -->
<!--                                                                 -->
<!-- Additional configuration files are available in $JETTY_HOME/etc -->
<!-- and can be mixed in. See start.ini file for the default         -->
<!-- configuration files.                                            -->
<!--                                                                 -->
<!-- For a description of the configuration mechanism, see the       -->
<!-- output of:                                                      -->
<!--   java -jar start.jar -?                                        -->
<!-- =============================================================== -->

<!-- =============================================================== -->
<!-- Configure a Jetty Server instance with an ID "Server"           -->
<!-- Other configuration files may also configure the "Server"       -->
<!-- ID, in which case they are adding configuration to the same     -->
<!-- instance.  If other configuration have a different ID, they     -->
<!-- will create and configure another instance of Jetty.            -->
<!-- Consult the javadoc of o.e.j.server.Server for all              -->
<!-- configuration that may be set here.                             -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Configure the Server Thread Pool.                           -->
    <!-- The server holds a common thread pool which is used by      -->
    <!-- default as the executor used by all connectors and servlet  -->
    <!-- dispatches.                                                 -->
    <!--                                                             -->
    <!-- Configuring a fixed thread pool is vital to controlling the -->
    <!-- maximal memory footprint of the server and is a key tuning  -->
    <!-- parameter for tuning.  In an application that rarely blocks -->
    <!-- then maximal threads may be close to the number of 5*CPUs.  -->
    <!-- In an application that frequently blocks, then maximal      -->
    <!-- threads should be set as high as possible given the memory  -->
    <!-- available.                                                  -->
    <!--                                                             -->
    <!-- Consult the javadoc of o.e.j.util.thread.QueuedThreadPool   -->
    <!-- for all configuration that may be set here.                 -->
    <!-- =========================================================== -->
    <!-- uncomment to change type of threadpool
    <Arg name="threadpool"><New id="threadpool" class="org.eclipse.jetty.util.thread.QueuedThreadPool"/></Arg>
    -->
    <Get name="ThreadPool">
      <Set name="minThreads" type="int"><Property name="threads.min" default="10"/></Set>
      <Set name="maxThreads" type="int"><Property name="threads.max" default="200"/></Set>
      <Set name="idleTimeout" type="int"><Property name="threads.timeout" default="60000"/></Set>
      <Set name="detailedDump">false</Set>
    </Get>

    <!-- =========================================================== -->
    <!-- Add shared Scheduler instance                               -->
    <!-- =========================================================== -->
    <Call name="addBean">
      <Arg>
        <New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler"/>
      </Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- Http Configuration.                                         -->
    <!-- This is a common configuration instance used by all         -->
    <!-- connectors that can carry HTTP semantics (HTTP, HTTPS, SPDY)-->
    <!-- It configures the non wire protocol aspects of the HTTP     -->
    <!-- semantic.                                                   -->
    <!--                                                             -->
    <!-- This configuration is only defined here and is used by      -->
    <!-- reference from the jetty-http.xml, jetty-https.xml and      -->
    <!-- jetty-spdy.xml configuration files which instantiate the    -->
    <!-- connectors.                                                 -->
    <!--                                                             -->
    <!-- Consult the javadoc of o.e.j.server.HttpConfiguration       -->
    <!-- for all configuration that may be set here.                 -->
    <!-- =========================================================== -->
    <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
      <Set name="secureScheme">https</Set>
      <Set name="securePort"><Property name="jetty.secure.port" default="8443" /></Set>
      <Set name="outputBufferSize"><Property name="jetty.output.buffer.size" default="32768" /></Set>
      <Set name="requestHeaderSize"><Property name="jetty.request.header.size" default="8192" /></Set>
      <Set name="responseHeaderSize"><Property name="jetty.response.header.size" default="8192" /></Set>
      <Set name="sendServerVersion"><Property name="jetty.send.server.version" default="true" /></Set>
      <Set name="sendDateHeader"><Property name="jetty.send.date.header" default="false" /></Set>
      <Set name="headerCacheSize">512</Set>
      <!-- Uncomment to enable handling of X-Forwarded- style headers
      <Call name="addCustomizer">
        <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg>
      </Call>
      -->
    </New>


    <!-- =========================================================== -->
    <!-- Set the default handler structure for the Server            -->
    <!-- A handler collection is used to pass received requests to   -->
    <!-- both the ContextHandlerCollection, which selects the next   -->
    <!-- handler by context path and virtual host, and the           -->
    <!-- DefaultHandler, which handles any requests not handled by   -->
    <!-- the context handlers.                                       -->
    <!-- Other handlers may be added to the "Handlers" collection,   -->
    <!-- for example the jetty-requestlog.xml file adds the          -->
    <!-- RequestLogHandler after the default handler                 -->
    <!-- =========================================================== -->
    <Set name="handler">
      <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
         <Array type="org.eclipse.jetty.server.Handler">
           <Item>
             <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
           </Item>
           <Item>
             <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
           </Item>
         </Array>
        </Set>
      </New>
    </Set>

    <!-- =========================================================== -->
    <!-- extra server options                                        -->
    <!-- =========================================================== -->
    <Set name="stopAtShutdown">true</Set>
    <Set name="stopTimeout">5000</Set>
    <Set name="dumpAfterStart"><Property name="jetty.dump.start" default="false"/></Set>
    <Set name="dumpBeforeStop"><Property name="jetty.dump.stop" default="false"/></Set>

</Configure>

jetty-http.xml

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Add a HTTP Connector.                                       -->
  <!-- Configure an o.e.j.server.ServerConnector with a single     -->
  <!-- HttpConnectionFactory instance using the common httpConfig  -->
  <!-- instance defined in jetty.xml                               -->
  <!--                                                             -->
  <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
  <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  <!-- that may be set here.                                       -->
  <!-- =========================================================== -->
  <Call name="addConnector">
    <Arg>
      <New class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Arg name="factories">
          <Array type="org.eclipse.jetty.server.ConnectionFactory">
            <Item>
              <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="httpConfig" /></Arg>
              </New>
            </Item>
          </Array>
        </Arg>
        <Set name="host"><Property name="jetty.host" /></Set>
        <Set name="port"><Property name="jetty.port" default="8090" /></Set>
        <Set name="idleTimeout"><Property name="http.timeout" default="30000"/></Set>
        <Set name="soLingerTime"><Property name="http.soLingerTime" default="-1"/></Set>
      </New>
    </Arg>
  </Call>

</Configure>

我还被建议使用一个集成测试来配置Jetty使用其他端口。 项目内有一个integration-tests.properties文件。 也许解决方案是在此文件中将 jetty.port 设置为9999?

integration-tests.properties:

host = localhost
port = 9999

在 jetty-http.xml 文件中更改 jetty.port 设置对我有用。也许您有另一个使用端口 9999 的应用程序? - slim
更改 SSL 默认端口的方法:https://dev59.com/u-o6XIcBkEYKwwoYJAvV#72082416 - MHSaffari
13个回答

35

如果你在命令行中这样启动它时设置端口,它会起作用吗:

这样启动它时设置端口是否有效:

java -jar start.jar -Djetty.port=9999

这不会改变默认端口。运行这行代码后,当我运行java -jar start.jar时,Jetty仍然在8080端口下运行。我正在寻找一种永久更改默认端口的方法。 - wag0325
@wag0325 我只是想知道是否有区别来解决问题。在我更新了jetty/start.d/http.ini并将端口更改为9999后,运行默认设置:java -jar start.jar,它会在9999端口上启动。如果您需要从maven运行它,则此帖子可能会有所帮助:链接 - Magnus Lassi
1
文档中说.. mvn -Djetty.http.port=8181 jetty:run 但你的建议对我有效。可能是因为我定义了上下文路径,像这样 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.14.v20131031</version> <configuration> <webAppConfig> <contextPath>/http</contextPath> </webAppConfig> </configuration> </plugin> - Himalay Majumdar
mvn -Djetty.port=8181 jetty:run 帮助您在 8181 端口启动 Jetty。 - Anunay
1
顺便提一下,如果在Spring Boot中使用Jetty,应该使用--server.port=8090 - JSelser

11
我在Jetty 9.x版本中完成了这个操作。你需要前往 $JETTY_HOME/start.ini 文件并编辑此设置 jetty.port
假设你想在9090端口上运行Jetty: 请在$JETTY_HOME/start.ini中更改jetty.port设置。
jetty.port=8080

 jetty.port=9090

然后使用 java -jar start.jar 选项启动 Jetty。 然后 Jetty 将运行在 9090 端口而不是默认的 8080 端口。 然后执行 curl -i -XGET "http://localhost:9090"。这应该会给你一个200 http状态。

就这样。


10

更新:

在Jetty 9.x中,jetty.port已被弃用,您可以改用jetty.http.port,如下所示:

$> cd $JETTY_HOME && java -jar start.jar -Djetty.http.port=8080

7
在Jetty 9.2.3.v20140905中,需要在/etc/default/jetty中编写。
# JETTY_ARGS
#   The default arguments to pass to jetty.
#   For example
JETTY_ARGS="jetty.port=8080 jetty.spdy.port=8443 jetty.secure.port=443"

但这只是改变了HTTP端口。要在Jetty 9.2中更改HTTPS端口,请创建ini文件$JETTY_HOME/start.d/https.ini
# Initialize module https
#
--module=https
## HTTPS Configuration
# HTTP port to listen on
https.port=8443
# HTTPS idle timeout in milliseconds
https.timeout=30000
# HTTPS Socket.soLingerTime in seconds. (-1 to disable)
# https.soLingerTime=-1

/etc/default/jetty中,jetty 9.3

# JETTY_ARGS
# The default arguments to pass to jetty.
# For example
JETTY_ARGS="jetty.http.port=8080 jetty.ssl.port=443"

或者命令行参数-Djetty.http.port=8080 -Djetty.ssl.port=443


4

我已经成功修改了端口,您可以尝试在位于$Jetty_home/start.d/http.ini的文件中编辑jetty.port


3

您需要在start.ini文件中更改http端口,因为它会覆盖jetty-http.xml配置文件。或者您可以注释掉start.ini中的行,并保留来自jetty-http.xml的配置。

[Jetty Home]/start.ini

## HTTP port to listen on
#jetty.port=8080

2

仅为完整性,关于jetty 7,您可以使用以下内容:

java -jar start.jar --module=http jetty.port=9080

那是一个很棒的答案。 - Mihai Alexandru-Ionut

2
如果您正在使用Eclipse,则需要设置运行配置。当您在Eclipse中安装Jetty时,Jetty的默认端口为8080。
因此,您需要将其更改为XML文件。如果问题仍然存在,则需要在Eclipse运行配置中进行更改。
希望它能够起作用,就像对我起作用一样。

2

对于IntelliJ IDE,可以按照 Magnus Lassi 的命令行方式进行相似操作。

选择 Run --> Edit Configurations --> 添加 "-Djetty.port=XXXX",例如:

jetty:run-war -Djetty.port=9999

0

我在jetty/start.ini中更改了jetty.http.port属性,这对我起作用了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接