ballerina 学习二十一 http2
ballerina 支持http2 协议,包含server push
http2 协议
- 参考代码
import ballerina/http;
import ballerina/log;endpoint http:Client http2serviceClientEP {
url: "http://localhost:7090",
httpVersion: "2.0"};@http:ServiceConfig {
basePath: "/http11Service"
}
service<http:Service> http11Service bind { port: 9090 } { @http:ResourceConfig {
path: "/"
}
http11Resource(endpoint caller, http:Request clientRequest) {
var clientResponse =
http2serviceClientEP->forward("/http2service", clientRequest); http:Response response = new;
match clientResponse {
http:Response resultantResponse => {
response = resultantResponse;
}
error err => {
response.statusCode = 500;
response.setPayload(err.message); }
}
caller->respond(response) but {
error e => log:printError(
"Error occurred while sending the response",
err = e) }; }
}endpoint http:Listener http2serviceEP {
port: 7090,
httpVersion: "2.0"};@http:ServiceConfig {
basePath: "/http2service"
}
service http2service bind http2serviceEP { @http:ResourceConfig {
path: "/"
}
http2Resource(endpoint caller, http:Request clientRequest) {
http:Response response = new;
json msg = { "response": { "message": "response from http2 service" } };
response.setPayload(msg);
caller->respond(response) but {
error e => log:printError(
"Error occurred while sending the response",
err = e) }; }
}
server push
- 参考代码
import ballerina/http;
import ballerina/log;
endpoint http:Listener http2ServiceEP {
port: 7090,
httpVersion: "2.0"};@http:ServiceConfig {
basePath: "/http2Service"
}
service http2Service bind http2ServiceEP { @http:ResourceConfig {
path: "/"
}
http2Resource(endpoint caller, http:Request req) {
http:PushPromise promise1 = new(path = "/resource1", method = "GET");
caller->promise(promise1) but {
error e => log:printError(
"Error occurred while sending the promise1",
err = e) };
http:PushPromise promise2 = new(path = "/resource2", method = "GET");
caller->promise(promise2) but {
error e => log:printError(
"Error occurred while sending the promise2",
err = e) };
http:PushPromise promise3 = new(path = "/resource3", method = "GET");
caller->promise(promise3) but {
error e => log:printError(
"Error occurred while sending the promise3",
err = e) };
http:Response response = new;
json msg = { "response": { "name": "main resource" } };
response.setPayload(msg);
caller->respond(response) but {
error e => log:printError(
"Error occurred while sending the response",
err = e) };
http:Response push1 = new;
msg = { "push": { "name": "resource1" } };
push1.setPayload(msg);
caller->pushPromisedResponse(promise1, push1) but {
error e => log:printError(
"Error occurred while sending the promised response1",
err = e) };
http:Response push2 = new;
msg = { "push": { "name": "resource2" } };
push2.setPayload(msg);
caller->pushPromisedResponse(promise2, push2) but {
error e => log:printError(
"Error occurred while sending the promised response2",
err = e) };
http:Response push3 = new;
msg = { "push": { "name": "resource3" } };
push3.setPayload(msg);
caller->pushPromisedResponse(promise3, push3) but {
error e => log:printError(
"Error occurred while sending the promised response3",
err = e) }; }
}
import ballerina/http;
import ballerina/log;
endpoint http:Client clientEP {
url: "http://localhost:7090",
httpVersion: "2.0"};function main(string... args) { http:Request serviceReq = new;
http:HttpFuture httpFuture = new;
var submissionResult = clientEP->submit("GET", "/http2Service", serviceReq); match submissionResult {
http:HttpFuture resultantFuture => {
httpFuture = resultantFuture;
}
error resultantErr => {
log:printError("Error occurred while submitting a request",
err = resultantErr);
return;
}
}
http:PushPromise[] promises = [];
int promiseCount = 0;
boolean hasPromise = clientEP->hasPromise(httpFuture); while (hasPromise) {
http:PushPromise pushPromise = new;
var nextPromiseResult = clientEP->getNextPromise(httpFuture); match nextPromiseResult {
http:PushPromise resultantPushPromise => {
pushPromise = resultantPushPromise;
}
error resultantErr => {
log:printError("Error occurred while fetching a push promise",
err = resultantErr);
return;
}
}
log:printInfo("Received a promise for " + pushPromise.path); if (pushPromise.path == "/resource2") {
clientEP->rejectPromise(pushPromise); log:printInfo("Push promise for resource2 rejected");
} else {
promises[promiseCount] = pushPromise; promiseCount = promiseCount + 1;
}
hasPromise = clientEP->hasPromise(httpFuture);
} http:Response response = new;
var result = clientEP->getResponse(httpFuture); match result {
http:Response resultantResponse => {
response = resultantResponse;
}
error resultantErr => {
log:printError("Error occurred while fetching response",
err = resultantErr);
return;
}
} var responsePayload = response.getJsonPayload();
match responsePayload {
json resultantJsonPayload =>
log:printInfo("Response : " + resultantJsonPayload.toString());
error e =>
log:printError("Expected response payload not received", err = e);
}
foreach promise in promises {
http:Response promisedResponse = new;
var promisedResponseResult = clientEP->getPromisedResponse(promise);
match promisedResponseResult {
http:Response resultantPromisedResponse => {
promisedResponse = resultantPromisedResponse;
}
error resultantErr => {
log:printError("Error occurred while fetching promised response",
err = resultantErr);
return;
}
}
var promisedPayload = promisedResponse.getJsonPayload();
match promisedPayload {
json promisedJsonPayload =>
log:printInfo("Promised resource : " +
promisedJsonPayload.toString());
error e =>
log:printError("Expected promised response payload not received",
err = e);
}
}}
参考资料
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的更多相关文章
- ballerina 学习 三十一 扩展开发(二)
上篇说了使用ballerina 语言开发扩展模块,对于注解类型的我们是需要使用java 语言进行 开发的 官方提供了一个hello 的demo可以参考 https://github.com/balle ...
- Scala学习二十一——隐式转换和隐式参数
一.本章要点 隐式转换用于类型之间的转换 必须引入隐式转换,并确保它们可以以单个标识符的形式出现在当前作用域 隐式参数列表会要求指定类型的对象.它们可以从当前作用域中以单个标识符定义的隐式对象的获取, ...
- JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- Python3.5 学习二十一
本节内容概要: 上节回顾及补充知识点: 一.请求周期: URL->路由->函数或类->返回字符串或者模板 Form表单提交: 提交->url-函数或者类中的方法 -....(执 ...
- ballerina 学习二十九 数据库操作
ballerina 数据操作也是比较方便的,官方也我们提供了数据操作的抽象,但是我们还是依赖数据库驱动的. 数据库驱动还是jdbc模式的 项目准备 项目结构 ├── mysql_demo │ ├── ...
- ballerina 学习二十八 快速grpc 服务开发
ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们 只要编写简单的ballerina service 就可以了,proto 文件是自动帮 ...
- ballerina 学习二十七 项目k8s部署&& 运行
ballerina k8s 部署和docker 都是同样的简单,编写service 添加注解就可以了 参考项目 https://ballerina.io/learn/by-guide/restful- ...
- ballerina 学习二十六 项目docker 部署&& 运行(二)
ballerina 从发布,到现在官方文档的更新也是很给力的,同时也有好多改进,越来越好用了 可以参考官方文档 https://ballerina.io/learn/by-guide/restful- ...
- ballerina 学习二十五 项目docker 部署&& 运行
ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...
随机推荐
- Spring MVC 复习笔记04
复习 springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器:根据url查找Handler.(可以通 ...
- DB杂记
1. mybatits 批量插入: <insert id="insertColumnitem2"> INSERT INTO REPORT_COLUMNITEM (COL ...
- QML基本可视化元素--Text
一个Text项目可以显示纯文本或者富文本 1. 可以使用Html标记:text: “<b>HELLO</b>” 2. 宽度和高度(width, height): ...
- HDU 4616 Game(经典树形dp+最大权值和链)
http://acm.hdu.edu.cn/showproblem.php?pid=4616 题意:给出一棵树,每个顶点有权值,还有存在陷阱,现在从任意一个顶点出发,并且每个顶点只能经过一次,如果经过 ...
- 从零开始,使用Docker Swarm部署集群教程
本文首先从Dockerfile创建了一个简单web镜像 然后将web镜像推送到了远程仓库,以备后面集群中不同机器自动下载 之后使用docker-compose.yml配置了一个应用 而后新建了2台虚拟 ...
- javascript debugger
打开调试工具,刷新,可以看到脚本被暂停 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- [原][译][osgearth][EarthFile]关于EarthFile 的Model Layer 讲解(通过earth文件加载模型层)(OE官方文档翻译)
原文参考:http://docs.osgearth.org/en/latest/references/earthfile.html#model-layer 本人翻译能有限.... 模型层 模型层渲染“ ...
- 【转】IntelliJ IDEA的光芒会盖过Eclipse吗
作为一个资深的Eclipse用户,我想对IntelliJ IDEA做一个更为严谨的审视.JetBrains的工作人员非常的友善,并为Podcastpedia.org和Codingpedia.org这两 ...
- JAVA消息 JMS 很重要
首先大致讲一下,java 消息模块 消息,个人理解分为两种:1.同步消息(RPC调用) 2.异步消息(本篇讲解部分) 一.同步消息java提供了多种方案: 最新比较常用的方式就是spring Http ...
- C#使用(NamedPipe)命名管道通信的例子
https://blog.csdn.net/yl2isoft/article/details/20228279