【发布时间】:2013-11-30 11:05:08
【问题描述】:
我有一个基于 Dropwizard 的 Jersey REST 服务在默认端口 8080(服务)和 8081(管理员)上运行,我需要将默认端口更改为不常用的端口,我无法找到任何信息这样做,有人可以指点我这样做吗?
【问题讨论】:
标签: java rest jersey dropwizard
我有一个基于 Dropwizard 的 Jersey REST 服务在默认端口 8080(服务)和 8081(管理员)上运行,我需要将默认端口更改为不常用的端口,我无法找到任何信息这样做,有人可以指点我这样做吗?
【问题讨论】:
标签: java rest jersey dropwizard
在您的 .yml 文件中进行这些更改
server:
registerDefaultExceptionMappers: false
applicationConnectors:
- type: http
port: 5020
adminConnectors:
- type: http
port: 5022
【讨论】:
我需要设置端口,但我无法从命令行设置它们。我最终得到了这个解决方案:
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 完成的
【讨论】:
如果您希望在运行时更改它,请使用
-Ddw.server.applicationConnectors[0].port=9090 -Ddw.server.adminConnectors[0].port=9091
我用的是1.0.5版
【讨论】:
对于 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);
}
}
【讨论】:
您可以在 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
【讨论】:
server之后的命令行。有关更多信息,请参阅dropwizard.codahale.com/getting-started/#running-your-service。它应该具有预期的效果。
这是我为我的测试应用程序(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 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,只使用 jersey 创建简单的服务。我决定查看用户手册,并立即找到了设置说明。
Dropwizard configuration manual
您可以在启动服务时通过传递特殊的 Java 系统属性来覆盖配置设置。覆盖必须以前缀 dw. 开头,后跟要覆盖的配置值的路径。 例如,要覆盖要使用的 HTTP 端口,您可以这样启动服务:
java -Ddw.http.port=9090 server my-config.json
适合你吗?
【讨论】:
在命令行中,您可以在 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.
【讨论】: