有关RedHat的Quarkus的优势就不详细介绍了,总归就是通过将Java应用编译成可执行文件,可以让应用启动超快,占内存超少。非常适合企业Java应用的微服务化和大规模部署,另外在Serverless等场景也非常适合。
本文基于quarkus.io的quarkus-quickstarts示例,说明如何实现一个最简单的Quarkus应用,并将其以可执行文件的形态部署到OpenShift上运行。
$ export GRAALVM_VERSION=19.3.1
$ export JAVA_VERSION=java11
$ curl -L https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-$GRAALVM_VERSION/graalvm-ce-$JAVA_VERSION-linux-amd64-$GRAALVM_VERSION.tar.gz -o graalvm.tar.gz
$ tar xvf graalvm.tar.gz
$ mv ./graalvm-ce-$JAVA_VERSION-$GRAALVM_VERSION ~/graalvm
$ export JAVA_HOME=~/graalvm
$ export PATH=./:$JAVA_HOME/bin/:$PATH
$ gu install native-image
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image
Downloading: Component native-image: Native Image from github.com
Installing new component: Native Image (org.graalvm.native-image, version 19.3.1)
$ gu list
ComponentId Version Component name Origin
--------------------------------------------------------------------------------
graalvm 19.3.1 GraalVM Core
native-image 19.3.1 Native Image github.com
$ java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment GraalVM CE 19.3.1 (build 11.0.6+9-jvmci-19.3-b07)
OpenJDK 64-Bit Server VM GraalVM CE 19.3.1 (build 11.0.6+9-jvmci-19.3-b07, mixed mode, sharing)
$ MAVEN_VERSION=3.6.3
$ curl -L http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz -o maven.tar.gz
$ tar xvf maven.tar.gz
$ mv ./apache-maven-$MAVEN_VERSION ~/maven
$ export MAVEN_HOME=~/maven
$ export PATH=$PATH:$MAVEN_HOME/bin/
$ mvn -Dplugin=io.quarkus:quarkus-maven-plugin help:describe
$ sudo yum -y install gcc glibc-devel zlib-devel libstdc++-static
$ mvn io.quarkus:quarkus-maven-plugin:1.2.0.Final:create \
-DprojectGroupId=com.example \
-DprojectArtifactId=hello \
-DclassName="com.example.quarkus.Greeting" \
-Dpath="/hello"
$ cd hello
$ mvnw quarkus:dev
。。。
2020-01-21 11:55:57,142 INFO [io.quarkus] (main) Quarkus 1.1.1.Final started in 2.453s. Listening on: http://0.0.0.0:8080
2020-01-21 11:55:57,183 INFO [io.quarkus] (main) Profile dev activated. Live Coding activated.
2020-01-21 11:55:57,183 INFO [io.quarkus] (main) Installed features: [cdi, resteasy]
$ curl -w "\n" http://localhost:8080/hello
hello
$ mvnw test
。。。
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
。。。
[...]
@Path("/hello")
public class Greeting {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello world";
}
}
。。。
2020-01-22 09:31:41,174 INFO [io.qua.dev] (vert.x-worker-thread-4) Changed source files detected, recompiling [/home/xiaoyliu-redhat.com/lab/hello/src/main/java/com/example/quarkus/Greeting.java]
2020-01-22 09:31:41,378 INFO [io.quarkus] (vert.x-worker-thread-4) Quarkus stopped in 0.007s
2020-01-22 09:31:41,743 INFO [io.quarkus] (vert.x-worker-thread-4) hello 1.0-SNAPSHOT (running on Quarkus 1.1.1.Final) started in 0.364s. Listening on: http://0.0.0.0:8080
2020-01-22 09:31:41,743 INFO [io.quarkus] (vert.x-worker-thread-4) Profile dev activated. Live Coding activated.
2020-01-22 09:31:41,743 INFO [io.quarkus] (vert.x-worker-thread-4) Installed features: [cdi, resteasy]
2020-01-22 09:31:41,743 INFO [io.qua.dev] (vert.x-worker-thread-4) Hot replace total time: 0.570s
$ curl -w "\n" http://localhost:8080/hello
hello world
$ mvnw package
$ java -jar target/hello-1.0-SNAPSHOT-runner.jar
$ mvnw package -Pnative
$ target/hello-1.0-SNAPSHOT-runner
$ oc new-project quarkus
$ oc new-build quay.io/quarkus/ubi-quarkus-native-binary-s2i:19.2.1 --binary --name=hello -l app=hello
$ oc start-build hello --from-file=target/hello-1.0-SNAPSHOT-runner --follow
$ oc new-app hello
$ oc expose service hello
$ oc rollout status -w dc/hello
$ curl $(oc get route hello -o template --template '{{.spec.host}}')/hello
hello