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/

posted on   荣锋亮  阅读(260)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-11-06 Webpack-dashboard 简单使用
2014-11-06 rpc介绍

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示