Quarkus初探

Quarkus初探

開場(chǎng)白

我們知道StringBoot的在開發(fā)商的地位,一直到現(xiàn)在的5.0,很多微服務(wù)的搭建還是大量基于它開發(fā)的,時(shí)代的變遷,也有歷史的原因?qū)е铝?br> SpringBoot在啟動(dòng)速度上一直是個(gè)問題,盡管有很多優(yōu)化啟動(dòng)的方案。上次對(duì)bootique做了基本的了解,短時(shí)間內(nèi)也不會(huì)撼動(dòng)SpringBoot
的地位。但是不得不提的是近期IBM換帥,5天前Quarkus1.3.2的發(fā)布,不得不對(duì)這個(gè)云世界中占據(jù)小片江山的框架做下介紹。

準(zhǔn)備

  • JDK8或者JDK11更高

  • Apache Maven 3.6.2以上。(我這里用了brew安裝,替換了我之前source安裝方式)

zzw:blog zzw$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 13.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
Default locale: zh_CN_#Hans, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"

zzw:blog zzw$ java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)

示例

你可以按照Bootstrapping項(xiàng)目j進(jìn)行操作,然后逐步創(chuàng)建應(yīng)用。

也可以下載示例項(xiàng)目,示例項(xiàng)目位于getting-started

git clone https://github.com/quarkusio/quarkus-quickstarts.git

首次創(chuàng)建項(xiàng)目

忽略下載項(xiàng),創(chuàng)建一個(gè)項(xiàng)目耗時(shí)7s,我的MBP是2011年的???♂?

zzw:temp zzw$ time mvn io.quarkus:quarkus-maven-plugin:1.3.2.Final:create -DprojectGroupId=com.wonderingwall -DprojectArtifactId=first-demo -DclassName="com.wonderingwall.quarkus.FirstResource" -Dpath="/hello"
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- quarkus-maven-plugin:1.3.2.Final:create (default-cli) @ standalone-pom ---
Creating a new project in /Users/zzw/Develop/workspace/github/temp/first-demo
[INFO] 
[INFO] Maven Wrapper version 0.5.6 has been successfully set up for your project.
[INFO] Using Apache Maven: 3.6.3
[INFO] Repo URL in properties file: https://repo.maven.apache.org/maven2
[INFO] 
[INFO] 
[INFO] ========================================================================================
[INFO] Your new application has been created in /Users/zzw/Develop/workspace/github/temp/first-demo
[INFO] Navigate into this directory and launch your application with mvn quarkus:dev
[INFO] Your application will be accessible on http://localhost:8080
[INFO] ========================================================================================
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.578 s
[INFO] Finished at: 2020-04-16T12:18:54+08:00
[INFO] ------------------------------------------------------------------------

real    0m7.519s
user    0m13.601s
sys 0m1.086s

這樣就創(chuàng)建了一個(gè)getting-started的項(xiàng)目。包含了以下內(nèi)容:

  • Maven的項(xiàng)目結(jié)構(gòu)。
  • com.wonderingwall.quarkus.GreetingResource指向了路徑/hello
  • 包含了相關(guān)的單元測(cè)試。
  • http://localhost:8080是啟動(dòng)應(yīng)用程序后可訪問的登錄頁面。
  • nativejvm兩種模式的Dockerfile文件在src/main/docker下。
  • 應(yīng)用的配置文件。

包含了最簡(jiǎn)單的Rest請(qǐng)求接口。

zzw:temp zzw$ cat getting-started/src/main/java/com/wonderingwall/quarkus/FirstResource.java 
package com.wonderingwall.quarkus;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class FirstResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
} 

與vanilla JAX-RS的區(qū)別:

使用Quarkus,不需要?jiǎng)?chuàng)建Application類。它支持,但不是必需的。此外,只創(chuàng)建資源的一個(gè)實(shí)例,而不是每個(gè)請(qǐng)求創(chuàng)建一個(gè)實(shí)例。
您可以使用不同的作用域注釋(ApplicationScoped、RequestScoped等)對(duì)此進(jìn)行配置。

啟動(dòng)應(yīng)用

使用命令mvnw compile quarkus:dev運(yùn)行。

zzw:first-demo zzw$ mvnw compile quarkus:dev
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.wonderingwall:first-demo >--------------------
[INFO] Building first-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ first-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ first-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zzw/Develop/workspace/github/temp/first-demo/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.870 s
[INFO] Finished at: 2020-04-16T12:19:16+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project first-demo: Fatal error compiling: 無效的目標(biāo)發(fā)行版: 11 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

編譯失敗,錯(cuò)誤的發(fā)行版。原因我用的JDK8,沒有11。修改pom.xml文件:

11     <maven.compiler.source>8</maven.compiler.source>
12     <maven.compiler.target>8</maven.compiler.target>

繼續(xù)執(zhí)行mvnw compile quarkus:dev。

