48

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 ?

Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
user1965449
  • 2,641
  • 5
  • 30
  • 47

9 Answers9

97

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
Gili
  • 76,473
  • 85
  • 341
  • 624
condit
  • 10,211
  • 2
  • 38
  • 60
  • Thanks , but just setting these ports in the .yml file in my project is not changing the default ports , it still runs on 8080. Is there a default yaml file,different than the one that I have for my service that I need to use in order to put in these new values? – user1965449 Nov 19 '13 at 04:43
  • Strange - it works for me. How are you specifying your configuration file? Should be on the Java command line when you're launching your service... – condit Nov 19 '13 at 16:45
  • I have a .yml file directly under the project folder in eclipse and run the service using a fat jar "java -jar myservice.jar server",I dont specify it using the command line , are there two config files here ? one for the service and one for the configuration ? I am following the tutorial for getting started. Is this configuration file the same as the .yml file that the tutorial mentions for the Hello World example ? – user1965449 Nov 19 '13 at 19:04
  • 4
    Add your configuration to the command line after `server`. See http://dropwizard.codahale.com/getting-started/#running-your-service for more information. It should have the desired effect. – condit Nov 19 '13 at 20:46
  • Perfect! Thanks a lot , I really appreciate that , I was under the impression that the config file would be automatically picked up . Now that I specify it in the command line it works like a charm! – user1965449 Nov 20 '13 at 05:09
  • One minor comment - I couldn't edit the answer because it was just 1 character - but needs to be http: not http - because you get parsing error without : – mikemil Feb 05 '14 at 21:17
  • java -jar server <.yml path=""> – Somnath Musib Jun 22 '19 at 09:06
33

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.
Ferran Maylinch
  • 9,556
  • 13
  • 70
  • 89
13

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();
  }
Natan
  • 2,620
  • 14
  • 29
6

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?

thSoft
  • 19,314
  • 5
  • 82
  • 97
MartenCatcher
  • 2,209
  • 7
  • 22
  • 31
4

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

Vikash Tiwari
  • 103
  • 1
  • 10
1

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 .

Jay Khatwani
  • 329
  • 3
  • 6
1

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

}
Prasad Revanaki
  • 693
  • 4
  • 10
  • 21
1

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

Thomas Sundberg
  • 3,596
  • 1
  • 13
  • 21
0

In your .yml file make these changes

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