1. 程式人生 > >ballerina 學習二十七 專案k8s部署&& 執行

ballerina 學習二十七 專案k8s部署&& 執行

import ballerina/http;
import ballerinax/kubernetes;

// 支援k8s 的註解
@kubernetes:Ingress {
    hostname:"dalongrong",
    name:"ballerina-guides-restful-service",
    path:"/"
}
@kubernetes:Service {
    serviceType:"NodePort",
    name:"ballerina-guides-restful-service"
}
@kubernetes:Deployment {
    image:"dalongrong/restful_service_k8s:v1.0",
    name:"ballerina-guides-restful-service"
}
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 學習 專案k8s部署&& 執行

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

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

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

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

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

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

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

學習

linux學習七周一次課(7月10日)10.1 使用w查看系統負載10.2 vmstat命令10.3 top命令10.4 sar命令10.5 nload命令使用w查看系統負載w命令load average: 0.00, 0.01, 0.05這裏表示系統負載,單位時間段內 一分鐘之內 十分鐘之內 十五分鐘之內最

JavaWeb學習 ()————監聽器(Listener)在開發中的應用

  監聽器在JavaWeb開發中用得比較多,下面說一下監聽器(Listener)在開發中的常見應用 一、統計當前線上人數   在JavaWeb應用開發中,有時候我們需要統計當前線上的使用者數,此時就可以使用監聽器技術來實現這個功能了。 1 package me.gacl.web.

【Katalon學習】數值型別

在Katalon Studio中設計自動化測試時,使用者通常需要為某些測試步驟配置資料,例如: 將輸入引數傳遞給測試步驟。 從測試步驟中獲取輸出值。 您可以從Katalon Studio支援的多種值型別中進行選擇,如下所示: Value Type

ballerina 學習八 快速grpc 服務開發

ballerina 的grpc 開發模型,對於開發者來說簡單了好多,不是schema first 的方式,而是我們 只要編寫簡單的ballerina service 就可以了,proto 檔案是自動幫我們生成的,同時我們用這個 檔案方便的生成各種客戶端的程式碼 專案準備 專案結構

ballerina 學習九 資料庫操作

dockerfile: mysql 資料庫的dockerfile,包含資料的初始化 FROM mysql/mysql-server:5.7 COPY inid.sql /docker-entrypoint-initdb.d docker-compose.yaml: docker-compose 執

shell學習天----退出狀態和if語句

退出狀態 每一條命令;不管是內建的,shell函式,還是外部的,當它退出時,都會返回一個小的整數值給引用它的程式,這就是大家所熟知的程式的退出狀態.在shell下執行程序是,有很多方式可取用程式的退出狀態. 以管理來說,退出狀態為0表示”成功”,也就是,程式執行完成且為遭遇

Gradle學習()——多專案構建詳解

跨專案配置 雖然子專案之間可以完全隔離單獨配置,但是子專案直接有相同特徵的情況也是很常見的,多個專案共享配置是更好的選擇。 配置和執行 在gradle學習-十八-構建的生命週期這一篇中我們已經講過Gradle構建過程中的各個階段,我們繼續擴充套件到多專

Java基礎學習筆記 DBUtils和連接池

ride 基本 代碼塊 ear 不同 一行 ria 靜態方法 ... DBUtils 如果只使用JDBC進行開發,我們會發現冗余代碼過多,為了簡化JDBC開發,本案例我們講采用apache commons組件一個成員:DBUtils。DBUtils就是JDBC的簡化開發工

【轉】JMeter學習)Jmeter常見問題

pre 麻煩 continue 而不是 行為 let 方式 prop 右上角 收集工作中JMeter遇到的各種問題 1. JMeter的工作原理是什麽?   向服務器提交請求;從服務器取回請求返回的結果。 2. JMeter的作用?   JMeter可以用於測試

輕松學習JavaScript:DOM編程學習之事件模型

經歷 學習 不存在 obj 發生 rip gb2 article 不支持 在介紹事件模型之前,我們先來看什麽是事件和什麽是event對象。 一事件介紹 JavaScript事件是由訪問Web頁面的用戶引起的一系列操作,使

Linux學習筆記()sed

sedsedsed是一種流編輯器,它是文本處理中非常中的工具,能夠完美的配合正則表達式使用,功能不同凡響。處理時,把當前處理的行存儲在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成後,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾

Linux學習總結()任務計劃,系統服務管理

crontab chkconfig systemctl unit target 1 任務計劃 說白了就是運行命令或者腳本的一個定時器,他可以讓我們在休息時間自動給我們執行任相關任務。來看下它的配置文件:cat /etc/crontab第一行定義了 shell環境第二行定義 環境變量第三行定

馬哥教育至三學習總結

函數、數組、變量馬哥M28二十七、進制轉換、$(())可以將其他進制轉成十進制顯示出來,用法如下:echo $((N#xxxxx)) N表示進制名稱如2 8 16;#後面跟的是當前進制數值echo $((2#110)) -> 6 計劃任務crontab -e:1.註意:crontab計劃任

python學習)郵件發送及面向對象編程(1)

() ive none 類名 學習 什麽 安全 password 一個 郵件發送 使用郵件發送功能,需要先做以下三點: 1、在郵箱設置裏面打開smtp服務 2、開啟郵箱授權碼,記住這個授權碼 3、安裝yagmail, 用 pip install yagmail-0.10.

【Android Studio安裝部署系列】、Android studio修改項目名稱和包名

detail 如何 裏的 繼續 想要 example 發現 and 版權 版權聲明:本文為HaiyuKing原創文章,轉載請註明出處! 概述 實際項目開發中可能碰到項目名稱寫錯了或者需要修改,而且包名可能也想要修改,那麽如何操作呢。 本文是在Android Studi

、python學習之前端():Vue入門

一、vue簡介: 1.vue簡介:   Vue.js是前端三大新框架:Angular.js、React.js、Vue.js之一,Vue.js目前的使用和關注程度在三大框架中稍微勝出,並且它的熱度還在遞增。   Vue.js可以作為一個js庫來使用,也可以用它全套的工具來構建系統介