クォーツを使用して、残りのAPIを設定する方法.io


なぜクォークは良い選択ですか?


QuarkusはJavaのための最良のフレームワークの一つです!最初に、Willy Swarmと呼ばれるマイクロサービスフォーカスプロジェクトになった実験として、Wildflyがありました.その後、Willy SwarmはThorntailに改名されました.Thorntailの主な目的は、マイクロサービス用のジャカルタEE実装ビルドを構築することでした.しかし、コードの完全な書き換えを必要とした落とし穴がありました.したがって、これはQualkus、軽い実装、マイクロサービスの準備ができて、それはGraalVMにネイティブの支持をします.

だからクォークは基づいているlessons learned 前の開発から.これらのレッスンに基づいて、私はこれがより良いフレームワーク、使いやすくて、実行に関して速いと思っています.

Mangling artifacts is dangerous
When you mangle and repackage a user’s artifacts and dependencies, it can many times go awry.

Don’t replace Maven
Let Maven (or Gradle) handle the entirety of pulling dependencies. We cannot predict the topology of someone’s repository managers, proxies and network.

Don’t get complicated with uberjars
The more complex our uberjar layout is, the harder it is to support Gradle or other non-Maven build systems.

Classpaths are tricky
If different codepaths are required for executing from Maven, an IDE, a unit-test, and during production, you will have a bad time.

Don’t insist on uberjars
For Linux containers, people want layers that cleanly separate application code from runtime support code.

Testability is important
A slow test is a test that is never willingly executed. PRs take forever to validate. Users like to be able to test their own code quickly and iteratively.

Easily extensible means ecosystem
If it’s entirely too difficult to extend the platform, the ecosystem will not grow. New integrations should be simple.

Related: Core things should not be any more first-class than community contributions
For instance, auto-detection in WildFly Swarm only worked with core fractions; user-provided wouldn’t auto-detect.

Ensure the public-vs-private API guarantees are clear.
Intertwingly code (and javadocs) make finding the delineation between public API and private implementations difficult.

Allow BYO components
We don’t want to decide all of the implementations, and certainly not versions, of random components we support.

Be a framework, not a platform
Frameworks are easier to integrate into an existing app; a platform becomes the target with (generally too many) constraints.

Maintain tests & documentation
Ensure the definition of "done" includes both tests and documentation.

Productization complexity
The greater divergence between community and product, the more effort is required for productization. Complicating any process to automate productization from community.

BOM complexity
Related to productization as well, but of itself having a handful of BOMs made life confusing for us and for users. There were often times where fractions would be "Unstable" or "Experimental" for months with no real reason other than we forgot to update it.


設定クォーク


