更改Dropwizard默认端口

49

我有一个基于Dropwizard的Jersey REST服务,运行在默认端口8080(服务)和8081(管理员)上,我需要将默认端口更改为不常用的端口,但我找不到任何相关信息,有人可以指点一下吗?

11个回答

99

你可以在yaml配置文件中更新端口:

http:
  port: 9000
  adminPort: 9001

更多信息请参见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

谢谢,但是仅在我的项目的.yml文件中设置这些端口并不能更改默认端口,它仍然运行在8080端口。是否有一个默认的yaml文件与我为我的服务准备的文件不同,我需要使用它来输入这些新值? - user1965449
奇怪 - 对我来说它是有效的。你是如何指定你的配置文件的?应该在启动服务时放在Java命令行上... - condit
我在eclipse的项目文件夹下直接有一个.yml文件,使用“java -jar myservice.jar server”命令运行服务,我没有在命令行中指定它,这里是否有两个配置文件?一个用于服务,一个用于配置?我正在遵循入门教程。这个配置文件是否与教程中提到的Hello World示例的.yml文件相同? - user1965449
4
在 "server" 之后将您的配置添加到命令行中。有关更多信息,请参见http://dropwizard.codahale.com/getting-started/#running-your-service。它应该产生期望的效果。 - condit
非常完美!非常感谢,我真的很感激。我原本以为配置文件会自动被识别,现在在命令行中指定它就像魔法一样顺利运行了! - user1965449
显示剩余2条评论

33

从命令行中,您可以使用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中设置它们(无论如何,系统属性具有优先权)。 至少在Dropwizard 0.7中发生了这种情况。以下是YAML端口配置的示例:

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

如果您没有在YAML中指定这些端口,Dropwizard会抱怨:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to override server.applicationConnectors[0].port; node with index not found.

14

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

也适用于0.8.x版本。 - MAGx2
是的,还有0.9.x版本。我会更新答案。 - Natan

6
我以前从未使用过dropwizard,只是使用jersey创建简单的服务。我决定查看用户手册,立即找到了设置的描述。 Dropwizard配置手册 引用: 您可以通过在启动服务时传递特殊的Java系统属性来覆盖配置设置。覆盖必须以前缀dw.开头,后跟要覆盖的配置值的路径。例如,要覆盖要使用的HTTP端口,您可以像这样启动服务:
java -Ddw.http.port=9090 server my-config.json

这适合你吗?


4
如果您希望在运行时进行更改,请使用以下方法:
-Ddw.server.applicationConnectors[0].port=9090  -Ddw.server.adminConnectors[0].port=9091

我使用的是1.0.5版本。

1

我需要设置端口,但是无法通过命令行进行设置。最终我找到了以下解决方案:

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完成的。


1
对于 Dropwizard 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);
}

}

1
对于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需要一个默认值,以便可以覆盖它。


1

虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。-【来自审查】 - AddeusExMachina

1
在较新版本的Dropwizard(例如2.0.25)中: 创建一个名为config.yml的属性文件,并将以下内容放入您的资源目录中:
server:
   applicationConnectors:
    - type: http
      port: 5020
   adminConnectors:
    - type: http
      port: 5022

如果使用Intellij IDE(版本为2021),请记得在运行配置中添加以下内容作为程序参数:
server src/main/resources/config.yml

enter image description here


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