zzw:first-demo zzw$ mvnw compile quarkus:dev
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.wonderingwall:first-demo >--------------------
[INFO] Building first-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ first-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ first-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zzw/Develop/workspace/github/temp/first-demo/target/classes
[INFO] 
[INFO] --- quarkus-maven-plugin:1.3.2.Final:dev (default-cli) @ first-demo ---
Listening for transport dt_socket at address: 5005
__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2020-04-16 12:23:15,986 INFO  [io.quarkus] (main) first-demo 1.0-SNAPSHOT (powered by Quarkus 1.3.2.Final) started in 5.584s. Listening on: http://0.0.0.0:8080
2020-04-16 12:23:16,002 INFO  [io.quarkus] (main) Profile dev activated. Live Coding activated.
2020-04-16 12:23:16,002 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]


訪問http://localhost:8080,顯示如下:

quarkus_first_attempt.png

查看下接口調(diào)用情形:

zzw:getting-started zzw$ http localhost:8080/hello
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain;charset=UTF-8

hello
 

依賴注入

Quarkus中的依賴注入基于ArC,它是為Quarkus架構(gòu)量身定制的基于CDI的依賴注入解決方案。

添加Bean文件。

1 package com.wonderingwall.quarkus;
2 
3 import javax.enterprise.context.ApplicationScoped;
4 
5 @ApplicationScoped
6 public class FirstService {
7 
8     public String getSay(String name) {
9         return "hello " + name;
10     }
11 } 

添加訪問路徑,注入Bean。

1 package com.wonderingwall.quarkus;
2 
3 import javax.inject.Inject;
4 import javax.ws.rs.GET;
5 import javax.ws.rs.Path;
6 import javax.ws.rs.Produces;
7 import javax.ws.rs.core.MediaType;
8 
9 import org.jboss.resteasy.annotations.jaxrs.PathParam;
10 
11 @Path("/hello")
12 public class FirstResource {
13 
14     @Inject
15     FirstService service;
16 
17     @GET
18     @Produces(MediaType.TEXT_PLAIN)
19     @Path("/{name}")
20     public String sayHello(@PathParam String name) {
21         return service.getSay(name);
22     }
23 
24     @GET
25     @Produces(MediaType.TEXT_PLAIN)
26     public String hello() {
27         return "hello";
28     }
29 } 

執(zhí)行mvnw compile quarkus:dev。運(yùn)行完訪問接口。

zzw:getting-started zzw$ http localhost:8080/hello/david
HTTP/1.1 200 OK
Content-Length: 11
Content-Type: text/plain;charset=UTF-8

hello david
 

打包

使用命令mvnw package打包,完成后會(huì)創(chuàng)建一個(gè)target目錄并會(huì)包含2個(gè)文件。

  • first-demo-1.0-SNAPSHOT.jar 僅包含項(xiàng)目的類和資源,這是Maven構(gòu)建產(chǎn)生的常規(guī)Jar;
  • first-demo-1.0-SNAPSHOT-runner.jar 一個(gè)可執(zhí)行文件,但是它不是一個(gè)fatjar,因?yàn)樗幸蕾嚢急粡?fù)制到了target/lib下。
zzw:first-demo zzw$ mvnw package
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.wonderingwall:first-demo >--------------------
[INFO] Building first-demo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ first-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ first-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/zzw/Develop/workspace/github/temp/first-demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ first-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/zzw/Develop/workspace/github/temp/first-demo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ first-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/zzw/Develop/workspace/github/temp/first-demo/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ first-demo ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.wonderingwall.quarkus.FirstResourceTest
2020-04-16 14:13:09,534 INFO  [io.quarkus] (main) Quarkus 1.3.2.Final started in 4.719s. Listening on: http://0.0.0.0:8081
2020-04-16 14:13:09,537 INFO  [io.quarkus] (main) Profile test activated. 
2020-04-16 14:13:09,538 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 12.894 s - in com.wonderingwall.quarkus.FirstResourceTest
2020-04-16 14:13:14,239 INFO  [io.quarkus] (main) Quarkus stopped in 0.229s
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ first-demo ---
[INFO] Building jar: /Users/zzw/Develop/workspace/github/temp/first-demo/target/first-demo-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- quarkus-maven-plugin:1.3.2.Final:build (default) @ first-demo ---
[INFO] [org.jboss.threads] JBoss Threads version 3.0.1.Final
[INFO] [io.quarkus.deployment.pkg.steps.JarResultBuildStep] Building thin jar: /Users/zzw/Develop/workspace/github/temp/first-demo/target/first-demo-1.0-SNAPSHOT-runner.jar
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 3327ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  27.197 s
[INFO] Finished at: 2020-04-16T14:13:21+08:00
[INFO] ------------------------------------------------------------------------ 

總結(jié)

開發(fā)流程和習(xí)慣都沒有什么改變,SpringBoot無縫上手,畢竟IBM也做的很成熟了。速度上要優(yōu)于Spring,后續(xù)看Native下GraalVM結(jié)合應(yīng)該很完美,將來
趨勢(shì)應(yīng)該很明顯。缺點(diǎn)么就是畢竟是IBM的,很多關(guān)聯(lián)的都是IBM開發(fā)的,jboss之類依賴還是很多的,感覺會(huì)很重,不過這點(diǎn)好像也不是現(xiàn)在這個(gè)開發(fā)環(huán)境會(huì)
考慮的問題。作為Java的開發(fā)者,我推薦它,正因?yàn)槟欠輬?zhí)著。

參考

QUARKUS - CREATING YOUR FIRST APPLICATION

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容