首页 技术 正文
技术 2022年11月6日
0 收藏 324 点赞 528 浏览 3495 个字

ballerina k8s 部署和docker 都是同样的简单,编写service 添加注解就可以了

参考项目 https://ballerina.io/learn/by-guide/restful-service/

项目准备

  • 项目代码
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 build restful_service_k8s
  • 生成的k8s部署文件
    ballerina 学习二十七  项目k8s部署&& 运行
    同时生成了helm 以及普通的部署文件(server ingress deploy ),很方便
    ballerina 学习二十七  项目k8s部署&& 运行
  • 运行图
    ballerina 学习二十七  项目k8s部署&& 运行

参考资料

https://ballerina.io/learn/by-guide/restful-service/
https://github.com/ballerina-guides/restful-service

    

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892