Dropwizard - resources on multiple ports

I have a Dropstizard service (v 0.7.1) based on Jersey REST. I am currently using one application connector port (8810) and I have two resources (for example, "/ path1", "/ path2").

I will be able to access these resources as http: \\ localhost: 8810 \ path1 and http: \\ localhost: 8810 \ path2 respectively. What you are trying to achieve is a separate port for each resource. (e.g. http: \\ localhost: 8810 \ path1 and http: \\ localhost: 8820 \ path2). I changed the yaml file to have the following configuration, and when I started the application, both resources were available using both ports, and I'm not sure how to configure these resources to use specific ports or is it even possible with Dropwizard?

server: applicationConnectors: - type: http port: 8810 - type: http port: 8820 

Appreciate if someone can enlighten.

thanks

+7
java jersey dropwizard
source share
1 answer

your problem is that DefaultServerFactory adds all applicationConntectors to the same handler, see DefaultServerFactory # build:

 @Override public Server build(Environment environment) { printBanner(environment.getName()); final ThreadPool threadPool = createThreadPool(environment.metrics()); final Server server = buildServer(environment.lifecycle(), threadPool); LOGGER.info("Registering jersey handler with root path prefix: {}", applicationContextPath); environment.getApplicationContext().setContextPath(applicationContextPath); final Handler applicationHandler = createAppServlet(server, environment.jersey(), environment.getObjectMapper(), environment.getValidator(), environment.getApplicationContext(), environment.getJerseyServletContainer(), environment.metrics()); LOGGER.info("Registering admin handler with root path prefix: {}", adminContextPath); environment.getAdminContext().setContextPath(adminContextPath); final Handler adminHandler = createAdminServlet(server, environment.getAdminContext(), environment.metrics(), environment.healthChecks()); final RoutingHandler routingHandler = buildRoutingHandler(environment.metrics(), server, applicationHandler, adminHandler); server.setHandler(addStatsHandler(addRequestLog(server, routingHandler, environment.getName()))); return server; } 

What you need to do is implement your own ServerFactory.

You can extend the DefaultServerFactory and overwrite the build method to customize your connectors the way you want. Presumably, you will want to add some more configuration that indicates what is happening, since from the point of view of your yaml it will be impossible to map the resource to a specific connector. How to know about him.

To overwrite the dropwizard behavior (adding a new ServerFactory), you can see this message I wrote about adding a log: Dropwizard does not log user logs in a file

This is mainly due to the implementation of the class and the possibility of its discovery for the dropwizard. After that, all you have to do is modify the yaml file to point to the correct ServerFactory.

If you don't like this approach, you can overwrite the get / set method in the configuration to return your class. To do this, your class MUST extend DefaultServerFactory, because otherwise yaml matching will no longer work. You can overwrite the build method, despite this.

Update:

Looking at it in more detail, you will encounter a second problem:

There is only one jersey environment in your environment that it can use. You will need to configure the second knitwear environment, since by default each handler will receive the same config that passed it (only the one that exists). That's why it will be available for all of your http configurations. So in short:

  • Create a new environment that supports multiple knitwear configurations.
  • Create a factory server that knows which jersey configuration belongs to that handler and instantiates handlers in this form.

I believe that these two steps will be required.

From an environmental point of view, you will have to create your own ServerCommand (this is the command that starts the dropwizard server). When viewed in EnvironmentCommand # run, you can see where the environment is created. This will be the only place where you can overwrite the default environment (as far as I know), what you need to do to support several jersey configurations.

To be honest with you, looking at this, I do not believe that this is what the dropwizard guys had in mind.

+2
source share

All Articles