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. LeetCode--035--搜索插入位置

    问题描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 方法1:for 循环 class S ...

  2. 3-18 关于namespace,双冒号::的用法; SelfYield.

    关于namespace,双冒号::的用法. 防止引用多个模块在一个文件/类中,有重名的对象.::可以调用类的类方法,和常量. class Foo   BAR = "hello"   ...

  3. Struts2 简介图

    Struts2官方提供的,strus2的内部工作机制图解.

  4. Vue自动化工具(Vue-CLI)

    一.组件的概念 1.概念 组件(Component)是自定义封装的功能.在前端开发过程中,经常出现多个网页的功能是重复的,而且很多不同的网站之间,也存在同样的功能. 而在网页中实现一个功能,需要使用h ...

  5. PHP函数总结 (五)

    <?php /** * 回调函数: * 指调用函数时并不是传递一个标准的变量作为参数,而是将另一个函数作为参数传递到调用的函数中 * 使用回调函数可以 将一段自己定义的功能传到函数内部使用 * ...

  6. 『科学计算』L0、L1与L2范数_理解

     『教程』L0.L1与L2范数 一.L0范数.L1范数.参数稀疏 L0范数是指向量中非0的元素的个数.如果我们用L0范数来规则化一个参数矩阵W的话,就是希望W的大部分元素都是0,换句话说,让参数W是稀 ...

  7. 使用dbms_output.put_line打印异常所在的行

    dbms_output.put_line(dbms_utility.format_error_stack); dbms_output.put_line(dbms_utility.format_call ...

  8. h5请求跨域问题Access-Control-Allow-Origin解决跨域

    访问后端接口报错:No 'Access-Control-Allow-Origin' header is present on the requested resource 解决: Access-Con ...

  9. POJ 2488 DFS

    DES:给一个n行m列的棋盘.马以L型走.问能否从某一位置开始走完棋盘上的每个位置.若能继续输出字典序最小的一条路径. 很典型的dfs.搜的时候就按照字典序从小到大的顺序.搜到第一条路径时停止搜索输出 ...

  10. ADO SQL delete 日期条件参数

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...