1. 程式人生 > >ballerina 學習二十六 專案docker 部署&& 執行(二)

ballerina 學習二十六 專案docker 部署&& 執行(二)

ballerina 從釋出,到現在官方文件的更新也是很給力的,同時也有好多改進,越來越好用了

專案初始化

  • 專案結構
└── 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="[email protected]"
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執行的測試

參考資料

相關推薦

ballerina 學習 專案docker 部署&& 執行

ballerina 從釋出,到現在官方文件的更新也是很給力的,同時也有好多改進,越來越好用了 專案初始化 專案結構 └── guide └── restful_service └── order_mgt_service.bal 初始化專案

ballerina 學習 專案docker 部署&& 執行

ballerina 從釋出,到現在官方文件的更新也是很給力的,同時也有好多改進,越來越好用了 可以參考官方文件 https://ballerina.io/learn/by-guide/restful-service/ 專案初始化 專案結構 └── guide └──

ballerina 學習專案k8s部署&& 執行

import ballerina/http; import ballerinax/kubernetes; // 支援k8s 的註解 @kubernetes:Ingress { hostname:"dalongrong", name:"ballerina-guides-rest

、COUNT(*)與COUNT到底誰更快?

        COUNT(*)與COUNT(列)到底誰更快? *count(列)當列值為空,將不被統計。 1、資料準備 以下命令執行有問題請參照上篇文章 --做個試驗,看看到底誰更快? dr

ballerina 學習專案k8s部署&& 執行

ballerina k8s 部署和docker 都是同樣的簡單,編寫service 添加註解就可以了 參考專案 https://ballerina.io/learn/by-guide/restful-service/ 專案準備 專案程式碼 import ballerina/h

效能測試:環境部署之Mysql+Redis+Tomcat環境整合

系統中使用了快取+資料庫,通用讀取資料規則1、先從快取讀資料,如果有,直接返回資料;2、如果沒有,去資料庫中讀,然後再插入到快取中,再返回資料 Mysql+Redis+Tomcat環境整合 1、修改PerfTeach/WEB-INF/classes/redis-config.properties2、將re

性能測試:環境部署之Mysql+Redis+Tomcat環境整合

web-inf 技術 提高 tar 從數據 之前 sse 取數 最小 系統中使用了緩存+數據庫,通用讀取數據規則1、先從緩存讀數據,如果有,直接返回數據;2、如果沒有,去數據庫中讀,然後再插入到緩存中,再返回數據 Mysql+Redis+Tomcat環境整合 1、修改Per

python 入門之 – 進位制運算

二進位制是由 0 ~ 1 組成的 八進位制是由 0 ~ 7組成的 十進位制是由 0 ~ 9組成的 十六進位制是由 0 ~ 15 組成的,可是 9 後面的的 10 是用字母來代替 A~ F ,也就是 0 ~ F,用字母代替了數字,避免不再重複 之前剛接觸 python 的時候學了以下 二進

docker 部署 flask配置環境及測試

index www cnblogs 發現 images 註意事項 文章 進入 系統啟動 簡介: flask也是要部署的。不能老在我們的pycharm裏面跑測試服務器。 各種配置linux,我看就算了吧。我們用docker部署。 也就兩三行

CSDN中字型顏色的進位制表轉載

顏色名列表 顏色名 十六進位制顏色值 顏色 AliceBlue #F0F8FF rgb(240, 248, 255) AntiqueWhite #FAEBD7

JAVA學習筆記-----第天引用資料型別

●引用資料型別(類)     ■ 類的型別有兩種:         ◆第一種,JAVA為我們提供好的類,如Scanner類,Math類,這些已存在的類中包含了很多的方法和屬性,可供我們使用。   &

Qt淺談之:TCP和UDP之一

一、簡介        Qt使用QtNetwork模組來進行網路程式設計,提供了一層統一的套接字抽象用於編寫不同層次的網路程式,避免了應用套接字進行網路編的繁瑣(因有時需引用底層作業系統的相關資料結構)。有較底層次的類如QTcpSocket、QTcpServer和QUdp

十進位制轉化為二進位制與進位制顯示彙編程式

stacks segment stack dw 200h dup(0);不太明白要200h這麼大 stacks ends data segment in_buf db 6;定義輸入字串最大長度 in_len db ?;輸入字串實際長度 dec_buf db 6 dup(3

Java進階專題() 訊息中介軟體架構體系2-- RabbitMQ研究

# 前言 接上文,這個繼續介紹RabbitMQ,並理解其底層原理。 # 介紹 RabbitMQ是由erlang語言開發,基於AMQP(Advanced Message Queue 高階訊息佇列協議)協議實現的訊息佇列。 為什麼使用RabbitMQ呢? 1、使得簡單,功能強大。 2、基於AMQP協議。

Java基礎學習筆記 集合框架

first 哈希 cat etag 基於 col 容器 處的 新元素 List List接口的特點: 它是一個元素存取有序的集合。例如,存元素的順序是11、22、33。那麽集合中,元素的存儲就是按照11、22、33的順序完成的。 它是一個帶有索引的集合,通過索引就

Linux學習筆記grep

grepgrepgrep [-cinvABC] ‘word‘ filename -c 行數-i 不區分大小寫-n 顯示行號-v 取反-r 遍歷所有子目錄-A 後面跟數字,過濾出符合要求的行以及下面n行-B 同上,過濾出符合要求的行以及上面n行-C 同上,同時過濾出符合要求的行以及上下各n行 mkdir /tm

馬哥教育第二四至學習總結

進程管理 計劃任務 shell進階1馬哥教育M28-24天、 1.使用nmcli創建bond, team ,bridgebondnmcli connection add con-name bond0 type bond ifname bond0 mode active-backupnmcli conn

Linux學習總結防火墻規則之firewalld

firewalld iptables 一iptables 規則備份 service iptables save //會把規則保存到/etc/sysconfig/iptables把iptables規則備份到my.ipt文件中iptables-save > my.ipt恢復剛才備份的規則 iptab

Java學習總計——JavaScript正則表達式,Js表單驗證,原生js+css頁面時鐘

text 先來 helloword 郵箱 用戶名 就是 lac round 外部 一.JavaScript正則表達式1.exec檢索字符串中指定的值,返回找到的值,並確定其位置2.test檢索字符串中指定的值,返回true或false3.正則表達式對象的創建:(1)方式一:

、python中json學習

十六 bank cbc bsp python文件 Coding pickle passwd strong 1.json序列介紹:提供4個關鍵字:dumps,dump,loads,load(與pickle用法完全相同)   語法:f.write(bytes(json.dump