ballerina 支持http2 协议,包含server push

http2 协议

  • 参考代码
  1. import ballerina/http;
  2. import ballerina/log;endpoint http:Client http2serviceClientEP {
  3. url: "http://localhost:7090",
  4. httpVersion: "2.0"};@http:ServiceConfig {
  5. basePath: "/http11Service"
  6. }
  7. service<http:Service> http11Service bind { port: 9090 } { @http:ResourceConfig {
  8. path: "/"
  9. }
  10. http11Resource(endpoint caller, http:Request clientRequest) {
  11. var clientResponse =
  12. http2serviceClientEP->forward("/http2service", clientRequest); http:Response response = new;
  13. match clientResponse {
  14. http:Response resultantResponse => {
  15. response = resultantResponse;
  16. }
  17. error err => {
  18. response.statusCode = 500;
  19. response.setPayload(err.message); }
  20. }
  21. caller->respond(response) but {
  22. error e => log:printError(
  23. "Error occurred while sending the response",
  24. err = e) }; }
  25. }endpoint http:Listener http2serviceEP {
  26. port: 7090,
  27. httpVersion: "2.0"};@http:ServiceConfig {
  28. basePath: "/http2service"
  29. }
  30. service http2service bind http2serviceEP { @http:ResourceConfig {
  31. path: "/"
  32. }
  33. http2Resource(endpoint caller, http:Request clientRequest) {
  34. http:Response response = new;
  35. json msg = { "response": { "message": "response from http2 service" } };
  36. response.setPayload(msg);
  37. caller->respond(response) but {
  38. error e => log:printError(
  39. "Error occurred while sending the response",
  40. err = e) }; }
  41. }

server push

  • 参考代码
  1. import ballerina/http;
  2. import ballerina/log;
  3. endpoint http:Listener http2ServiceEP {
  4. port: 7090,
  5. httpVersion: "2.0"};@http:ServiceConfig {
  6. basePath: "/http2Service"
  7. }
  8. service http2Service bind http2ServiceEP { @http:ResourceConfig {
  9. path: "/"
  10. }
  11. http2Resource(endpoint caller, http:Request req) {
  12. http:PushPromise promise1 = new(path = "/resource1", method = "GET");
  13. caller->promise(promise1) but {
  14. error e => log:printError(
  15. "Error occurred while sending the promise1",
  16. err = e) };
  17. http:PushPromise promise2 = new(path = "/resource2", method = "GET");
  18. caller->promise(promise2) but {
  19. error e => log:printError(
  20. "Error occurred while sending the promise2",
  21. err = e) };
  22. http:PushPromise promise3 = new(path = "/resource3", method = "GET");
  23. caller->promise(promise3) but {
  24. error e => log:printError(
  25. "Error occurred while sending the promise3",
  26. err = e) };
  27. http:Response response = new;
  28. json msg = { "response": { "name": "main resource" } };
  29. response.setPayload(msg);
  30. caller->respond(response) but {
  31. error e => log:printError(
  32. "Error occurred while sending the response",
  33. err = e) };
  34. http:Response push1 = new;
  35. msg = { "push": { "name": "resource1" } };
  36. push1.setPayload(msg);
  37. caller->pushPromisedResponse(promise1, push1) but {
  38. error e => log:printError(
  39. "Error occurred while sending the promised response1",
  40. err = e) };
  41. http:Response push2 = new;
  42. msg = { "push": { "name": "resource2" } };
  43. push2.setPayload(msg);
  44. caller->pushPromisedResponse(promise2, push2) but {
  45. error e => log:printError(
  46. "Error occurred while sending the promised response2",
  47. err = e) };
  48. http:Response push3 = new;
  49. msg = { "push": { "name": "resource3" } };
  50. push3.setPayload(msg);
  51. caller->pushPromisedResponse(promise3, push3) but {
  52. error e => log:printError(
  53. "Error occurred while sending the promised response3",
  54. err = e) }; }
  55. }
  56. import ballerina/http;
  57. import ballerina/log;
  58. endpoint http:Client clientEP {
  59. url: "http://localhost:7090",
  60. httpVersion: "2.0"};function main(string... args) { http:Request serviceReq = new;
  61. http:HttpFuture httpFuture = new;
  62. var submissionResult = clientEP->submit("GET", "/http2Service", serviceReq); match submissionResult {
  63. http:HttpFuture resultantFuture => {
  64. httpFuture = resultantFuture;
  65. }
  66. error resultantErr => {
  67. log:printError("Error occurred while submitting a request",
  68. err = resultantErr);
  69. return;
  70. }
  71. }
  72. http:PushPromise[] promises = [];
  73. int promiseCount = 0;
  74. boolean hasPromise = clientEP->hasPromise(httpFuture); while (hasPromise) {
  75. http:PushPromise pushPromise = new;
  76. var nextPromiseResult = clientEP->getNextPromise(httpFuture); match nextPromiseResult {
  77. http:PushPromise resultantPushPromise => {
  78. pushPromise = resultantPushPromise;
  79. }
  80. error resultantErr => {
  81. log:printError("Error occurred while fetching a push promise",
  82. err = resultantErr);
  83. return;
  84. }
  85. }
  86. log:printInfo("Received a promise for " + pushPromise.path); if (pushPromise.path == "/resource2") {
  87. clientEP->rejectPromise(pushPromise); log:printInfo("Push promise for resource2 rejected");
  88. } else {
  89. promises[promiseCount] = pushPromise; promiseCount = promiseCount + 1;
  90. }
  91. hasPromise = clientEP->hasPromise(httpFuture);
  92. } http:Response response = new;
  93. var result = clientEP->getResponse(httpFuture); match result {
  94. http:Response resultantResponse => {
  95. response = resultantResponse;
  96. }
  97. error resultantErr => {
  98. log:printError("Error occurred while fetching response",
  99. err = resultantErr);
  100. return;
  101. }
  102. } var responsePayload = response.getJsonPayload();
  103. match responsePayload {
  104. json resultantJsonPayload =>
  105. log:printInfo("Response : " + resultantJsonPayload.toString());
  106. error e =>
  107. log:printError("Expected response payload not received", err = e);
  108. }
  109. foreach promise in promises {
  110. http:Response promisedResponse = new;
  111. var promisedResponseResult = clientEP->getPromisedResponse(promise);
  112. match promisedResponseResult {
  113. http:Response resultantPromisedResponse => {
  114. promisedResponse = resultantPromisedResponse;
  115. }
  116. error resultantErr => {
  117. log:printError("Error occurred while fetching promised response",
  118. err = resultantErr);
  119. return;
  120. }
  121. }
  122. var promisedPayload = promisedResponse.getJsonPayload();
  123. match promisedPayload {
  124. json promisedJsonPayload =>
  125. log:printInfo("Promised resource : " +
  126. promisedJsonPayload.toString());
  127. error e =>
  128. log:printError("Expected promised response payload not received",
  129. err = e);
  130. }
  131. }}

