Question

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 ?

Was it helpful?

Solution

You can update the ports in your yaml configuration file:

http:
  port: 9000
  adminPort: 9001

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

EDIT

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

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

OTHER TIPS

From the command line, you can set them this way, in 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:

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:

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

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

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

This is what I've done for my test applications (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.

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.

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

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 configuration manual

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 -Ddw.http.port=9090 server my-config.json

Is it suitable for you?

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

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

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

}

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

For Dropwizard 0.8.0 --

Your YAML file can be -

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 .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top