更改Dropwizard默认端口

use*_*449 47 java rest jersey dropwizard

我在默认端口8080(服务)和8081(管理员)上运行基于Dropwizard的Jersey REST服务,我需要将默认端口更改为不太常用的端口,我无法找到任何信息,请有人指点我这样做吗?

con*_*dit 90

您可以更新yaml配置文件中的端口:

http:
  port: 9000
  adminPort: 9001
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅http://www.dropwizard.io/0.9.2/docs/manual/configuration.html#http.

编辑

如果您已迁移到Dropwizard 0.7.x,0.8.x,0.9.x,则可以使用以下内容:

server:
  applicationConnectors:
  - type: http 
    port: 9000
  adminConnectors:
  - type: http
    port: 9001
Run Code Online (Sandbox Code Playgroud)

  • 在`server`之后将配置添加到命令行.有关详细信息,请参阅http://dropwizard.codahale.com/getting-started/#running-your-service.它应该具有预期的效果. (4认同)

Fer*_*nch 31

在命令行中,您可以在Dropwizard 0.6中以这种方式设置它们:

java -Ddw.http.port=9090 -Ddw.http.adminPort=9091 -jar yourapp.jar server yourconfig.yml
Run Code Online (Sandbox Code Playgroud)

如果您使用Dropwizard 0.7,系统属性将以这种方式设置:

java -Ddw.server.applicationConnectors[0].port=9090 -Ddw.server.adminConnectors[0].port=9091 -jar yourapp.jar server yourconfig.yml
Run Code Online (Sandbox Code Playgroud)

我似乎如果你通过系统属性配置端口,你还需要在yml中设置它们(无论如何,系统属性优先).至少在Dropwizard 0.7中发生了这种情况.YAML端口配置示例:

server:
  applicationConnectors:
  - type: http
    port: 8090
  adminConnectors:
  - type: http
    port: 8091
Run Code Online (Sandbox Code Playgroud)

如果你不把这些端口放在YAML中,Dropwizard会抱怨:

Exception in thread "main" java.lang.IllegalArgumentException: Unable to override server.applicationConnectors[0].port; node with index not found.
Run Code Online (Sandbox Code Playgroud)


Nat*_*tan 13

这就是我为我的测试应用程序所做的(0.7.x,0.8.x,0.9.x):

public class TestConfiguration extends Configuration {

  public TestConfiguration() {
    super();
    // The following is to make sure it runs with a random port. parallel tests clash otherwise
    ((HttpConnectorFactory) ((DefaultServerFactory) getServerFactory()).getApplicationConnectors().get(0)).setPort(0);
    // this is for admin port
    ((HttpConnectorFactory) ((DefaultServerFactory) getServerFactory()).getAdminConnectors().get(0)).setPort(0);   } }
Run Code Online (Sandbox Code Playgroud)

0给出了一个可用的随机端口.

我知道它不漂亮,但无法以编程方式找到更好的方法.我需要确保端口不会在不同的集成测试之间发生冲突,因为它们并行运行.我相信,为每次测试随机创建一个yml文件会更加丑陋.

哦,这就是你以后如何获得运行端口:

@Override
  public void run(TestConfiguration configuration, Environment environment) throws Exception {
    this.environment = environment;
    // do other stuff if you need to
  }

  public int getPort() {
    return ((AbstractNetworkConnector) environment.getApplicationContext().getServer().getConnectors()[0]).getLocalPort();
  }
Run Code Online (Sandbox Code Playgroud)


Mar*_*her 6

我之前从未使用过dropwizard,只使用jersey创建简单的服务.我决定看用户手册,并立即找到设置说明.

Dropwizard配置手册

您可以通过在启动服务时传递特殊的Java系统属性来覆盖配置设置.覆盖必须以前缀dw.开头,然后是覆盖配置值的路径.例如,要覆盖要使用的HTTP端口,您可以像这样启动服务:

java -Ddw.http.port=9090 server my-config.json
Run Code Online (Sandbox Code Playgroud)

它适合你吗?