java云原生系列课程,quarkus入门体验

文章目录

  • 前言
  • 一、quarkus是什么?
  • 二、使用步骤
    • 1.使用idea创建quarkus项目
    • 3.编译镜像
  • 总结


前言

一、quarkus是什么?

为 OpenJDK HotSpot 和 GraalVM 量身定制的 Kubernetes 原生 Java 堆栈,由最好的 Java 库和标准制作而成。
有几大特性,这是官网介绍
java云原生系列课程,quarkus入门体验_第1张图片
下面开始入门教程

二、使用步骤

1.使用idea创建quarkus项目

java云原生系列课程,quarkus入门体验_第2张图片
java云原生系列课程,quarkus入门体验_第3张图片
创建完成后相关依赖都会自动生成


<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://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.0modelVersion>
    <groupId>com.examplegroupId>
    <artifactId>testartifactId>
    <version>1.0-SNAPSHOTversion>
    <properties>
        <compiler-plugin.version>3.8.1compiler-plugin.version>
        <failsafe.useModulePath>falsefailsafe.useModulePath>
        <maven.compiler.release>11maven.compiler.release>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bomquarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platformquarkus.platform.group-id>
        <quarkus.platform.version>2.7.1.Finalquarkus.platform.version>
        <surefire-plugin.version>3.0.0-M5surefire-plugin.version>
    properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}groupId>
                <artifactId>${quarkus.platform.artifact-id}artifactId>
                <version>${quarkus.platform.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>io.quarkusgroupId>
            <artifactId>quarkus-resteasy-jacksonartifactId>
        dependency>
        <dependency>
            <groupId>io.quarkusgroupId>
            <artifactId>quarkus-arcartifactId>
        dependency>
        <dependency>
            <groupId>io.quarkusgroupId>
            <artifactId>quarkus-junit5artifactId>
            <scope>testscope>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>${quarkus.platform.group-id}groupId>
                <artifactId>quarkus-maven-pluginartifactId>
                <version>${quarkus.platform.version}version>
                <extensions>trueextensions>
                <executions>
                    <execution>
                        <goals>
                            <goal>buildgoal>
                            <goal>generate-codegoal>
                            <goal>generate-code-testsgoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <artifactId>maven-compiler-pluginartifactId>
                <version>${compiler-plugin.version}version>
                <configuration>
                    <compilerArgs>
                        <arg>-parametersarg>
                    compilerArgs>
                configuration>
            plugin>
            <plugin>
                <artifactId>maven-surefire-pluginartifactId>
                <version>${surefire-plugin.version}version>
                <configuration>
                    <systemPropertyVariables>
                        <java.util.logging.manager>org.jboss.logmanager.LogManagerjava.util.logging.manager>
                        <maven.home>${maven.home}maven.home>
                    systemPropertyVariables>
                configuration>
            plugin>
        plugins>
    build>
    <profiles>
        <profile>
            <id>nativeid>
            <activation>
                <property>
                    <name>nativename>
                property>
            activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-failsafe-pluginartifactId>
                        <version>${surefire-plugin.version}version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-testgoal>
                                    <goal>verifygoal>
                                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>nativequarkus.package.type>
            properties>
        profile>
    profiles>
project>

然后我们简单编写个接口,这里按照的是官网的demo

public class Fruit {
    public String name;
    public String description;

    public Fruit() {
    }

    public Fruit(String name, String description) {
        this.name = name;
        this.description = description;
    }
}
@Path("/fruits")
public class FruitResource {

    private Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));

    public FruitResource() {
        fruits.add(new Fruit("Apple", "Winter fruit"));
        fruits.add(new Fruit("Pineapple", "Tropical fruit"));
    }

    @GET
    public Set<Fruit> list() {
        return fruits;
    }

    @POST
    public Set<Fruit> add(Fruit fruit) {
        fruits.add(fruit);
        return fruits;
    }

    @DELETE
    public Set<Fruit> delete(Fruit fruit) {
        fruits.removeIf(existingFruit -> existingFruit.name.contentEquals(fruit.name));
        return fruits;
    }
}

修改运行端口,也就是修改application.properties文件内容

quarkus.http.port=8088
quarkus.http.root-path=/

编写完成后使用idea右侧已jar包编译测试
java云原生系列课程,quarkus入门体验_第4张图片
然后启动成功
java云原生系列课程,quarkus入门体验_第5张图片
访问java云原生系列课程,quarkus入门体验_第6张图片
看到这个说明测试成功

3.编译镜像

这里我采用了docker阶段式编译,省去了安装graalvm繁琐的过程,这里我是按照官方文档来的
首先在docker文件夹下编辑新的dockerfile命名为Dockerfile.multistage,编辑内容为

## Stage 1 : build with maven builder image with native capabilities
FROM quay.io/quarkus/ubi-quarkus-native-image:21.3.1-java11 AS build
COPY --chown=quarkus:quarkus mvnw /code/mvnw
COPY --chown=quarkus:quarkus .mvn /code/.mvn
COPY --chown=quarkus:quarkus pom.xml /code/
USER quarkus
WORKDIR /code
RUN ./mvnw -B org.apache.maven.plugins:maven-dependency-plugin:3.1.2:go-offline
COPY src /code/src
RUN ./mvnw package -Pnative

## Stage 2 : create the docker final image
FROM quay.io/quarkus/quarkus-micro-image:1.0
WORKDIR /work/
COPY --from=build /code/target/*-runner /work/application

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
  && chown -R 1001 /work \
  && chmod -R "g+rwX" /work \
  && chown -R 1001:root /work

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

然后把项目文件夹弄到有docker运行环境的机器上
cd到项目目录下,运行

docker build -f src/main/docker/Dockerfile.multistage -t quarkus-quickstart/getting-started .

期间要下载各种依赖,打包。等了大概20分钟,最后
java云原生系列课程,quarkus入门体验_第7张图片java云原生系列课程,quarkus入门体验_第8张图片
打包成功
运行命令

docker run -i --rm -p 8080:8080 quarkus-quickstart/getting-started

在这里插入图片描述
成功启动
看打印日志启动只用了0.033s,确实是快,内存占用也就几十兆,对于用jvm的springboot项目起步两三百兆这个确实省了不少资源

总结

以上就是今天要讲的内容,本文仅仅简单介绍了quarkus的使用,算是初步体验quarkus了

你可能感兴趣的:(java,linux,docker,java,云原生,quarkus,kubernetes,docker)