airlift 简单试用

airlift 使用简单,而且周边集成也不少,只是官方文档比较少,使用最多的也是trino 以及presto 中,trino 代码基于airlift 框架的开发代码看起来是
很简洁的

项目结构

 
├── README.md
├── app # 实际应用,使用airlift 开发的,包含了静态页面以及简单的rest api 
├── pom.xml
└── src
├── main
├── java
└── com
└── dalong
├── App.java
└── MyDemoResource.java
└── resources
└── webapp
├── app.css
└── index.html
└── test
└── java
├── package # 专门用来进行项目打包使用的,基于了一个类似maven assembly 的插件provisio,此插件在trino 中也有使用到
├── pom.xml
├── src
├── etc
└── config.properties
├── main
├── java
├── provisio
└── myapp.xml
└── resources
└── test
└── java
├── pom.xml

代码简单说明

  • app 部分
    主要是要一个rest api ,包装了静态页面以及应用的启动入口(airlift 自带的bootstrap 能力)
    App.java
 
public class App {
    public static void main(String[] args) {
        Bootstrap app = new Bootstrap(ImmutableList.<Module>builder()
                .add(new NodeModule())
                .add(new Module() {
                    @Override
                    public void configure(Binder binder) {
                       // node 环境配置,还是比较重要的
                        configBinder(binder).bindConfigDefaults(NodeConfig.class, nodeConfig -> {
                           nodeConfig.setEnvironment("test");
                        });
                        // rest api 注册
                        jaxrsBinder(binder).bind(MyDemoResource.class);
                        // 静态页面配置
                        httpServerBinder(binder).bindResource("/","webapp").withWelcomeFile("index.html");
                        httpServerBinder(binder).bindResource("/dalongdemoapp","webapp").withWelcomeFile("index.html");
                    }
                })
                .add(new HttpServerModule())
                .add(new JsonModule())
                .add(new JaxrsModule())
                .add(new EventModule())
                .build());
        app.initialize();
    }
}

MyDemoResource.java

@Path("/")
public class MyDemoResource
{
    private static final Logger log = Logger.get(MyDemoResource.class);
 
    @GET
    @Path("/v1/info")
    @Produces(APPLICATION_JSON)
    public String getInfo(
            @Context HttpServletRequest servletRequest) {
       return  "dalongdemo";
    }
} 
  • package 部分
    实际上参考了trino 的打包
    package/src/main/provisio/myapp.xml
 
<runtime>
    <!-- Target -->
    <archive name="${project.artifactId}-${project.version}.tar.gz" hardLinkIncludes="**/*.jar" />
 
    <!-- Server -->
    <artifactSet to="lib">
        <artifact id="${project.groupId}:app:${project.version}" />
    </artifactSet>
 
</runtime>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.dalong</groupId>
        <artifactId>mywebapp-airlift</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>package</artifactId>
 
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.dalong</groupId>
            <artifactId>app</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>ca.vanzyl.provisio.maven.plugins</groupId>
                <artifactId>provisio-maven-plugin</artifactId>
                <version>1.0.19</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>provision</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
 
</project>

说明

airlift 实际上就是对于不少现有框架的包装,使用起来还是很方便的,值得试试,上边app pom 文件就没贴,需要的可以github 查看

参考资料

https://github.com/airlift/airlift/tree/master/docs
https://github.com/rongfengliang/airlift-learning
https://github.com/jvanzyl/provisio

posted on 2023-01-27 19:52  荣锋亮  阅读(266)  评论(0编辑  收藏  举报

导航