2017-07-18 52 views
2

當我向嵌入式Jetty服務器添加兩個連接器時,我既不能使用HTTP也不能使用HTTPS - 瀏覽器/ curl被卡住了。該代碼我用它來創建嵌入式碼頭約以下(它是基於這個例子 - http://self-learning-java-tutorial.blogspot.de/2015/10/jetty-configuring-many-connectors.html):在Jetty v.9.4.3中不能使用兩個連接器(http和https)

HttpConfiguration httpConfiguration = new HttpConfiguration(); 
httpConfiguration.setRequestHeaderSize(requestHeaderSize); 

ServerConnector httpConnector= new ServerConnector(server, 1, -1, new 
    HttpConnectionFactory(httpConfiguration)); 
httpConnector.setPort(getPort()); 
httpConnector.setReuseAddress(true); 
httpConnector.setIdleTimeout(maxTimeout); 
server.addConnector(httpConnector); 

HttpConfiguration httpsConfiguration = new HttpConfiguration(); 
httpsConfiguration.setSecureScheme("https"); 
httpsConfiguration.setSecurePort(securePort); 
httpsConfiguration.addCustomizer(new SecureRequestCustomizer()); 

ServerConnector sslConnector = new ServerConnector(server, 
      new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), 
      new HttpConnectionFactory(httpsConfiguration)); 
sslConnector.setPort(securePort); 
sslConnector.setIdleTimeout(maxTimeout); 
sslConnector.setReuseAddress(true); 

server.addConnector(sslConnector); 

ServletContextHandler servContext = new 
ServletContextHandler(ServletContextHandler.NO_SESSIONS); 
servContext.setContextPath("/"); 
server.setHandler(servContext); 
server.start(); 

我打開調試日誌裏面org.eclipse.jetty和任何要求,我得到以下幾點:

Selector loop woken up from select, 0/1 selected [] [io.ManagedSelector][jetty-default-3] 
Running action [email protected] [][io.ManagedSelector] [jetty-default-3] 
Queued change [email protected] on [email protected] id=3 keys=2 selected=0 [] [io.ManagedSelector] [jetty-default-3] 
[email protected]/[email protected]/PRODUCING/0/1->PRODUCING/0/1 PEC [email protected] [] [strategy.EatWhatYouKill] [jetty-default-3] 
Selector loop waiting on select [] [io.ManagedSelector] [jetty-default-3] 

當只添加一個連接器時,所有功能都按預期工作。

P.S. SO問題"Selector loop waiting on select" when running multiple test cases which use wiremock stubsJetty+Jersey infinite loop with curl post query除了它是在9.3(我使用9.4.3)中修復的碼頭bug之外沒有給出任何答案

+0

你在做什麼,你共享的鏈接在做什麼是不同的。該鏈接正確使用2個嵌套的HttpConfiguration對象,而不是。 –

+0

我更改了代碼並使用了嵌套對象,但結果相同 –

+0

事實上,我最終啓動了兩個Jetty服務器 - 一個使用HTTP,一個使用HTTPS。 HTTP工作正常,而後者沒有工作進入無限循環的ManagedSelector,我相信。 –

回答

3

嵌入式Jetty在1臺服務器上支持儘可能多的連接器,這是您夢寐以求的。 Jetty沒有技術限制(存在的唯一限制是在您的環境中的操作系統和網絡堆棧中)

重要的是要注意,您必須擁有一個健全的HttpConfiguration安裝程序。 因爲他們可以參考彼此的連接器。 (這是爲了「安全」的行爲,安全限制等)

儘管可能有多個簡單的連接器沒有意識到彼此,但這不是一般用例。

當使用HTTPS(也稱爲HTTP over TLS/SSL)時,證書(大小,類型,算法等)的選擇以及密碼套件選擇將影響您連接到該HTTPS連接器的能力。

請注意,HTTPS是TLS(不是SSL),Jetty可以使用ALPN擴展到TLS,允許客戶端協商實際使用的下一個協議(無論是HTTP/1.x或HTTP/2還是其他任何配置的下一個協議列表是)

下面是嵌入式Jetty中多個連接器的幾個示例。

eclipse/jetty.project - embedded/ManyConnectors.java

eclipse/jetty.project - embedded/LikeJettyXml.java

jetty-project/embedded-jetty-cookbook - ConnectorSpecificContexts.java

jetty-project/embedded-jetty-cookbook - ConnectorSpecificWebapps.java

jetty-project/embedded-jetty-cookbook - SecuredRedirectHandlerExample.java

jetty-project/embedded-jetty-cookbook - ServletTransportGuaranteeExample.java

+0

感謝您的鏈接,Joakim。 問題根本不在於HTTPS - 我無法在不同的端口上添加兩個HTTP連接器 - 它們都無法工作。 在我看到 選擇循環從選擇喚醒日誌,選擇0/0運行 行動org.eclipse.jetty.io.ManagedSelector $ @接受排隊 變化org.eclipse.jetty.io.ManagedSelector $ @ CreateEndPoint [email protected]/org.eclipse.jetty.io.ManagedSelector$SelectorProducer 選擇器循環等待選擇 –

+1

忽略與您的問題無關的選擇器循環消息,它是一個紅色鯡魚。 –

+1

示例'ConnectorSpecificContexts'和'ConnectorSpecificWebapps'添加2個不同的HTTP連接器。 –