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

时间:2023-07-09 18:53:43

ballerina 的grpc 开发模型,对于开发者来说简单了好多,不是schema first 的方式,而是我们
只要编写简单的ballerina service 就可以了,proto 文件是自动帮我们生成的,同时我们用这个
文件方便的生成各种客户端的代码

项目参考 https://ballerina.io/learn/by-guide/grpc-service/

项目准备

  • 项目结构
├── 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 服务开发
ballerina 学习二十八 快速grpc 服务开发

  • 客户端代码
ballerina grpc --input target/grpc/orderMgt.proto --output grpc_client

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

  • 修改生成的客户端代码
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

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

  • client 调用
ballerina run grpc_client

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

参考资料

https://ballerina.io/learn/by-guide/grpc-service/
https://github.com/ballerina-guides/grpc-service