Java 更改 Dropwizard 默认端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20028451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:27:28  来源:igfitidea点击:

Change Dropwizard default ports

javarestjerseydropwizard

提问by user1965449

I have a Dropwizard based Jersey REST service running on the default ports 8080(service) and 8081(admin), I need to change the default ports to something that is less commonly used, I am not able to find any information to do so , can someone please point me to do so ?

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

采纳答案by condit

You can update the ports in your yaml configuration file:

您可以更新 yaml 配置文件中的端口:

http:
  port: 9000
  adminPort: 9001

See http://www.dropwizard.io/0.9.2/docs/manual/configuration.html#httpfor more information.

有关更多信息,请参阅http://www.dropwizard.io/0.9.2/docs/manual/configuration.html#http

EDIT

编辑

If you've migrated to Dropwizard 0.7.x, 0.8.x, 0.9.x you can use the following:

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

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

回答by MartenCatcher

I never work with dropwizard before, only creating simple services using jersey. I decided to see the user's manual, and immediately found a description of the settings.

我以前从未使用过 dropwizard,只使用 jersey 创建简单的服务。我决定查看用户手册,并立即找到了设置说明。

Dropwizard configuration manual

Dropwizard 配置手册

You can override configuration settings by passing special Java system properties when starting your service. Overrides must start with prefix dw., followed by the path to the configuration value being overridden. For example, to override the HTTP port to use, you could start your service like this:

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

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

Is it suitable for you?

它适合你吗?

回答by Ferran Maylinch

From the command line, you can set them this way, in Dropwizard 0.6:

在命令行中,您可以在 Dropwizard 0.6 中以这种方式设置它们:

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

If you use Dropwizard 0.7, the system properties are set this way:

如果您使用 Dropwizard 0.7,系统属性设置如下:

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

I seems that, if you configure ports through system properties, you also need to set them in the yml (the system property takes precedence, anyway). At least that's happening to me in Dropwizard 0.7. Example of the YAML port configuration:

我认为,如果您通过系统属性配置端口,则还需要在 yml 中设置它们(无论如何,系统属性优先)。至少在 Dropwizard 0.7 中发生在我身上。YAML 端口配置示例:

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

If you don't put those ports in the YAML, Dropwizard complains:

如果你不把这些端口放在 YAML 中,Dropwizard 会抱怨:

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

回答by Natan

This is what I've done for my test applications (0.7.x, 0.8.x, 0.9.x):

这是我为我的测试应用程序(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 gives a random port that is available.

0 给出一个可用的随机端口。

I know it's not pretty but couldn't find a better way to do it programmatically. I needed to make sure ports don't clash between different integration tests, because they run in parallel. Creating a yml file randomly for each test would have been uglier I believe.

我知道它不漂亮,但找不到更好的编程方式。我需要确保端口不会在不同的集成测试之间发生冲突,因为它们是并行运行的。我相信为每个测试随机创建一个 yml 文件会更丑陋。

Oh and this is how you get the running port later on:

哦,这就是您稍后获取正在运行的端口的方式:

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

回答by Jay Khatwani

For Dropwizard 0.8.0 --

对于 Dropwizard 0.8.0 --

Your YAML file can be -

您的 YAML 文件可以是 -

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

If you want to change the ports from command-line,

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

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

The command will work only if you have the entry in the YAML file. DW needs a default value which it can override .

只有当您在 YAML 文件中有条目时,该命令才会起作用。DW 需要一个可以覆盖的默认值。

回答by Prasad Revanaki

For Dropwizard 0.6.2 you can change the port programmatically as below in your service class.

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

}

回答by Vikash Tiwari

If you want it to be changed at run-time use

如果您希望在运行时更改它,请使用

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

I have used it with version 1.0.5

我在 1.0.5 版中使用过它

回答by Thomas Sundberg

I needed to set the ports but I couldn't set them from the command line. I ended up with this solution:

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

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

This is done using Dropwizard 1.3.0-rc7

这是使用 Dropwizard 完成的 1.3.0-rc7

回答by Mehul Gupta

In your .yml file make these changes

在您的 .yml 文件中进行这些更改

server:
  registerDefaultExceptionMappers: false
  applicationConnectors:
    - type: http
      port: 5020
  adminConnectors:
    - type: http
      port: 5022