我々がチュートリアルの上で答える必要がある最初の質問は、以下です:Qualkusを使ってプロジェクトを構築するために、あなたは何を必要としますか?
クォークについては、
  • 依存関係を追加する
  • パッケージの構成
  • コーディングの開始
  • 依存性の設定


    QualkusはジャカルタEEですので、コードにJakarta EE注釈を使用します.しかし、pom.xml QualkusがGraalVMをネイティブにサポートしているので、クォーク依存性を指摘すべきです.
    まず、すべての依存関係をクォートに追加する必要があります.
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-universe-bom</artifactId>
                <version>1.9.2.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    そのプロジェクトに必要なのは、
  • APIを作成
  • JSONサポートの追加
  • 反応支持を加える
  • そのためには、以下の依存関係が必要になります.
  • io.quarkus:io.quarkus REST APIの作成
  • io.quarkus:quarkus-resteasy-jsonb JSONシリアライザーをREST APIに追加するには
  • io.quarkus:quarkus-resteasy-mutiny REST APIに対するリアクティブサポートの追加
  • ビルドの設定


    次のステップはクォーツビルドを設定する必要があります.私たちが知っているように、Qualkusはすべての依存関係で脂肪の瓶を作成します.
    MavenのQuarkusビルダーを有効にするには、次のプラグインを追加します.
    <plugin>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-maven-plugin</artifactId>
        <version>1.9.2.Final</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate-code</goal>
                    <goal>generate-code-tests</goal>
                    <goal>build</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    この例では、Java 11としてコンパイルしていますが、Java 15をテストしています.それは11より新しいJavaの任意のバージョンのために動作します.Java 8で実行する必要がある場合は、コンパイラオプションを変更します.
    ビルドを実行するだけです.
    mvn clean package
    
    これは2つを作成しますjars ターゲットフォルダの中で-runner.jar 依存性なしで実行できます.
    $ java -jar target\quarkus-tutorial-runner.jar
    __  ____  __  _____   ___  __ ____  ______
     --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
     -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
    --\___\_\____/_/ |_/_/|_/_/|_|\____/___/
    2020-11-09 11:16:53,416 INFO  [io.quarkus] (main) quarkus-tutorial 0.0.1-SNAPSHOT on JVM (powered by Quarkus 1.9.2.Final) started in 4.706s. Listening on: http://0.0.0.0:8080
    2020-11-09 11:16:53,470 INFO  [io.quarkus] (main) Profile prod activated.
    2020-11-09 11:16:53,475 INFO  [io.quarkus] (main) Installed features: [cdi, mutiny, resteasy, resteasy-jsonb, resteasy-mutiny, smallrye-context-propagation]
    2020-11-09 11:16:58,790 INFO  [io.quarkus] (Shutdown thread) quarkus-tutorial stopped in 0.024s
    
    これは、我々が生産環境のために実行するべき方法です、開発のために、我々はQualkus Mavenプラグインを使用することができます.既に実行中のサーバーで変更を展開します.
    mvn quarkus:dev
    

    残りAPI APIの追加


    APIを作成するための最新の手順は、要求を処理するコードを作成することです.JAX - RSを使用すると簡単です.クラスを作成し、注釈を追加します.
    最も簡単な例は
    @Path("/hello")
    @ApplicationScoped
    public class HelloEndpoint {
        @GET
        public String sayHello() {
            return "Hello World!";
        }
    }
    
    JAX - RSはこのメソッドによって返されたオブジェクトのJSON表現を自動的に生成します.
    @Path("/hello")
    @ApplicationScoped
    public class HelloEndpoint {
        private HelloResponse generateResponse() {
            HelloResponse response = new HelloResponse();
            response.setCode(new Random().nextInt());
            response.setMessage("Hello World!");
            return response;
        }
    
        @GET
        @Path("/json")
        @Produces(MediaType.APPLICATION_JSON)
        public HelloResponse sayHelloWithJson() {
            return generateResponse();
        }
    }
    
    Qualkusも反応プログラミングをサポートしている.JAX - RSの場合、あなたはUni またはCompletableFuture .
    @Path("/hello")
    @ApplicationScoped
    public class HelloEndpoint {
        private HelloResponse generateResponse() {
            HelloResponse response = new HelloResponse();
            response.setCode(new Random().nextInt());
            response.setMessage("Hello World!");
            return response;
        }
    
        @GET
        @Path("/json/reactive")
        @Produces(MediaType.APPLICATION_JSON)
        public Uni<HelloResponse> sayHelloWithJsonReactively() {
            return Uni.createFrom().item(this::generateResponse);
        }
    }
    

    結論


    JAKA - RSはジャカルタEE仕様であるため、他の既存の実装にいくつかの変更を加えてコードを移行することができますが、Qualkusはより軽い実装です.
    Qualkusは良い選択です!
    すべての例を見つけることができますgithub.com/vepo/quarkus-tutorial

    Vepo / クォークチュートリアル


    これは私がQuakusのチュートリアルを作成するブログの記事のシリーズです。入出力


    クォークチュートリアル


    ステップ


    1 . REST APIを作成する


    More information
    最初の例では、クォークカスとJAX - RSを使用して、最小のREST APIを作成します.

    JPAジャカルタ持続性の設定


    More information
    番目の例では、残りのAPIの持続性レイヤーを追加します.

    Jakarta Beanの検証


    More information
    番目の例では、残りのAPIのすべてのレイヤーに妥当性検査を追加します.
    View on GitHub