Java 更改 Jetty 默认端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24644114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:40:42  来源:igfitidea点击:

Change Jetty default port

javamavenjettymaven-jetty-plugin

提问by wag0325

Jetty default port is 8080, but I want to change to default port to some other port (9999).

Jetty 默认端口是 8080,但我想将默认端口更改为其他端口(9999)。

I read a few tutorials and they said almost all of configuration information is by default maintained in file jetty.xml, this file is located under $JETTY_HOME/etc/. Then, change property jetty.portto 9999. However, when I opened up that file, I couldn't find jetty.port property inside the jetty.xml. I'm currently using Jetty-9.2.1 and the port is at 8080.

我阅读了一些教程,他们说几乎所有配置信息默认都保存在文件中jetty.xml,该文件位于$JETTY_HOME/etc/. 然后,将属性更改jetty.port为 9999。但是,当我打开该文件时,在jetty.xml. 我目前使用的是 Jetty-9.2.1,端口是 8080。

I found jetty.port property under jetty-http.xml file. Even though I changed the port to 8090 in the jetty-http.xml file, jetty is still running at port 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

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

I was also advised to use an integration test to configure Jetty to use other port. There's a integration-tests.propertiesfile inside the project. Maybe a solution is to set jetty.port to 9999 inside this file?

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

integration-tests.properties:

集成测试.properties:

host = localhost
port = 9999

回答by Magnus Lassi

does it work if you set the port when you start it from the command line like this:

如果在从命令行启动它时设置端口,它是否有效,如下所示:

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

回答by Danilo Silveira

Just for completeness on jetty 7 you can use this:

只是为了在码头 7 上的完整性,你可以使用这个:

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

回答by Haidong Zhang

I changed the port successfully, you can try to edit jetty.portin the file located at $Jetty_home/start.d/http.ini.

我成功更改了端口,您可以尝试jetty.port在位于$Jetty_home/start.d/http.ini.

回答by sja

You need to change the http port in the start.ini file because it will over-right jetty-http.xml config.Or just comment the line in start.ini and keep your config from jetty-http.xml. In [jetty home]/start.ini

您需要更改 start.ini 文件中的 http 端口,因为它会覆盖 jetty-http.xml 配置。或者只是在 start.ini 中注释该行并保留 jetty-http.xml 中的配置。在[码头主页]/start.ini

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

回答by user3348367

I did this in Jetty 9.x version. You need to go to $JETTY_HOME/start.inifile and edit this setting jetty.port.

我在 Jetty 9.x 版本中做到了这一点。您需要转到$JETTY_HOME/start.ini文件并编辑此设置 jetty.port

Lets say that you want to run jetty at 9090 port: Please change jetty.portsetting in $JETTY_HOME/start.inifrom

假设您想在 9090 端口运行 jetty:请jetty.port$JETTY_HOME/start.inifrom 中更改设置

jetty.port=8080

to

 jetty.port=9090

Then start jetty using java -jar start.jaroption. Then jetty will be running at 9090 port than default 8080 port. Then do curl -i -XGET "http://localhost:9090". That should give you 200 http status.

然后使用java -jar start.jar选项启动码头。然后 jetty 将在 9090 端口而不是默认 8080 端口上运行。然后做curl -i -XGET "http://localhost:9090"。那应该给你 200 http 状态。

Thats it.

就是这样。

回答by amey91

Update:

更新:

On Jetty 9.x, jetty.porthas been deprecated and you can use jetty.http.portinstead, as shown below:

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

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

回答by sytolk

On jetty 9.2.3.v20140905 it`s need to write in /etc/default/jetty

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

but this change only http port. To change https port in jetty 9.2 create ini file $JETTY_HOME/start.d/https.ini

但这只是改变了 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

jetty 9.3in /etc/default/jetty

码头9.3英寸/etc/default/jetty

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

or command line parameters -Djetty.http.port=8080 -Djetty.ssl.port=443

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

回答by Haseeb Ahmad

If you are using eclipseyou need to set the run configurations. When you install jettyin eclipse, the default port for jetty is 8080.

如果您使用的是eclipse,则需要设置运行配置。在 eclipse 中安装jetty时,jetty的默认端口是8080

So you need to change it into an XML file. If the problem remains, you need to change it in eclipse run configurations.

所以你需要把它改成一个XML文件。如果问题仍然存在,则需要在 eclipse 运行配置中进行更改。

I hope it works, as it worked for me.

我希望它有效,因为它对我有效。

回答by Matthias B?

For IntelliJ, this can be done similar to Magnus Lassi's command-line answer.

对于 IntelliJ,这可以类似于 Magnus Lassi 的命令行答案来完成。

Run --> Edit Configurations --> Add "-Djetty.port=XXXX", e.g.:

运行 --> 编辑配置 --> 添加“-Djetty.port=XXXX”,例如:

jetty:run-war -Djetty.port=9999

回答by Milton BO

For SSL port you can pass the param:

对于 SSL 端口,您可以传递参数:

-Dssl.port=8445

It worked for me.

它对我有用。