Grizzly文档指出
添加到 ServerConfiguration 的所有 HttpHandler 将在所有侦听器之间共享
是否有另一种方法可以将不同的处理程序绑定到不同的端口?还是我必须乘以实例化HttpServer
?
您可以将其绑定到一个PortRange
即多个端口
NetworkListener(String name, String host, PortRange portRange)
记录在案
或者
HttpServer httpServer = new HttpServer();
NetworkListener networkListener1 = new NetworkListener("sample-listener1", "localhost", 8888);
NetworkListener networkListener2 = new NetworkListener("sample-listener2", "localhost", 8889);
httpServer.addListener(networkListener1);
httpServer.addListener(networkListener2);
这样您就可以为不同的端口添加多个处理程序
或(尚未尝试但很可能有效)
HttpServer server = HttpServer.createSimpleServer();
server.getServerConfiguration().addHttpHandler(handler1);
server.getServerConfiguration().addHttpHandler(handler2);
server.start()