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 ...
随机推荐
- Git冲突:commit your changes or stash them before you can merge. 解决办法
用git pull来更新代码的时候,遇到了下面的问题: 1 2 3 4 error: Your local changes to the following files would be overwr ...
- 三.野指针和free
在C语言项目中,经常会遇到需要程序员手动分配内存的地方.这样做能够节省大量的内存空间,也让程序更加灵活.只要你有一定的基础,那么肯定用过 malloc 或者 ralloc和free的组合.这个组合使用 ...
- noip2018 爆炸记
noip2018 爆炸记 day-4 ~ day-2 最后考了两套模拟题,题目好水啊,但是我还是爆炸了. 第一套最后一道题竟然时一道毒瘤打表?但是我看着插头DP可做啊..(然而我并不会插头DP)然后还 ...
- 【bzoj2721】[Violet 5]樱花
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=2721 好久没做数学题了,感觉有些思想僵化,走火入魔了. 这道题就是求方程$ \frac ...
- Java IO流-Properties
2017-11-05 21:37:50 Properties Properties:Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其 ...
- 查找一个Class到底在那一个jar文件里
整理自己的一些笔记,发觉这个命令 ,看起来是用来找一个Class到底在那一个jar文件里的. 虽然没有再测一下,估计是好使的. 先在博客园里记下来,防止自己忘掉. findstr /S /M org. ...
- IDEAL葵花宝典:java代码开发规范插件 GenerateAllSetter、ECtranslation、translation、插件
前言: GenerateAllSetter: 我们在程序开发过程中,往往要编写这样的类:类的部分或者全部属性不希望让外部世界直接访问,而不用public字段修饰.这样,方法调用成了访问这些属性的唯一 ...
- HIVE从路人到入门
绪论 第一章 Hive的基本架构及原理 第二章 基础知识 第三章 基本操作 第四章 复杂操作 总结
- Integer与int的种种比较你知道多少
如果面试官问Integer与int的区别:估计大多数人只会说道两点,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null. 但是如果面试官再问一下Integer i = 1 ...
- 高并发数据采集的架构应用(Redis的应用)
问题的出发点: 最近公司为了发展需要,要扩大对用户的信息采集,每个用户的采集量估计约3W.如果用户量增加的话,将会大量照成采集量成3W倍的增长,但是又要满足日常业务需要,特别是报表数据必要 ...