Change Dropwizard default ports
Asked Answered
S

11

49

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 ?

Shiny answered 17/11, 2013 at 7:43 Comment(0)
O
100

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
Octavla answered 17/11, 2013 at 12:52 Comment(7)
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?Shiny
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...Octavla
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 ?Shiny
Add your configuration to the command line after server. See dropwizard.codahale.com/getting-started/#running-your-service for more information. It should have the desired effect.Octavla
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!Shiny
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 :Atheism
java -jar <yourJar.jar> server <.yml path>Dentation
B
34

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.
Bouchier answered 16/8, 2014 at 14:0 Comment(0)
Z
14

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();
  }
Zared answered 19/9, 2014 at 15:18 Comment(2)
Works also with 0.8.xTilsit
Yes, as well as 0.9.x actually. I'll update the answer.Zared
A
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?

Azoic answered 17/11, 2013 at 7:57 Comment(0)
M
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

Mishandle answered 17/2, 2017 at 10:50 Comment(0)
D
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 .

Diu answered 26/8, 2015 at 4:27 Comment(0)
C
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);
}

}
Commit answered 14/10, 2016 at 15:10 Comment(0)
F
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

Feuchtwanger answered 7/3, 2018 at 10:42 Comment(0)
S
1

In the newer version of dropwizard (e.g. 2.0.25): create a property file config.yml with the following content into your resources directory:

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

If using Intellij IDE (ver 2021), remember to add the following to Run Configuration Program argument:

server src/main/resources/config.yml

enter image description here

Semiaquatic answered 14/11, 2021 at 21:5 Comment(0)
T
1

this is where you can all configuration require to solve your problem https://www.dropwizard.io/en/latest/manual/configuration.html

Turbinal answered 1/8, 2022 at 18:50 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewStyrax
J
0

In your .yml file make these changes

server:
  registerDefaultExceptionMappers: false
  applicationConnectors:
    - type: http
      port: 5020
  adminConnectors:
    - type: http
      port: 5022
Jabon answered 10/5, 2020 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.