最近幾年,Spring全家桶橫掃其它Java框架,已然成爲事實標準,單體應用使用Spring Framework+Spring Boot,服務治理Spring Cloud,生態完善,各類組件層出不窮。曾經還玩過JFinal和Nutz等國內的開發框架,可是僅僅是私下玩玩,實際工做過程當中,仍是Spring的天下,不多會選擇其它小衆的框架(誰知道會不會有填不完的大坑)。 最近在社區閒逛的時候,發現一個Red Hat開源的Java框架Quarkus,定位爲GraalVM和OpenJDK HotSpot量身定製的一個Kurbernetes Native Java框架。雖然開源時間較短,可是生態方面也已經達到可用的狀態,自身包含擴展框架,已經支持像Netty、Undertow、Hibernate、JWT等框架,足以用於開發企業級應用,用戶也能夠基於擴展框架自行擴展。java
開發環境 除了通常的Java開發環境外,你還須要額外安裝Graalvm,用於構建Native應用。 Graalvm安裝參考:Install GraalVMgit
建立項目 你能夠使用Maven命令快速建立一個Quarkus應用,也能夠自行搭建。 **方式一:**快速建立命令以下(來源於官網):apache
mvn io.quarkus:quarkus-maven-plugin:0.15.0:create \ -DprojectGroupId=org.acme \ -DprojectArtifactId=getting-started \ -DclassName="org.acme.quickstart.GreetingResource" \ -Dpath="/hello"
**方式二:**自行搭建,首先建立一個Maven項目,添加Quarkus的依賴和plugin,參考以下:框架
<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>cn.centychen</groupId> <artifactId>quarkus-simple-example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <surefire-plugin.version>3.0.0-M3</surefire-plugin.version> <quarkus.version>0.14.0</quarkus.version> </properties> <dependencyManagement> <dependencies> <!-- 管理Quarkus依賴包版本 --> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-bom</artifactId> <version>${quarkus.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 使用Resteasy框架 --> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency> <!-- Junit --> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5</artifactId> <scope>test</scope> </dependency> <!-- Rest接口測試 --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- quarkus maven插件 --> <plugin> <groupId>io.quarkus</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus.version}</version> <executions> <execution> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> <!-- 用於單元測試 --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <systemProperties> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> </systemProperties> </configuration> </plugin> </plugins> </build> </project>
編寫Resource類,實際就是Restful接口,按照Restful思想,接口即資源,因此叫Resource挺貼切的。該類中實現一個/hello/{name}
接口,參考以下:異步
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; /** * @author: cent * @email: chenzhao@viomi.com.cn * @date: 2019/5/4. * @description: */ @Path("/hello") public class GreetingResource { @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{name}") public CompletionStage<String> hello(@PathParam("name") String name) { //使用異步響應 return CompletableFuture.supplyAsync(() -> String.format("Hello,%s!", name)); } }
你會發現,上面沒有像Spring Boot框架同樣編寫main方法,也沒有部署到Web容器(如Tomcat、Undertow等)中,那麼應該如何啓動測試呢?很簡單,命令行進入項目根目錄,經過如下命令啓動開發測試:maven
mvn compile quarkus:dev
訪問http://localhost:8080/hello/cent
,返回以下: 單元測試
至此,一個簡單的基於Quarkus框架的Restful服務已經搭建好了,好像跟Spring Boot比較,除了啓動方式,其它沒太大差異哦?下面纔是差別的地方。測試
Quarkus定位是一個Native Java的框架,那麼怎樣將項目構建成Native應用呢?只須要兩個步驟:網站
pom.xml
中增長構建profile配置,以下:<profiles> <profile> <id>native</id> <build> <plugins> <plugin> <groupId>io.quarkus</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus.version}</version> <executions> <execution> <goals> <goal>native-image</goal> </goals> <configuration> <enableHttpUrlHandler>true</enableHttpUrlHandler> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
mvn package -Pnative
命令構建Native Image,構建完成後,target目錄下會存在一個名字爲[project name]-runner
的文件,這個就是應用的可執行文件,你能夠拷貝到其它目錄運行,運行以下:./quarkus-simple-example-1.0-SNAPSHOT-runner 2019-05-15 12:02:31,199 INFO [io.quarkus] (main) Quarkus 0.14.0 started in 0.012s. Listening on: http://[::]:8080 2019-05-15 12:02:31,201 INFO [io.quarkus] (main) Installed features: [cdi, resteasy]
上面僅僅是Quarkus的一個簡單示例,搭建一個Restful服務並構建成Native Image。完成這一步以後,你還能夠將Native Image構建成Docker鏡像並使用Kubernetes進行部署,後面的步驟與Spring Boot應用構建和發佈沒太大差別,在本文就不說了。 Quarkus目前仍是一個很輕量級的框架,使用簡單且啓動超快,但願後續版本演進可以保持足夠的輕量,若是變得像Spring全家桶那麼龐大,估計啓動也快不了了~ui