Java Dropwizard-多个端口上的资源
我有一个基于Dropwizard(V0.7.1)的Jersey REST服务。目前我使用一个应用程序连接器端口(8810),我有两个资源(比如“/path1”、““/path2”) 我将能够分别以http:\\localhost:8810\path1和http:\\localhost:8810\path2访问这些资源。我试图实现的是为每个资源提供一个单独的端口。(例如http:\\localhost:8810\path1和http:\\localhost:8820\path2)。我调整了yaml文件,使其具有以下配置,当我启动应用程序时,两个资源都可以使用这两个端口,并且不确定如何配置这些资源以使用特定端口,或者使用Dropwizard也可以这样做Java Dropwizard-多个端口上的资源,java,jersey,dropwizard,Java,Jersey,Dropwizard,我有一个基于Dropwizard(V0.7.1)的Jersey REST服务。目前我使用一个应用程序连接器端口(8810),我有两个资源(比如“/path1”、““/path2”) 我将能够分别以http:\\localhost:8810\path1和http:\\localhost:8810\path2访问这些资源。我试图实现的是为每个资源提供一个单独的端口。(例如http:\\localhost:8810\path1和http:\\localhost:8820\path2)。我调整了yaml
server:
applicationConnectors:
-
type: http
port: 8810
-
type: http
port: 8820
如果有人能启发你,你会感激的
谢谢您的问题是DefaultServerFactory将所有ApplicationConnector添加到同一个处理程序中,请参阅DefaultServerFactory#build: 您需要做的是实现自己的ServerFactory 您可以扩展DefaultServerFactory并覆盖构建方法,以按照您希望的方式设置连接器。 大概您会想添加更多的配置,以指示要使用的内容,因为就yaml而言,不可能将资源映射到特定的连接器。dropwizard怎么会知道呢 要覆盖dropwizard的行为(添加新的ServerFactory),您可以看到我写的关于添加日志记录的帖子: 它主要涉及实现该类并使其可供dropwizard发现。之后,您需要做的就是更改yaml文件以指向正确的ServerFactory 如果不喜欢这种方法,可以覆盖配置上的get/set方法以返回类。为此,您的类必须扩展DefaultServerFactory,否则yaml映射将不再工作。尽管如此,您仍然可以覆盖生成方法 更新: 更详细地看一下,您将遇到第二个问题: 您的环境只有一个可以使用的jersey环境。 您将需要配置第二个jersey环境,因为当前默认情况下,每个处理程序将获得传递给它的相同jersey配置(唯一存在的一个)。这就是为什么它将可用于所有http配置的原因。总而言之:
老实说,看看这个,我不相信这就是dropwizard的人的想法 我很好奇——在不同的端口上运行每个资源背后的原因是什么?我有同样的需求。问题是安全。有些服务应该对外公开,有些只在子网上公开。如果您将所有私有服务放在一个单独的端口上,然后在防火墙中阻止对该端口的访问,那么您就可以了。
@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;
}