我有一个基于Dropwizard的Jersey REST服务,运行在默认端口8080(服务)和8081(管理员)上,我需要将默认端口更改为不常用的端口,但我找不到任何相关信息,有人可以指点一下吗?
我有一个基于Dropwizard的Jersey REST服务,运行在默认端口8080(服务)和8081(管理员)上,我需要将默认端口更改为不常用的端口,但我找不到任何相关信息,有人可以指点一下吗?
你可以在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
从命令行中,您可以使用Dropwizard 0.6设置它们:
java -Ddw.http.port=9090 -Ddw.http.adminPort=9091 -jar yourapp.jar server yourconfig.yml
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
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();
}
java -Ddw.http.port=9090 server my-config.json
这适合你吗?
-Ddw.server.applicationConnectors[0].port=9090 -Ddw.server.adminConnectors[0].port=9091
我需要设置端口,但是无法通过命令行进行设置。最终我找到了以下解决方案:
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
完成的。
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);
}
}
server:
type: simple
connector:
type: http
port: 80
如果您想通过命令行更改端口,java -Ddw.server.connector.port=9090 -jar yourapp.jar server yourconfig.yml
只有在YAML文件中有条目时,该命令才能起作用。DW需要一个默认值,以便可以覆盖它。