ballerina 学习二十七 项目k8s部署&& 运行
2024-10-19 06:18:41
ballerina k8s 部署和docker 都是同样的简单,编写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部署文件
同时生成了helm 以及普通的部署文件(server ingress deploy ),很方便
- 运行图
参考资料
https://ballerina.io/learn/by-guide/restful-service/
https://github.com/ballerina-guides/restful-service
最新文章
- PHP 链接多种数据库 的方法
- 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢
- Android 利用RecyclerView.Adapter刷新列表中的单个view问题
- Linux下man手册使用
- ThinkPHP报错处理
- 虚拟机 vlan trunk 特性
- spark新能优化之数据本地化
- 业务gis 怎么让别的开发人员不需要懂gis就可以搞开发? (五)
- pandas库学习笔记(二)DataFrame入门学习
- 电商H5制作常使用的排版方式
- SQL Server 无法启动之TCP端口占用
- 配置 php-fpm 监听的socket
- sql还原(.bak文件还原)
- Extjs:添加查看全部按钮
- Git的基本使用教程
- Qt编写的开源帖子集合(懒人专用)
- Todolist组件
- Android studio button 按钮 四种绑定事件的方法
- 通过python-libvirt管理KVM虚拟机 源码
- uafxcwd.lib(afxmem.obj) : error LNK2005: ";void * __cdecl operator new(unsigned int)";解决办法
热门文章
- (GoRails) 用app/decorators来取代app/helpers; delegate()方法
- Android 列表使用(ListView GridView Gallery图片计时滚动)
- MySQL缓存机制
- 51nod-1534-博弈
- 【转】EntityFramework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句
- dubbo监控中心搭建
- PHP:第一章——PHP中的位运算
- bug 问题
- SQL Server 调优系列基础篇 - 常用运算符总结
- 快速切题 sgu103. Traffic Lights 最短路 难度:1