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 ...
随机推荐
- English trip -- VC(情景课)1 F Another view
Another view 另一种观点 拓展应用 Life-skills reading 生活技能阅读 Midtown Adult School 中城成人学校 NAME: Samir Ahmed ...
- 关于controller中调用多个service方法的问题
一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.
- 我的Java学习笔记-语法
Java的语法与C#的语法基本都一样,毕竟都是面向对象编程语言.下面记录下Java独有的和我在C#中学习不熟的语法知识 一.Java是解释型语言 二.Java修饰符 1. 访问控制修饰符 defaul ...
- 4.1 delegate
delegate ---packed up function public delegate double myDelegate (double x); my delegate d2 = new m ...
- RpcContext
RpcContext内部有一个ThreadLocal变量,它是作为ThreadLocalMap的key,表明每个线程有一个RpcContext. public class RpcContext { p ...
- Quartz定时任务和IIS程序池闲置超时时间冲突解决方案
一.问题描述 Bs项目中用Quartz功能执行一个定时任务(每隔5分钟执行一个Job),正常情况,Quartz定时任务会5分钟执行一次,但IIS程序池闲置 超时默认为20分钟,造成的结果是:定时任务只 ...
- bat批处理文件运行时隐藏cmd窗口
想让bat运行时隐藏cmd窗口,最好的方法是使用vbs文件实现, 1.新建一个文本文档,改名为123.vbs,编辑内容: set ws=WScript.CreateObject("WScri ...
- CCF 2015-03-3 节日
输出y1到y2年之间的每一年的a月份的第b个周c.感觉像是一个恶心的模拟.更像一个很恶心的小学奥赛题. 题目:http://115.28.138.223/view.page?gpid=T25 考试的时 ...
- CUDA ---- Memory Model
Memory kernel性能高低是不能单纯的从warp的执行上来解释的.比如之前博文涉及到的,将block的维度设置为warp大小的一半会导致load efficiency降低,这个问题无法用war ...
- jquery 中事件
jQuery 事件 - submit() 方法 定义和用法 当提交表单时,会发生 submit 事件. 该事件只适用于表单元素. submit() 方法触发 submit 事件,或规定当发生 subm ...