ballerina 从发布,到现在官方文档的更新也是很给力的,同时也有好多改进,越来越好用了

可以参考官方文档 https://ballerina.io/learn/by-guide/restful-service/

项目初始化

  • 项目结构
└── guide└── restful_service└── order_mgt_service.bal

  • 初始化项目
cd  guide &&   ballerina init

  • 效果

添加代码&& docker 支持

  • http rest 服务编写
import ballerina/http;
import ballerinax/docker;// docker 支持配置
@docker:Config {registry:"dalongrong",name:"restful_service",tag:"v1.0"
}
endpoint http:Listener listener {port:9090
};// Order management is done using an in memory map.
// Add some sample orders to 'ordersMap' at startup.
map<json> ordersMap;// RESTful service.
@http:ServiceConfig { basePath: "/ordermgt" }
service<http:Service> orderMgt bind listener {// Resource that handles the HTTP GET requests that are directed to a specific// order using path '/order/<orderId>'.@http:ResourceConfig {methods: ["GET"],path: "/order/{orderId}"}findOrder(endpoint client, http:Request req, string orderId) {// Find the requested order from the map and retrieve it in JSON format.json? payload = ordersMap[orderId];http:Response response;if (payload == null) {payload = "Order : " + orderId + " cannot be found.";}// Set the JSON payload in the outgoing response message.response.setJsonPayload(untaint payload);// Send response to the client._ = client->respond(response);}// Resource that handles the HTTP POST requests that are directed to the path// '/order' to create a new Order.@http:ResourceConfig {methods: ["POST"],path: "/order"}addOrder(endpoint client, http:Request req) {json orderReq = check req.getJsonPayload();string orderId = orderReq.Order.ID.toString();ordersMap[orderId] = orderReq;// Create response message.json payload = { status: "Order Created.", orderId: orderId };http:Response response;response.setJsonPayload(untaint payload);// Set 201 Created status code in the response message.response.statusCode = 201;// Set 'Location' header in the response message.// This can be used by the client to locate the newly added order.response.setHeader("Location", "http://localhost:9090/ordermgt/order/" +orderId);// Send response to the client._ = client->respond(response);}// Resource that handles the HTTP PUT requests that are directed to the path// '/order/<orderId>' to update an existing Order.@http:ResourceConfig {methods: ["PUT"],path: "/order/{orderId}"}updateOrder(endpoint client, http:Request req, string orderId) {json updatedOrder = check req.getJsonPayload();// Find the order that needs to be updated and retrieve it in JSON format.json existingOrder = ordersMap[orderId];// Updating existing order with the attributes of the updated order.if (existingOrder != null) {existingOrder.Order.Name = updatedOrder.Order.Name;existingOrder.Order.Description = updatedOrder.Order.Description;ordersMap[orderId] = existingOrder;} else {existingOrder = "Order : " + orderId + " cannot be found.";}http:Response response;// Set the JSON payload to the outgoing response message to the client.response.setJsonPayload(untaint existingOrder);// Send response to the client._ = client->respond(response);}// Resource that handles the HTTP DELETE requests, which are directed to the path// '/order/<orderId>' to delete an existing Order.@http:ResourceConfig {methods: ["DELETE"],path: "/order/{orderId}"}cancelOrder(endpoint client, http:Request req, string orderId) {http:Response response;// Remove the requested order from the map._ = ordersMap.remove(orderId);json payload = "Order : " + orderId + " removed.";// Set a generated payload with order status.response.setJsonPayload(untaint payload);// Send response to the client._ = client->respond(response);}
}

  • 运行
ballerina run restful_service

  • 测试
post 数据
curl -v -X POST -d \
'{ "Order": { "ID": "100500", "Name": "XYZ", "Description": "Sample order."}}' \
"http://localhost:9090/ordermgt/order" -H "Content-Type:application/json"get
curl -i http://localhost:9090/ordermgt/order/100500

  • 构建(支持docker)
ballerina build restful_service


  • 生成的dockerfile

    ballerina 生成的中间语言是跨平台的

# Auto Generated Dockerfile
FROM ballerina/ballerina:0.982.0
LABEL maintainer="dev@ballerina.io"
COPY restful_service.balx /home/ballerina
CMD ballerina run restful_service.balx

  • docker 运行
docker run -d -p 9090:9090 dalongrong/restful_service:v1.0

  • 运行流程图

    可以使用vscode 的插件,直接查看,很方便

说明

ballerina 对于开发来说还真的是比较方便,平台的支持也很好,后边会有k8s运行的测试

参考资料

https://ballerina.io/learn/by-guide/restful-service/

