设为首页 收藏本站
查看: 857|回复: 0

[经验分享] Jetty/Feature/SPDY

[复制链接]

尚未签到

发表于 2017-2-25 13:18:11 | 显示全部楼层 |阅读模式
  转载地址:http://wiki.eclipse.org/Jetty/Feature/SPDY
Introduction
  Jetty supports both a client and a server implementation for the SPDY protocol, beginning with versions 7.6.2 and 8.1.2. To provide the best support possible for SPDY, the Jetty project also provides animplementation for NPN. Both the SPDY and the NPN implementation require OpenJDK 1.7 or greater.
  A server deployed over TLS normally advertises the SPDY protocol, via the Next Protocol Negotiation TLS Extension (NPN).
Feature
Introducing SPDY Modules
  Jetty's SPDY implementation consists of four modules:

  • spdy-core – contains the SPDY API and a partial implementation. This module is independent of Jetty (the servlet container), and you can reuse it with other Java SPDY implementations. One of the goals of this module is to standardize the SPDYJava API.
  • spdy-jetty module – binds the spdy-core module to Jetty's NIO framework to provide asynchronous socket I/O. This module uses Jetty internals, but Java SPDY client applications can also use it to communicate with a SPDY server.
  • spdy-jetty-http module – provides a server-side layering of HTTP over SPDY. This module allows any SPDY compliant browser, such as Chromium/Chrome, to talk SPDY to a Jetty server that deploys a standard web application made of servlets, filtersand JSPs. This module performs the conversion SPDY to HTTP and vice versa so that for the web application it is as if a normal HTTP request has arrived, and a normal HTTP response is returned.
  • spdy-jetty-http-webapp module – provides a demo application for the aspdy-jetty-http module.
Transparently Enabling HTTP over SPDY in Jetty
  The spdy-jetty-http module provides an out-of-the-box server connector that performs the SPDY to HTTP conversion and vice versa (HTTP over SPDY).You can use this connector instead of Jetty'sSslSelectChannelConnector (that only speaks HTTP), and it falls back to HTTPS if SPDY is not negotiated.
  An example jetty-spdy.xml file that you can use instead of jetty-ssl.xml follows:
<?xml version="1.0" encoding="UTF-8"?><br>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <br>
<Configure id="Server" class="org.eclipse.jetty.server.Server"><br>
<New id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory"><br>
<Set name="keyStorePath">your_keystore.jks</Set><br>
<Set name="keyStorePassword">storepwd</Set><br>
<Set name="protocol">TLSv1</Set><br>
</New><br>
<br>
<Call name="addConnector"><br>
<Arg><br>
<New class="org.eclipse.jetty.spdy.http.HTTPSPDYServerConnector"><br>
<Arg><br>
<Ref id="sslContextFactory" /><br>
</Arg><br>
<Set name="Port">8443</Set><br>
</New><br>
</Arg><br>
</Call><br>
</Configure>

  This is sufficient to enable your Jetty server to speak SPDY to browsers that support it.
  Remember, however, that SPDY over SSL (as set up like the configuration above) requires that you set up NPN correctly; in particular, you need to start the JVM with the NPN boot jar in the boot classpath.
  Follow the instructions at the NPN documentation page.
Using SPDY Client Applications
  The spdy-jetty module provides a SPDY client (that speaks pure SPDY, and not HTTP over SPDY), which you can use in this way:
// Start a SPDYClient factory shared among all SPDYClient instances
SPDYClient.Factory clientFactory = new SPDYClient.Factory();
clientFactory.start();

// Create one SPDYClient instance
SPDYClient client = clientFactory.newSPDYClient(SPDY.V2);

// Obtain a Session instance to send data to the server that listens on port 8181

Session session = client.connect(new InetSocketAddress("localhost", 8181),null).get(5, TimeUnit.SECONDS);

// Sends SYN_STREAM and DATA to the server
Stream stream = session.syn(new SynInfo(false),null).get(5, TimeUnit.SECONDS);
stream.data(new StringDataInfo("Hello, World",true));

  To listen to SPDY frames the server sends, you need to pass a StreamFrameListener upon stream creation:
StreamFrameListener streamListener = new StreamFrameListener.Adapter()
{
public void onReply(Stream stream, ReplyInfo replyInfo)
{
// Reply received from server, send DATA to the server
stream.data(new StringDataInfo("Hello, World", true));
}

public void onData(Stream stream, DataInfo dataInfo)
{
// Data received from server
String content = dataInfo.asString("UTF-8", true);
System.err.printf("SPDY content: %s%n", content);
}
};

// Sends SYN_STREAM to the server, adding headers
Headers headers = new Headers();
headers.put("url", "/echo");
Stream stream = session.syn(new SynInfo(headers, false), null).get(5, TimeUnit.SECONDS);

Using SPDY Server Applications
  The spdy-jetty module provides a server connector that speaks pure SPDY, and that you can use in conjunction with the SPDY client (see above).
  You can start the SPDYServerConnector in this way:
// The application code that handles incoming SYN_STREAMS
ServerSessionFrameListener application = new ServerSessionFrameListener.Adapter()
{
public StreamFrameListener onSyn(Stream stream, SynInfo synInfo)
{
// Reply upon receiving a SYN_STREAM
stream.reply(new ReplyInfo(false));

// Inspect the headers
Headers headers = synInfo.getHeaders();
if ("/echo".equals(headers.get("url").value()))
{
return new StreamFrameListener.Adapter()
{
public void onData(Stream stream, DataInfo dataInfo)
{
// Upon receiving hello data from the client, echo it back
String clientData = dataInfo.asString("UTF-8", true);
stream.data(new StringDataInfo("Echo for " + clientData, true));
}
};
}
return null;
}
};

// Wire up and start the connector
org.eclipse.jetty.server.Server server = new Server();
server.addConnector(new SPDYServerConnector(application));
server.start();

  The ServerSessionFrameListener can inspect the incoming SynInfo and provide different execution paths depending on headers and/or data content.
  Become familiar with the SPDY API to understand how to use it.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-347111-1-1.html 上篇帖子: Web容器Jetty 下篇帖子: Jetty配置JAAS(jetty-maven-plugin)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表