这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本文是《quarkus实战》系列的第二篇,目标是开发HelloWorld应用,让我们对quarkus有最基本的了解,写好的代码会在以下几种场景运行,这应该覆盖了大部分运行情况,绿色背景的表示具体的运行方式,可见一共有五种:
为达到上述目标,接下来的文章会被划分为这几个部分:
mvn io.quarkus.platform:quarkus-maven-plugin:2.7.1.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=getting-started \
-Dextensions="resteasy"
cd getting-started
mvn "io.quarkus:quarkus-maven-plugin:create" \
-DprojectGroupId="com.bolingcavalry" \
-DprojectArtifactId="hello-quarkus" \
-DprojectVersion="1.0-SNAPSHOT" \
-DclassName="HobbyResource" \
-Dpath="actions"
[INFO] -----------
[INFO]
applying codestarts...
[INFO] 📚 java
🔨 maven
📦 quarkus
📝 config-properties
🔧 dockerfiles
🔧 maven-wrapper
🚀 resteasy-codestart
[INFO]
-----------
[SUCCESS] ✅ quarkus project has been successfully generated in:
--> /home/will/temp/202202/02/24/001/hello-quarkus
-----------
[INFO]
[INFO] ========================================================================================
[INFO] Your new application has been created in /home/will/temp/202202/02/24/001/hello-quarkus
[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: 02:10 min
[INFO] Finished at: 2022-02-24T08:03:03+08:00
[INFO] ------------------------------------------------------------------------
will@lenovo:~/temp/202202/02/24/001$ tree
.
└── hello-quarkus
├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md
└── src
├── main
│ ├── docker
│ │ ├── Dockerfile.jvm
│ │ ├── Dockerfile.legacy-jar
│ │ ├── Dockerfile.native
│ │ └── Dockerfile.native-micro
│ ├── java
│ │ └── com
│ │ └── bolingcavalry
│ │ └── HobbyResource.java
│ └── resources
│ ├── application.properties
│ └── META-INF
│ └── resources
│ └── index.html
└── test
└── java
└── com
└── bolingcavalry
├── HobbyResourceTest.java
└── NativeHobbyResourceIT.java
14 directories, 13 files
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
此时可以用IDE导入这个maven工程了,我这边用的是IDEA,可以看到自动生成的类HobbyResource.java,是个web接口响应类:
如下图红框,做一点小小的改动,用于验证能否生效:
改了web接口的返回值,还要记得修改对应的单元测试代码,否则测试就不能通过了,修改HobbyResourceTest.java,如下图黄框所示,原本是is方法,现在改为containsString:
mvn compile quarkus:dev
[INFO] Nothing to compile - all classes are up to date
Listening for transport dt_socket at address: 5005
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-02-25 08:07:33,050 INFO [io.quarkus] (Quarkus Main Thread) hello-quarkus 1.0-SNAPSHOT on JVM (powered by Quarkus 2.7.1.Final) started in 1.580s. Listening on: http://localhost:8080
2022-02-25 08:07:33,066 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2022-02-25 08:07:33,067 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation, vertx]
--
Tests paused
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>
我这里输入r表示继续执行单元测试,等待测试用例执行完成后,如下图:
用浏览器访问http://localhost:8080/actions,即可看到响应信息,如下图,内容与我们修改的一致:
访问http://localhost:8080/q/dev/,可以看到丰富的系统信息,该地址只在dev模式有效:
mvn clean package -U -Dquarkus.package.type=uber-jar
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-quarkus ---
[INFO] Building jar: /home/will/temp/202202/02/24/001/hello-quarkus/target/hello-quarkus-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- quarkus-maven-plugin:2.7.1.Final:build (default) @ hello-quarkus ---
[INFO] [io.quarkus.deployment.pkg.steps.JarResultBuildStep] Building uber jar: /home/will/temp/202202/02/24/001/hello-quarkus/target/hello-quarkus-1.0-SNAPSHOT-runner.jar
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 2837ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 26.274 s
[INFO] Finished at: 2022-02-26T09:05:51+08:00
[INFO] ------------------------------------------------------------------------
will@lenovo:~/temp/202202/02/24/001/hello-quarkus/target$ java -jar hello-quarkus-1.0-SNAPSHOT-runner.jar
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-02-26 09:12:12,968 INFO [io.quarkus] (main) hello-quarkus 1.0-SNAPSHOT on JVM (powered by Quarkus 2.7.1.Final) started in 0.841s. Listening on: http://0.0.0.0:8080
2022-02-26 09:12:12,990 INFO [io.quarkus] (main) Profile prod activated.
2022-02-26 09:12:12,990 INFO [io.quarkus] (main) Installed features: [cdi, resteasy, smallrye-context-propagation, vertx]
mvn clean package -U -DskipTests -Dnative -Dquarkus.native.container-build=true
mvn clean package -U -DskipTests -Dnative -Dquarkus.native.native-image-xmx=4096m -Dquarkus.native.container-build=true
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] Running Quarkus native-image plugin on GraalVM 21.3.1 Java 11 CE (Java Version 11.0.14+9-jvmci-21.3-b09)
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildRunner] docker run --env LANG=C --rm --user 1000:1000 -v /home/will/temp/202202/02/24/001/hello-quarkus/target/hello-quarkus-1.0-SNAPSHOT-native-image-source-jar:/project:z --name build-native-TArNh quay.io/quarkus/ubi-quarkus-native-image:21.3-java11 -J-Djava.util.logging.manager=org.jboss.logmanager.LogManagerGraalVM -J-Dsun.nio.ch.maxUpdateArraySize=100 -J-Dvertx.logger-delegate-factory-class-name=io.quarkus.vertx.core.runtime.VertxLogDelegateFactory -J-Dvertx.disableDnsResolver=true -J-Dio.netty.leakDetection.level=DISABLED -J-Dio.netty.allocator.maxOrder=3 -J-Duser.language=zh -J-Duser.country=CN -J-Dfile.encoding=UTF-8 -H:-ParseOnce -J--add-exports=java.security.jgss/sun.security.krb5=ALL-UNNAMED -J--add-opens=java.base/java.text=ALL-UNNAMED -H:InitialCollectionPolicy=com.oracle.svm.core.genscavenge.CollectionPolicy\$BySpaceAndTime -H:+JNI -H:+AllowFoldMethods -J-Djava.awt.headless=true -H:FallbackThreshold=0 -H:+ReportExceptionStackTraces -H:-AddAllCharsets -H:EnableURLProtocols=http -H:-UseServiceLoaderFeature -H:+StackTrace hello-quarkus-1.0-SNAPSHOT-runner -jar hello-quarkus-1.0-SNAPSHOT-runner.jar
[hello-quarkus-1.0-SNAPSHOT-runner:24] classlist: 3,543.22 ms, 1.18 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (cap): 615.36 ms, 1.18 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] setup: 2,545.56 ms, 1.18 GB
02:04:45,844 INFO [org.jbo.threads] JBoss Threads version 3.4.2.Final
[hello-quarkus-1.0-SNAPSHOT-runner:24] (clinit): 653.12 ms, 3.30 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (typeflow): 5,560.43 ms, 3.30 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (objects): 25,577.19 ms, 3.30 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (features): 1,546.08 ms, 3.30 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] analysis: 34,608.07 ms, 3.30 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] universe: 1,705.85 ms, 3.30 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (parse): 6,557.68 ms, 4.91 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (inline): 8,785.79 ms, 5.82 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] (compile): 33,586.33 ms, 6.02 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] compile: 51,302.42 ms, 6.02 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] image: 3,213.24 ms, 6.02 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] write: 608.88 ms, 6.02 GB
[hello-quarkus-1.0-SNAPSHOT-runner:24] [total]: 97,869.00 ms, 6.02 GB
# Printing build artifacts to: /project/hello-quarkus-1.0-SNAPSHOT-runner.build_artifacts.txt
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildRunner] docker run --env LANG=C --rm --user 1000:1000 -v /home/will/temp/202202/02/24/001/hello-quarkus/target/hello-quarkus-1.0-SNAPSHOT-native-image-source-jar:/project:z --entrypoint /bin/bash quay.io/quarkus/ubi-quarkus-native-image:21.3-java11 -c objcopy --strip-debug hello-quarkus-1.0-SNAPSHOT-runner
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 174203ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:58 min
[INFO] Finished at: 2022-02-26T10:06:08+08:00
[INFO] ------------------------------------------------------------------------
docker build \
-f src/main/docker/Dockerfile.native \
-t bolingcavalry/hello-quarkus:0.0.1 .
docker run -i --rm -p 8080:8080 bolingcavalry/hello-quarkus:0.0.1
mvn clean package -U -DskipTests
docker build \
-f src/main/docker/Dockerfile.jvm \
-t bolingcavalry/hello-quarkus-jar:0.0.1 .
docker run \
-i \
--rm \
-p 8080:8080 \
bolingcavalry/hello-quarkus-jar:0.0.1