参考资料

https://ballerina.io/learn/by-example/http-2.0-server-push.html
https://ballerina.io/learn/by-example/http-1.1-to-2.0-protocol-switch.html

 
 
 
 

ballerina 学习二十一 http2的更多相关文章

  1. ballerina 学习 三十一 扩展开发(二)

    上篇说了使用ballerina 语言开发扩展模块,对于注解类型的我们是需要使用java 语言进行 开发的 官方提供了一个hello 的demo可以参考 https://github.com/balle ...

  2. Scala学习二十一——隐式转换和隐式参数

    一.本章要点 隐式转换用于类型之间的转换 必须引入隐式转换,并确保它们可以以单个标识符的形式出现在当前作用域 隐式参数列表会要求指定类型的对象.它们可以从当前作用域中以单个标识符定义的隐式对象的获取, ...

  3. JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  4. Python3.5 学习二十一

    本节内容概要: 上节回顾及补充知识点: 一.请求周期: URL->路由->函数或类->返回字符串或者模板 Form表单提交: 提交->url-函数或者类中的方法 -....(执 ...

  5. ballerina 学习二十九 数据库操作

    ballerina 数据操作也是比较方便的,官方也我们提供了数据操作的抽象,但是我们还是依赖数据库驱动的. 数据库驱动还是jdbc模式的 项目准备 项目结构 ├── mysql_demo │ ├── ...

  6. ballerina 学习二十八 快速grpc 服务开发

    ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们 只要编写简单的ballerina service 就可以了,proto 文件是自动帮 ...

  7. ballerina 学习二十七 项目k8s部署&& 运行

    ballerina k8s 部署和docker 都是同样的简单,编写service 添加注解就可以了 参考项目 https://ballerina.io/learn/by-guide/restful- ...

  8. ballerina 学习二十六 项目docker 部署&& 运行(二)

    ballerina 从发布,到现在官方文档的更新也是很给力的,同时也有好多改进,越来越好用了 可以参考官方文档 https://ballerina.io/learn/by-guide/restful- ...

  9. ballerina 学习二十五 项目docker 部署&& 运行

    ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...

随机推荐

  1. Spring MVC 复习笔记04

    复习 springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器:根据url查找Handler.(可以通 ...

  2. DB杂记

    1. mybatits 批量插入: <insert id="insertColumnitem2"> INSERT INTO REPORT_COLUMNITEM (COL ...

  3. QML基本可视化元素--Text

    一个Text项目可以显示纯文本或者富文本 1.     可以使用Html标记:text: “<b>HELLO</b>” 2.     宽度和高度(width, height): ...

  4. HDU 4616 Game(经典树形dp+最大权值和链)

    http://acm.hdu.edu.cn/showproblem.php?pid=4616 题意:给出一棵树,每个顶点有权值,还有存在陷阱,现在从任意一个顶点出发,并且每个顶点只能经过一次,如果经过 ...

  5. 从零开始,使用Docker Swarm部署集群教程

    本文首先从Dockerfile创建了一个简单web镜像 然后将web镜像推送到了远程仓库,以备后面集群中不同机器自动下载 之后使用docker-compose.yml配置了一个应用 而后新建了2台虚拟 ...

  6. javascript debugger

    打开调试工具,刷新,可以看到脚本被暂停 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  7. [原][译][osgearth][EarthFile]关于EarthFile 的Model Layer 讲解(通过earth文件加载模型层)(OE官方文档翻译)

    原文参考:http://docs.osgearth.org/en/latest/references/earthfile.html#model-layer 本人翻译能有限.... 模型层 模型层渲染“ ...

  8. 【转】IntelliJ IDEA的光芒会盖过Eclipse吗

    作为一个资深的Eclipse用户,我想对IntelliJ IDEA做一个更为严谨的审视.JetBrains的工作人员非常的友善,并为Podcastpedia.org和Codingpedia.org这两 ...

  9. JAVA消息 JMS 很重要

    首先大致讲一下,java 消息模块 消息,个人理解分为两种:1.同步消息(RPC调用) 2.异步消息(本篇讲解部分) 一.同步消息java提供了多种方案: 最新比较常用的方式就是spring Http ...

  10. C#使用(NamedPipe)命名管道通信的例子

    https://blog.csdn.net/yl2isoft/article/details/20228279