zoukankan      html  css  js  c++  java
  • 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 ! 
    ");
           _ = 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 ! 
    ");
            // 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/

  • 相关阅读:
    java的类继承(与c++对比)
    java的数据类型、自动拆装箱、字面量
    java中关键字static和final
    JVM之JIT
    java之JIT(Just in time)
    栈和堆
    C++中vector的使用
    canvas
    ajax笔记
    CSS笔记
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9914103.html
Copyright © 2011-2022 走看看