ballerina 学习 三十一 扩展开发(二)
上篇说了使用ballerina 语言开发扩展模块,对于注解类型的我们是需要使用java 语言进行
开发的
官方提供了一个hello 的demo可以参考 https://github.com/ballerinax/hello
注解扩展插件的使用
这个注解是调用方法之后,会生成一个文本文件
import ballerina/http;
import ballerinax/hello;
@hello:Greeting{
salutation: "Guten Tag!"
}
service<http:Service> helloWorld bind {port:9091} {
sayHello(endpoint outboundEP, http:Request request) {
http:Response response = new;
response.setTextPayload("Hello, World from service helloWorld ! \n");
_ = outboundEP->respond(response);
}
}
开发细节
开发需要的环境是jdk8 同时需要maven ,构建完成之后,用户需要安装扩展,为了测试我使用的是直接clone 代码
- clone 代码
git clone https://github.com/ballerinax/hello.git
- 导入项目
- 构建&&测试
mvn clean package
- 构建结果
- 说明
hello-extension-0.980.0.jar 文件需要放ballerian 的 /bre/lib,同时repo 包需要解压,并放到
- 测试
import ballerina/http;
import ballerinax/hello;
@hello:Greeting{salutation : "Guten Tag!"}
@http:ServiceConfig {
basePath:"/dalong"
}
service<http:Service> helloWorld bind {port:9091} {
sayHello(endpoint outboundEP, http:Request request) {
http:Response response;
response.setJsonPayload("Hello, World from service helloWorld ! \n");
// Send response to the client.
_ = outboundEP->respond(response);
}
}
- 效果
会生成一个hello_world.txt 文件
Guten Tag!
说明
以下为代码的结构
- 因为开发会有依赖的打包,官方的方式是使用maven 的maven-shade-plugin ,好多mapreduce 以及开发打包也是类似的
详细使用说明,可以参考https://ballerina.io/learn/how-to-extend-ballerina/ - 主要的处理代码是实现 AbstractCompilerPlugin 其中包含了具体的代码处理,以及逻辑
实现 init process codeGenerated 方法,docker 扩展是一个比较有价值的参考项目
参考资料
https://github.com/ballerinax/docker
https://github.com/ballerinax/hello
https://ballerina.io/learn/how-to-extend-ballerina/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2017-11-06 Webpack-dashboard 简单使用
2014-11-06 rpc介绍