dak*_*987 8 java jersey dropwizard
我有一个基于Jersey REST服务的Dropwizard(v 0.7.1).目前我使用一个应用程序连接器端口(8810),我有两个资源(比如"/ path1","/ path2").
我将能够分别以http:\\ localhost:8810\path1和http:\\ localhost:8810\path2访问这些资源.我们想要实现的是每个资源都有一个单独的端口.(例如http:\\ localhost:8810\path1和http:\\ localhost:8820\path2).我调整了yaml文件以具有以下配置,当我启动应用程序时,两个端口都可以使用这两个资源,并且我不确定如何配置这些资源以使用特定端口,或者甚至可以使用Dropwizard?
server:
applicationConnectors:
-
type: http
port: 8810
-
type: http
port: 8820
Run Code Online (Sandbox Code Playgroud)
感谢有人可以启发.
谢谢
您的问题是 DefaultServerFactory 将所有 applicationConntectors 添加到同一个处理程序,请参阅 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;
}
Run Code Online (Sandbox Code Playgroud)
你需要做的是实现你自己的ServerFactory。
您可以扩展 DefaultServerFactory 并覆盖构建方法,以按照您希望的方式设置连接器。想必您会想要添加一些更多的配置来指示什么去哪里,因为就您的 yaml 而言,不可能将资源映射到特定的连接器。dropwizard 是如何知道这一点的。
要覆盖 dropwizard 的行为(添加新的 ServerFactory),您可以查看我写的关于添加日志记录的这篇文章:Dropwizard does not log custom loggers to file
它基本上涉及实现该类并使其可供 dropwizard 发现。之后,您需要做的就是更改 yaml 文件以指向正确的 ServerFactory。
如果您不喜欢这种方法,您可以覆盖配置上的 get/set 方法以返回您的类。为此,您的类必须扩展 DefaultServerFactory,否则 yaml 映射将不再起作用。不过,您可以覆盖构建方法。
更新:
更详细地看一下,您会遇到第二个问题:
您的环境只有一种可以使用的球衣环境。您将需要配置第二个球衣环境,因为当前默认情况下每个处理程序都会获得传递给它的相同的球衣配置(唯一存在的)。这就是为什么它可用于您的所有 http 配置。总结来说:
我相信这两个步骤是必要的。
在环境方面,您必须创建自己的ServerCommand(即启动dropwizard服务器的命令)。查看EnvironmentCommand#run,您可以看到环境的创建位置。这将是您可以覆盖默认环境的唯一位置(据我所知),这是您需要执行的操作来支持多个球衣配置。
老实说,看到这个,我不相信这就是掉落巫师们的想法。