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 ...
随机推荐
- JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库
首先是JDBC 连接Oracle 数据库 package com.util; import com.pojo.UserInfo; import java.sql.*; public class DB ...
- RESTful源码学习笔记之RPC和Restful深入理解
以下资料搜集自网络 0x00 RPC RPC 即远程过程调用(Remote Procedure Call Protocol,简称RPC),像调用本地服务(方法)一样调用服务器的服务(方法).通常的实现 ...
- GreenOpenPaint的实现(三)添加标尺
标尺对于图像处理程序,特别是需要精确测量的程序来说意义很大.这里进行了专门的研究. 基于现在已经引入的类和定义的变量,主要讲一讲如何调用. 1.添加放大缩小 void CGreenOpenPaintD ...
- linux及安全期中总结——20135227黄晓妍
Linux及安全期中总结 黄晓妍 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ...
- Linux内核分析第一周-通过分析汇编代码理解计算机是如何工作的
首先,我们先写一个简单的C语言程序,如下: int g(int x) { return x +3; } int f(int x) { return g(x); } int main(void) { r ...
- Ant Design of Angular
1.按照官方的方法,报了这个 node_modules/rxjs/internal/types.d.ts(81,74): error TS1005: ';' expected.node_modules ...
- springboot 日期类型处理
1. 日期类型输出参数处理 默认日期格式只支持:2017-07-01T10:10:01 修改为2017-07-01 10:10:01 只需要修改配置文件即可: spring: jackson: dat ...
- java 生成验证Guid码
- Notepad++ 管理工程--转载
http://blog.csdn.net/cashey1991/article/details/7001385 @1.首先从下面这个菜单打开工程panel @2.在工程panel的“Workspace ...
- php 格式化时间
<?php echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . " ...