更改Dropwizard默认端口

我有一个基于Dropwizard的泽西REST服务在默认端口8080(服务)和8081(pipe理员)上运行,我需要将默认端口更改为不太常用的东西,我无法find任何信息这样做,有人能指点我吗?

您可以更新yamlconfiguration文件中的端口:

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 

从命令行,你可以这样设置,在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 

我看来,如果你通过系统属性configuration端口,你也需要在yml中设置它们(系统属性优先,无论如何)。 至less我在Dropwizard 0.7中发生了这种情况。 YAML端口configuration示例:

 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. 

这是我为我的testing应用程序(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给出一个可用的随机端口。

我知道这不是很好,但找不到更好的方式来编程。 我需要确保端口不会在不同的集成testing之间冲突,因为它们并行运行。 为每个testing随机创build一个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,只使用jersey创build简单的服务。 我决定看看用户手册,并立即find设置说明。

Dropwizardconfiguration手册

您可以在启动服务时通过传递特殊的Java系统属性来覆盖configuration设置。 覆盖必须以前缀dw。开始,后面是覆盖configuration值的path。 例如,要覆盖要使用的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版本

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

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