Fedora - Using the Quarkus Framework on Fedora Silverblue – Just a Quick Look - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Computers (https://www.sickgaming.net/forum-86.html) +--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html) +--- Thread: Fedora - Using the Quarkus Framework on Fedora Silverblue – Just a Quick Look (/thread-93965.html) |
Fedora - Using the Quarkus Framework on Fedora Silverblue – Just a Quick Look - xSicKxBot - 03-11-2020 Using the Quarkus Framework on Fedora Silverblue – Just a Quick Look <div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/03/using-the-quarkus-framework-on-fedora-silverblue-just-a-quick-look.png" width="235" height="85" title="" alt="" /></div><div><p><a rel="noreferrer noopener" aria-label="Quarkus (opens in a new tab)" href="https://quarkus.io/" target="_blank">Quarkus</a> is a framework for Java development that is described on their web site as:</p> <div class="wp-block-group"> <div class="wp-block-group__inner-container"> <blockquote class="wp-block-quote"> <p>A Kubernetes Native Java stack tailored for OpenJDK HotSpot and GraalVM, crafted from the best of breed Java libraries and standards</p> <p><cite>https://quarkus.io/ – Feb. 5, 2020</cite></p></blockquote> </div> </div> <p>Silverblue — a Fedora Workstation variant with a container based workflow central to its functionality — should be an ideal host system for the Quarkus framework.</p> <p>There are currently two ways to use Quarkus with Silverblue. It can be run in a pet container such as Toolbox/Coretoolbox. Or it can be run directly in a terminal emulator. This article will focus on the latter method.</p> <h2>Why Quarkus</h2> <p><a rel="noreferrer noopener" aria-label="According to Quarkus.io (opens in a new tab)" href="https://quarkus.io/vision/container-first" target="_blank">According to Quarkus.io</a>: “Quarkus has been designed around a containers first philosophy. What this means in real terms is that Quarkus is optimized for low memory usage and fast startup times.” To achieve this, they employ first class support for Graal/Substrate VM, build time Metadata processing, reduction in reflection usage, and native image preboot. For details about why this matters, read <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://quarkus.io/vision/container-first" target="_blank">Container First</a> at Quarkus.</p> <h2>Prerequisites</h2> <p>A few prerequisites will need to configured before you can start using Quarkus. First, you need an IDE of your choice. Any of the popular ones will do. VIM or Emacs will work as well. The Quarkus site provides full details on how to set up the three major Java IDE’s (Eclipse, Intellij Idea, and Apache Netbeans). You will need a version of JDK installed. JDK 8, JDK 11 or any distribution of OpenJDK is fine. GrallVM 19.2.1 or 19.3.1 is needed for compiling down to native. You will also need Apache Maven 3.53+ or Gradle. This article will use Maven because that is what the author is more familiar with. Use the following command to layer Java 11 OpenJDK and Maven onto Silverblue:</p> <pre class="wp-block-preformatted">$ rpm-ostree install java-11-openjdk* maven</pre> <p>Alternatively, you can download your favorite version of Java and install it directly in your home directory.</p> <p>After rebooting, configure your <em>JAVA_HOME</em> and <em>PATH</em> environment variables to reference the new applications. Next, go to the <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://www.graalvm.org/downloads/" target="_blank">GraalVM download page</a>, and get GraalVM version 19.2.1 or version 19.3.1 for Java 11 OpenJDK. Install Graal as per the instructions provided. Basically, copy and decompress the archive into a directory under your home directory, then modify the <em>PATH</em> environment variable to include Graal. You use it as you would any JDK. So you can set it up as a platform in the IDE of your choice. Now is the time to setup the native image if you are going to use one. For more details on setting up your system to use Quarkus and the Quarkus native image, check out their <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://quarkus.io/get-started/" target="_blank">Getting Started tutorial</a>. With these parts installed and the environment setup, you can now try out Quarkus. </p> <h2>Bootstrapping</h2> <p>Quarkus recommends you create a project using the bootstrapping method. Below are some example commands entered into a terminal emulator in the Gnome shell on Silverblue. </p> <pre class="wp-block-preformatted">$ mvn io.quarkus:quarkus-maven-plugin:1.2.1.Final:create \ -DprojectGroupId=org.jakfrost \ -DprojectArtifactId=silverblue-logo \ -DclassName="org.jakfrost.quickstart.GreetingResource" \ -Dpath="/hello" $ cd silverblue-logo</pre> <p>The bootstrapping process shown above will create a project under the current directory with the name <em>silverblue-logo</em>. After this completes, start the application in development mode:</p> <pre class="wp-block-preformatted">$ ./mvnw compile quarkus:dev</pre> <p>With the application running, check whether it responds as expected by issuing the following command:</p> <pre class="wp-block-preformatted">$ curl -w '\n' http://localhost:8080/hello</pre> <p>The above command should print <em>hello</em> on the next line. Alternatively, test the application by browsing to <em>http://localhost:8080/hello</em> with your web browser. You should see the same lonely <em>hello</em> on an otherwise empty page. Leave the application running for the next section.</p> <h2>Injection</h2> <p>Open the project in your favorite IDE. If you are using Netbeans, simply open the project directory where the <em>pom.xml</em> file resides. Now would be a good time to have a look at the <em>pom.xml</em> file. </p> <p>Quarkus uses ArC for its dependency injection. ArC is a dependency of quarkus-resteasy, so it is already part of the core Quarkus installation. Add a companion bean to the project by creating a java class in your IDE called <em>GreetingService.java</em>. Then put the following code into it:</p> <pre class="wp-block-preformatted">import javax.enterprise.context.ApplicationScoped; @ApplicationScoped public class GreetingService { public String greeting(String name) { return "hello " + name; } }</pre> <p>The above code is a verbatim copy of what is used in the injection example in the Quarkus Getting Started tutorial. Modify <em>GreetingResource.java</em> by adding the following lines of code:</p> <pre class="wp-block-preformatted">import javax.inject.Inject; import org.jboss.resteasy.annotations.jaxrs.PathParam; @Inject GreetingService service;//inject the service @GET //add a getter to use the injected service @Produces(MediaType.TEXT_PLAIN) @Path("/greeting/{name}") public String greeting(@PathParam String name) { return service.greeting(name); }</pre> <p>If you haven’t stopped the application, it will be easy to see the effect of your changes. Just enter the following <em>curl</em> command:</p> <pre class="wp-block-preformatted">$ curl -w '\n' http://localhost:8080/hello/greeting/Silverblue</pre> <p>The above command should print <em>hello Silverblue</em> on the following line. The URL should work similarly in a web browser. There are two important things to note:</p> <ol> <li>The application was running and Quarkus detected the file changes on the fly.</li> <li>The injection of code into the app was very easy to perform.</li> </ol> <h2>The native image</h2> <p>Next, package your application as a native image that will work in a <em>podman</em> container. Exit the application by pressing <strong>CTRL-C</strong>. Then use the following command to package it:</p> <pre class="wp-block-preformatted">$ ./mvnw package -Pnative -Dquarkus.native.container-runtime=podman</pre> <p>Now, build the container:</p> <pre class="wp-block-preformatted">$ podman build -f src/main/docker/Dockerfile.native -t silverblue-logo/silverblue-logo</pre> <p>Now run it with the following:</p> <pre class="wp-block-preformatted">$ podman run -i --rm -p 8080:8080 localhost/silverblue-logo/silverblue-logo</pre> <p>To get the container build to successfully complete, it was necessary to copy the <em>/target</em> directory and contents into the <em>src/main/docker/</em> directory. Investigation as to the reason why is still required, and though the solution used was quick and easy, it is not an acceptable way to solve the problem. </p> <p>Now that you have the container running with the application inside, you can use the same methods as before to verify that it is working.</p> <p>Point your browser to the URL http://localhost:8080/ and you should get a <em>index.html</em> that is automatically generated by Quarkus every time you create or modify an application. It resides in the <em>src/main/resources/META-INF/resources/</em> directory. Drop other HTML files in this <em>resources</em> directory to have Quarkus serve them on request.</p> <p>For example, create a file named <em>logo.html</em> in the <em>resources</em> directory containing the below markup:</p> <pre class="wp-block-preformatted"><!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>Silverblue</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div> <img src="fedora-silverblue-logo.png" alt="Fedora Silverblue"/> </div> </body> </html></pre> <p>Next, save the below image alongside the <em>logo.html</em> file with the name <em>fedora-silverblue-logo.png</em>:</p> <p><img class="wp-image-30675" style="width: 150px" src="https://www.sickgaming.net/blog/wp-content/uploads/2020/03/using-the-quarkus-framework-on-fedora-silverblue-just-a-quick-look.png" alt="" /></p> <p>Now view the results at http://localhost:8080/logo.html.</p> <h3>Testing your application</h3> <p>Quarkus supports junit 5 tests. Look at your project’s <em>pom.xml</em> file. In it you should see two test dependencies. The generated project will contain a simple test, named <em>GreetingResourceTest.java</em>. Testing for the native file is only supported in <em>prod</em> mode. However, you can test the <em>jar</em> file in <em>dev</em> mode. These tests are RestAssured, but you can use whatever test library you wish with Quarkus. Use Maven to run the tests:</p> <pre class="wp-block-preformatted">$ ./mvnw test</pre> <p>More details can be found in the Quarkus <a href="https://quarkus.io/guides/getting-started" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Getting Started</a> tutorial.</p> <h3>Further reading and tutorials</h3> <p>Quarkus has an extensive collection of <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://quarkus.io/guides/" target="_blank">tutorials and guides</a>. They are well worth the time to delve into the breadth of this microservices framework.</p> <p>Quarkus also maintains a <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://quarkus.io/publications/" target="_blank">publications</a> page that lists some very interesting articles on actual use cases of Quarkus. This article has only just scratched the surface of the topic. If what was presented here has piqued your interest, then follow the above links for more information.</p> </div> https://www.sickgaming.net/blog/2020/03/11/using-the-quarkus-framework-on-fedora-silverblue-just-a-quick-look/ |