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. ballerina 学习二十五 项目docker 部署&& 运行

    ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...

  2. javaweb学习总结二十六(response对象的用法二 下载文件)

    一:浏览器打开服务器上的文件 1:读取服务器上面的资源,如果在web层,可以直接使用servletContext,如果在非web层 可以使用类加载器读取文件 2:向浏览器写数据,实际上是把数据封装到r ...

  3. 性能测试二十六:环境部署之Mysql+Redis+Tomcat环境整合

    系统中使用了缓存+数据库,通用读取数据规则1.先从缓存读数据,如果有,直接返回数据:2.如果没有,去数据库中读,然后再插入到缓存中,再返回数据 Mysql+Redis+Tomcat环境整合 1.修改P ...

  4. (二十六)svn的问题二

    上周五请了一天假,电脑放在公司没有带回来,三天的时间都没有看代码,使得我电脑上的东西与svn上相差了太多,因为不一样,所以就要更新同步,因为要更新同步的东西多,便又出了一些问题,也因此对svn有了更进 ...

  5. Bootstrap <基础二十六>进度条

    Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...

  6. 二十六个月Android学习工作总结【转】

    原文:二十六个月Android学习工作总结 1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍.     2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该 ...

  7. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  8. 深度学习(二十六)Network In Network学习笔记

    深度学习(二十六)Network In Network学习笔记 Network In Network学习笔记 原文地址:http://blog.csdn.net/hjimce/article/deta ...

  9. Web 前端开发人员和设计师必读精华文章【系列二十六】

    <Web 前端开发精华文章推荐>2014年第5期(总第26期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

随机推荐

  1. 『TensorFlow』通过代码理解gan网络_中

    『cs231n』通过代码理解gan网络&tensorflow共享变量机制_上 上篇是一个尝试生成minist手写体数据的简单GAN网络,之前有介绍过,图片维度是28*28*1,生成器的上采样使 ...

  2. 多目标跟踪方法:deep-sort

    多目标跟踪方法:deep-sort deep_sort Multitarget tracking data association 读'Simple Online and Realtime Track ...

  3. 利用nodeJs anywhere搭建本地服务器环境

    1.npm install anywhere -g 如果是mac系统会提示你权限不够,需要在代码前加上 sudo获取管理员权限.即sudo npm install anywhere -g. 2.安装完 ...

  4. After reading a picture than out a picture

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  5. Mvc 学习笔记(一)

    1. MVC 表示 模型-视图-控制器.MVC是一种用于开发应用程序的模式,具备良好的架构,可测试和易于维护.基于MVC应用程序中包含: Models:表示应用程序的数据,并使用验证逻辑强制执行业务规 ...

  6. POJ 3481 treap

    这是利用treap写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了. 相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生 ...

  7. silent install oracle 11.2.0.1 x86_64 for linux

    su - root#groupadd oinstall#useradd -g oinstall oracle#passwd oracle#mkdir -p /u01/app/oracle#chown ...

  8. 快速切题 poj1258

    坑!!!我还以为一个整数会截到两行!! Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40056   Ac ...

  9. Delphi IDE 版本

    产品 compiler Version version bds 2007-WIN32 VER 180 185 11 5.0 2009 VER 200 13 6.0 XE VER 220 15 8.0 ...

  10. Flask初级(二)为flash创建路由,访问路径。

    Project name :Flask_Plan 上一篇文章,我们创建了默认的flask项目,也可以运行起来. 但是只有一个首页,只显示一个hello world. 现在我们创建访问路由,也就是访问地 ...