ballerina 学习二十六 项目docker 部署&& 运行(二)
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 部署&& 运行(二)的更多相关文章
- ballerina 学习二十五 项目docker 部署&& 运行
ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...
- javaweb学习总结二十六(response对象的用法二 下载文件)
一:浏览器打开服务器上的文件 1:读取服务器上面的资源,如果在web层,可以直接使用servletContext,如果在非web层 可以使用类加载器读取文件 2:向浏览器写数据,实际上是把数据封装到r ...
- 性能测试二十六:环境部署之Mysql+Redis+Tomcat环境整合
系统中使用了缓存+数据库,通用读取数据规则1.先从缓存读数据,如果有,直接返回数据:2.如果没有,去数据库中读,然后再插入到缓存中,再返回数据 Mysql+Redis+Tomcat环境整合 1.修改P ...
- (二十六)svn的问题二
上周五请了一天假,电脑放在公司没有带回来,三天的时间都没有看代码,使得我电脑上的东西与svn上相差了太多,因为不一样,所以就要更新同步,因为要更新同步的东西多,便又出了一些问题,也因此对svn有了更进 ...
- Bootstrap <基础二十六>进度条
Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...
- 二十六个月Android学习工作总结【转】
原文:二十六个月Android学习工作总结 1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该 ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- 深度学习(二十六)Network In Network学习笔记
深度学习(二十六)Network In Network学习笔记 Network In Network学习笔记 原文地址:http://blog.csdn.net/hjimce/article/deta ...
- Web 前端开发人员和设计师必读精华文章【系列二十六】
<Web 前端开发精华文章推荐>2014年第5期(总第26期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
随机推荐
- 用Omniauth来Login with Facebook(Go-rails课程)
https://gorails.com/episodes/login-with-facebook?autoplay=1 大概看了一遍,留了视频的截图. https://gorails.com/epis ...
- android--------热修复介绍
热修复技术在近年来飞速发展,尤其是在InstantRun方案推出之后,各种热修复技术竞相涌现.国内大部分成熟的主流APP都拥有自己的热修复技术,像手淘.支付宝.QQ.饿了么.美团等等. 代码热修复是最 ...
- Jersey 2.x JDK 上的客户端应用
如应用是运行在 JDK 上的话,你只需要使用 JAX-RS 中的客户端部分就可以了,这个根据你使用的客户端有所调整. 这里有一系列的模块是可以供你使用的,例如 grizzly 或 apache 或 j ...
- PHP函数总结 (五)
<?php /** * 回调函数: * 指调用函数时并不是传递一个标准的变量作为参数,而是将另一个函数作为参数传递到调用的函数中 * 使用回调函数可以 将一段自己定义的功能传到函数内部使用 * ...
- python-day15函数递归
1.递归: 在函数内,调用自己. (技巧: 每次调用时,函数前面需加上return,这样返回值就可以一层一层 的返回去) #def age(n):# if n == 1:# re ...
- hdu 1175 bfs+priority_queue
连连看 如上图所示如果采用传统bfs的话,如果按照逆时针方向从(1,1)-->(3,4)搜索,会优先选择走拐四次弯的路径导致ans错误: Time Limit: 20000/10000 MS ( ...
- Form嵌入到Panel里(C#)
直接把这个 Form嵌入到一个 Panel中即可. 示例如下: 要嵌入的 Form: public partial class FormEmbed : Form { public FormEmbed( ...
- 在EO中获取某字段基于表的列名
//生成EO的时候自动生成的字段 public static final int BRIEFINTRO = 88; String[][] str = null; str = new String[][ ...
- Sentry项目监控工具结合vue的安装与使用(前端)
一.官网:https://sentry.io/welcome/ 二.介绍 Sentry 是一个开源的实时错误报告工具,支持 web 前后端.移动应用以及游戏,支持 Python.OC.Java.Go. ...
- html form表单提交后处理返回数据
上传如果通过form提交并且需要处理返回值.(其实用ajax上传方式刚好,看需要) 可以这么做: <form id="importBookForm" action=" ...