ballerina 学习二十六 项目docker 部署 运行(二)相关推荐

  1. Polyworks脚本开发学习笔记(十六)-用C#进行Polyworks二次开发

    Polyworks脚本开发学习笔记(十六)-用C#进行Polyworks二次开发 Polyworks支持C#二次开发,用对应的SDK文档试着做一下开发样例. 新建一个C#项目,在解决方案中右键添加引用 ...

  2. 二十六、多孔介质模型(二)-催化器

    1.概念介绍 许多工业应用,如过滤器.催化剂床和填料,都涉及到模拟通过多孔介质的流动.本案例为气体通过催化转换器的流动.催化转换器通常用于净化汽油和柴油发动机的所产生的废气,将有害环境的废气转化为可接 ...

  3. 【opencv学习之四十六】OpenCV4.2 QR二维码识别及定位

    首先系统得安装opencv4以上的库,代码实验了一下,相比zxing和zbar,更简洁一些,并且提供了二维码定位: 代码如下: #include <iostream> #include & ...

  4. 十六. 项目干系人管理

    @(项目经理考试学习笔记) 十六. 项目干系人管理 1. 概述 干系人在新版的教材中叫项目相关方 2.识别干系人 2.1 输入,输出,工具技术 2.2 干系人分析 2.3 干系人登记册 3. 规划干系 ...

  5. 【Microsoft Azure 的1024种玩法】二十六. 在Azure VM中手动部署Windows Admin Center管理平台

    [简介] Windows Admin Center是微软开发的一套可以部署在本地基于浏览器的GUI的工具集平台,其平台可用于管理Windows相关服务器和PC机器,我们可以利用Windows Admi ...

  6. Docker系列(二十四)——Docker实例六Docker安装Redis实例

    < Docker实例三Docker安装Redis实例 > 前言 在前面一篇文章种,完成了 < Docker安装MongoDB实例 >,本篇将继续镜像安装教程,并完成Docker ...

  7. 窗口消息——Windows核心编程学习手札之二十六

    窗口消息 --Windows核心编程学习手札之二十六 Windows允许一个进程至多建立10000个不同类型的用户对象(user object):图符.光标.窗口类.菜单.加速键表等,当一个线程调用一 ...

  8. OpenCV学习笔记(二十六)——小试SVM算法ml OpenCV学习笔记(二十七)——基于级联分类器的目标检测objdect OpenCV学习笔记(二十八)——光流法对运动目标跟踪Video Ope

    OpenCV学习笔记(二十六)--小试SVM算法ml 总感觉自己停留在码农的初级阶段,要想更上一层,就得静下心来,好好研究一下算法的东西.OpenCV作为一个计算机视觉的开源库,肯定不会只停留在数字图 ...

  9. 零基础带你学习MySQL—foreign key 外键(二十六)

    零基础带你学习MySQL-foreign key 外键(二十六) -- 外键演示 -- 创建 主表 my_class CREATE TABLE my_class ( id INT PRIMARY KE ...

最新文章

  1. (0027)iOS 开发之调整导航条上BarButtonItem与屏幕边界的间距
  2. java怎么通过ip地址查具体地址_制作通过IP 查询地址的java版程序
  3. 对称矩阵(Symmetric Matrices)
  4. 记下来 Spring 装配 Bean 的三种方式
  5. Fiori应用的书签模式 - bookmark
  6. 64位linux下的gns3网络模拟器配置
  7. YouTube 多目标排序系统:如何推荐接下来收看的视频
  8. Day1:360培训学习重点笔记(7.13)
  9. 支付宝回应 AI 换脸风险;新 iPhone 或将于 13 号接受预订;Linux Lite 4.6 发布 | 极客头条...
  10. java mysql怎么改密码错误_java 修改mysql密码的四种方法
  11. Python学习-第一天-函数和模块的使用
  12. 计算机视觉技术与应用综述
  13. 车架号 生成 java_JAVA匹配车架号以及生成虚拟车架号
  14. C/C++ - http协议发送字段,文件,单个和多张图片
  15. linux 磁盘碎片整理
  16. STM32F4之按键(二)
  17. php面试题4---php面试题系列
  18. atcoder abc284 E
  19. python获取数据库返回字符串出现/uxxxxxx解决方案
  20. 《图解番茄工作法》读后感

热门文章

  1. ActiveMQ学习(四)——应用程序接口
  2. 比尔盖茨:十条“金口玉言”-- 世界不会在意你的自尊
  3. Python3中的魔术方法汇总
  4. 代码也浪漫——Python烟花秀
  5. yolov3前向传播(三)-- 坐标转换,iou计算,权重加载,图片显示
  6. hibernate hql 关联查询_Hibernate【关联查询篇】
  7. C++ 常见错误(02) —— 将dll(用c++写的)处理的结果展示在界面上
  8. mysql什么是表的并的关系_MySQL表与表的关系
  9. python 相对导入_Python相对导入机制详解
  10. 虚拟机实现java线程_深入理解java虚拟机(23):java与多线程