前言
Quarkus框架是一个redhat 公司开源的项目,定位为GraalVM和OpenJDK HotSpot量身定制的一个Kurbernetes Native Java框架。
安装graalvm
下载地址:graalvm官网
有两个版本,分别以java8/java11 进行构建,这里选择java11版本进行构建,下载下来然后解压缩 注意 我这里选择的 2.71Final版本需要maven 较新版本,我这里选择的是Apache Maven 3.8.4
配置环境变量
将GRAALVM_HOME和JAVA_HOME环境变量都设置到graalvm的根目录,这里的配置是通过命令可以调整的,方便编写程序
export JAVA_11_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.14.jdk/Contents/Home
export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
export GRAALVM_HOME=/xxx/Documents/graalvm-ce-java11-21.3.0/Contents/Home
#默认JDK1.7
export JAVA_HOME=$JAVA_8_HOME
#alias命令动态切换JDK版本 和path地址
alias jdk11="export JAVA_HOME=$JAVA_11_HOME"
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias gra11="export JAVA_HOME=$GRAALVM_HOME"
alias grapath=PATH=/xxx/Documents/graalvm-ce-java11-21.3.0/Contents/Home/bin:$PATH
安装完了 调用命令可以查看系统配置成功与否:
$ gu -list
$ java -version
安装 native-image
使用gu命令下载native-image 模块
$ 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
初始化 Quarkus框架
一种方式在code.quarkus.io/ 网站上可以初始化项目,初始化完了可以直接下载zip包打开。
还有一种方式可以手动配置maven
<groupId>org.test</groupId>
<artifactId>org.test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus.version>2.7.1.Final</quarkus.version>
<maven.home>/Users/xuejiameng/Documents/maven/apache-maven-3.8.4</maven.home>
<surefire-plugin.version>3.0.0-M3</surefire-plugin.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>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</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>
配置完了以后可以写个简单的hello程序测试
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
在文件夹下输入命令
$ mvn quarkus:dev
可以看到运行的结果
native-image 打包
我们刚刚已经在gu里面安装了native-image模块,现在可以尝试用native-image打包程序 首先在pom里面加入配置
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
等待安装架包完成以后在文件夹中输入命令:$ mvn package -Pnative
等待一段较长的时间以后打包完成:
然后可以在target里面看到可以直接运行的的runner程序,名字为[project name]-runner
直接输入命令运行./org.test-1.0-SNAPSHOT-runner
运行的结果都是一样的。
总结
Quarkus应用的最大优势应该就在于Pnative编译以后启动速度较spring等框架大大加强,大概能快个几百倍,还有就是对于graalvm以及相对应的对于容器和云原生的支持。