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

有帮助吗?

解决方案

您可以在YAML配置文件中更新端口:

http:
  port: 9000
  adminPort: 9001
.

请参阅 http://www.dropwizard.io/0.9.2/docs/手动/配置.html #tpth 更多信息。

编辑

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

server:
  applicationConnectors:
  - type: http 
    port: 9000
  adminConnectors:
  - type: http
    port: 9001
.

其他提示

从命令行中,您可以通过这种方式设置它们,在DropWizard 0.6:

java -Ddw.http.port=9090 -Ddw.http.adminPort=9091 -jar yourapp.jar server yourconfig.yml
.

如果使用dropWizard 0.7,系统属性可按方式设置:

java -Ddw.server.applicationConnectors[0].port=9090 -Ddw.server.adminConnectors[0].port=9091 -jar yourapp.jar server yourconfig.yml
.

我似乎,如果通过系统属性配置端口,还需要在yml中设置它们(无论如何,系统属性优先)。至少这是在滴注暗暗暗束中发生的。yaml端口配置示例:

server:
  applicationConnectors:
  - type: http
    port: 8090
  adminConnectors:
  - type: http
    port: 8091
.

如果您没有将这些端口放在yaml中,optwizard抱怨:

Exception in thread "main" java.lang.IllegalArgumentException: Unable to override server.applicationConnectors[0].port; node with index not found.
.

这是我为我的测试应用程序(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);   } }
.

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();
  }
.

我以前从未使用滴注,只使用泽西岛创建简单的服务。我决定看到用户的手册,并立即找到了设置的描述。

dropwizard配置手册

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

java -Ddw.http.port=9090 server my-config.json
.

是适合你的吗?

如果您希望在运行时间使用时更改

-Ddw.server.applicationConnectors[0].port=9090  -Ddw.server.adminConnectors[0].port=9091
.

我用它使用了1.0.5版

for forwizard 0.6.2您可以在服务类中以下面以下面的方式以编程方式更改端口。

import com.yammer.dropwizard.config.Configuration;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.config.HttpConfiguration;
import com.yammer.dropwizard.Service;

public class BlogService extends Service<Configuration> {

public static void main(String[] args) throws Exception {
    new BlogService().run(new String[] {"server"});
}

@Override
public void initialize(Bootstrap<Configuration> bootsrap) {
    bootsrap.setName("blog");
}    


public void run(Configuration configuration, Environment environment) throws Exception {

    HttpConfiguration config = new HttpConfiguration();
    config.setPort(8085);
    config.setAdminPort(8086);
    configuration.setHttpConfiguration(config);
}

}
.

我需要设置端口,但无法从命令行设置它们。我最终得到了这个解决方案:

public static void main(String[] args) throws Exception {
    String applicationPort = "9090";
    String adminPort = "9091";

    System.setProperty("dw.server.applicationConnectors[0].port", applicationPort);
    System.setProperty("dw.server.adminConnectors[0].port", adminPort);

    new Main().run(args);
}

这是使用 Dropwizard 完成的 1.3.0-rc7

for dropWizard 0.8.0 -

您的yaml文件可以 -

server:
    type: simple
    connector:
      type: http
      port: 80
.

如果要从命令行更改端口,

java -Ddw.server.connector.port=9090 -jar yourapp.jar server yourconfig.yml
.

命令只有在yaml文件中有条目时才工作。DW需要一个可以覆盖的默认值。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top