ballerina 学习二十八 快速grpc 服务开发
ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们
只要编写简单的ballerina service 就可以了,proto 文件是自动帮我们生成的,同时我们用这个
文件方便的生成各种客户端的代码
项目准备
- 项目结构
├── Ballerina.toml
├── grpc_service
│ └── order_mgt_service.bal
- grpc 代码
可以看出就是普通的ballerina service
import ballerina/grpc;
// gRPC service endpoint definition.
endpoint grpc:Listener listener {
host:"localhost",
port:9090
};
// Order management is done using an in memory map.
// Add sample orders to the 'orderMap' at startup.
map<orderInfo> ordersMap;
// Type definition for an order.
type orderInfo record {
string id;
string name;
string description;
};
// gRPC service.
@grpc:ServiceConfig
service orderMgt bind listener {
// gRPC method to find an order.
findOrder(endpoint caller, string orderId) {
string payload;
// Find the requested order from the map.
if (ordersMap.hasKey(orderId)) {
json orderDetails = check <json>ordersMap[orderId];
payload = orderDetails.toString();
} else {
payload = "Order : '" + orderId + "' cannot be found.";
}
// Send response to the caller.
_ = caller->send(payload);
_ = caller->complete();
}
// gRPC method to create a new Order.
addOrder(endpoint caller, orderInfo orderReq) {
// Add the new order to the map.
string orderId = orderReq.id;
ordersMap[orderReq.id] = orderReq;
// Create a response message.
string payload = "Status : Order created; OrderID : " + orderId;
// Send a response to the caller.
_ = caller->send(payload);
_ = caller->complete();
}
// gRPC method to update an existing Order.
updateOrder(endpoint caller, orderInfo updatedOrder) {
string payload;
// Find the order that needs to be updated.
string orderId = updatedOrder.id;
if (ordersMap.hasKey(orderId)) {
// Update the existing order.
ordersMap[orderId] = updatedOrder;
payload = "Order : '" + orderId + "' updated.";
} else {
payload = "Order : '" + orderId + "' cannot be found.";
}
// Send a response to the caller.
_ = caller->send(payload);
_ = caller->complete();
}
// gRPC method to delete an existing Order.
cancelOrder(endpoint caller, string orderId) {
string payload;
if (ordersMap.hasKey(orderId)) {
// Remove the requested order from the map.
_ = ordersMap.remove(orderId);
payload = "Order : '" + orderId + "' removed.";
} else {
payload = "Order : '" + orderId + "' cannot be found.";
}
// Send a response to the caller.
_ = caller->send(payload);
_ = caller->complete();
}
}
生成proto&&客户端代码
- proto生成
ballerina build grpc_service/
- 客户端代码
ballerina grpc --input target/grpc/orderMgt.proto --output grpc_client
- 修改生成的客户端代码
import ballerina/log;
import ballerina/grpc;
// This is client implementation for unary blocking scenario
public function main(string... args) {
// Client endpoint configuration
endpoint orderMgtBlockingClient orderMgtBlockingEp {
url:"http://localhost:9090"
};
// Create an order
log:printInfo("-----------------------Create a new order-----------------------");
orderInfo orderReq = {id:"100500", name:"XYZ", description:"Sample order."};
var addResponse = orderMgtBlockingEp->addOrder(orderReq);
match addResponse {
(string, grpc:Headers) payload => {
string result;
grpc:Headers resHeaders;
(result, resHeaders) = payload;
log:printInfo("Response - " + result + "\n");
}
error err => {
log:printError("Error from Connector: " + err.message + "\n");
}
}
// Update an order
log:printInfo("--------------------Update an existing order--------------------");
orderInfo updateReq = {id:"100500", name:"XYZ", description:"Updated."};
var updateResponse = orderMgtBlockingEp->updateOrder(updateReq);
match updateResponse {
(string, grpc:Headers) payload => {
string result;
grpc:Headers resHeaders;
(result, resHeaders) = payload;
log:printInfo("Response - " + result + "\n");
}
error err => {
log:printError("Error from Connector: " + err.message + "\n");
}
}
// Find an order
log:printInfo("---------------------Find an existing order---------------------");
var findResponse = orderMgtBlockingEp->findOrder("100500");
match findResponse {
(string, grpc:Headers) payload => {
string result;
grpc:Headers resHeaders;
(result, resHeaders) = payload;
log:printInfo("Response - " + result + "\n");
}
error err => {
log:printError("Error from Connector: " + err.message + "\n");
}
}
// Cancel an order
log:printInfo("-------------------------Cancel an order------------------------");
var cancelResponse = orderMgtBlockingEp->cancelOrder("100500");
match cancelResponse {
(string, grpc:Headers) payload => {
string result;
grpc:Headers resHeaders;
(result, resHeaders) = payload;
log:printInfo("Response - " + result + "\n");
}
error err => {
log:printError("Error from Connector: " + err.message + "\n");
}
}
}
运行&&测试
- 运行server
ballerina run grpc_service
- client 调用
ballerina run grpc_client
参考资料
https://ballerina.io/learn/by-guide/grpc-service/
https://github.com/ballerina-guides/grpc-service
ballerina 学习二十八 快速grpc 服务开发的更多相关文章
- ballerina 学习二十九 数据库操作
ballerina 数据操作也是比较方便的,官方也我们提供了数据操作的抽象,但是我们还是依赖数据库驱动的. 数据库驱动还是jdbc模式的 项目准备 项目结构 ├── mysql_demo │ ├── ...
- Java开发学习(二十八)----拦截器(Interceptor)详细解析
一.拦截器概念 讲解拦截器的概念之前,我们先看一张图: (1)浏览器发送一个请求会先到Tomcat的web服务器 (2)Tomcat服务器接收到请求以后,会去判断请求的是静态资源还是动态资源 (3)如 ...
- ballerina 学习二十二 弹性服务
主要包含断路器模式,负载均衡模式,故障转移,重试 Circuit Breaker 参考代码 import ballerina/http; import ballerina/log; import ba ...
- Python学习二十八周(vue.js)
一.指令 1.一个例子简单实用vue: 下载vue.js(这里实用1.0.21版本) 编写html代码: <!DOCTYPE html> <html lang="en&qu ...
- ballerina 学习二十六 项目docker 部署&& 运行(二)
ballerina 从发布,到现在官方文档的更新也是很给力的,同时也有好多改进,越来越好用了 可以参考官方文档 https://ballerina.io/learn/by-guide/restful- ...
- ballerina 学习二十五 项目docker 部署&& 运行
ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...
- ballerina 学习二十四 监控ballerina
ballerina 服务的监控还是比较方便的,以及集成了Prometheus Grafana Jaeger Elastic Stack 监控服务监控的集成 主要包含以下几个步骤 a. 安装docker ...
- JavaWeb学习 (二十八)————文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- ballerina 学习二十 http/https
提供http && https server && client 访问功能 client endpoint 说白了就是http client 参考代码 import b ...
随机推荐
- hdu1686字符串kmp
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- 微信公众号开发之如何一键导出微信所有用户信息到Excel
微信开发交流群:148540125 系列文章参考地址 极速开发微信公众号欢迎留言.转发.打赏 项目源码参考地址 点我点我--欢迎Start 极速开发微信公众号系列文章之如何一键导出微信所有用户信息到E ...
- Vim:replace with foobar (y/n/a/q/l/^E/^Y)?
y:to substitute this match n:to skip this match a:to substitute this and all remaining matches q:to ...
- [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- adobe flash player不是最新版本
adobe flash player不是最新版本
- PHP:第三章——PHP中返回引用的函数
<?php header("Content-Type:text/html;charset=utf-8"); $i=1; function &F(){ global $ ...
- harbor私有镜像仓库的搭建与使用与主从复制
harbor私有镜像仓库,私有仓库有两种,一种是harbor,一种是小型的私有仓库,harbor有两种模式,一种是主 从,一种是高可用仓库,项目需求,需要两台服务器,都有docker.ldap权限统一 ...
- 【转载】oracle之rowid详解
原文URL:http://www.2cto.com/database/201109/104961.html 本文讨论的是关于oracle从8i开始引进object的概念后的rowid,即扩展(exte ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 宇宙最帅叉叉——第五周博客 for 测试与发布(Alpha版本)
Alpha版本测试报告 1.在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个? a.修复的BUG UDP传输 recvfrom 当没有消息来的时候一直循环等待因其阻塞 ,时间戳无效了. ...