Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Microservices with Micronaut®

You're reading from   Building Microservices with Micronaut® A quick-start guide to building high-performance reactive microservices for Java developers

Arrow left icon
Product type Paperback
Published in Sep 2021
Publisher Packt
ISBN-13 9781800564237
Length 362 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Zack Dawood Zack Dawood
Author Profile Icon Zack Dawood
Zack Dawood
Nirmal Singh Nirmal Singh
Author Profile Icon Nirmal Singh
Nirmal Singh
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Section 1: Core Concepts and Basics
2. Chapter 1: Getting Started with Microservices Using the Micronaut Framework FREE CHAPTER 3. Section 2: Microservices Development
4. Chapter 2: Working on Data Access 5. Chapter 3: Working on RESTful Web Services 6. Chapter 4: Securing the Microservices 7. Chapter 5: Integrating Microservices Using Event-Driven Architecture 8. Section 3: Microservices Testing
9. Chapter 6: Testing Microservices 10. Section 4: Microservices Deployment
11. Chapter 7: Handling Microservice Concerns 12. Chapter 8: Deploying Microservices 13. Section 5: Microservices Maintenance
14. Chapter 9: Distributed Logging, Tracing, and Monitoring 15. Section 6: IoT with Micronaut and Closure
16. Chapter 10: IoT with Micronaut 17. Chapter 11: Building Enterprise-Grade Microservices 18. Assessment 19. Other Books You May Enjoy

Working on a hello world project in the Micronaut framework

To understand the practical aspects of using the Micronaut framework for developing a microservice, we will work with a hello world project. This will help you quickly get started with the Micronaut framework and also give you first-hand experience of how easy it is to do microservices development.

Micronaut works seamlessly with the Maven and Gradle packaging managers. We will cover one example for each using the Micronaut CLI as well as Micronaut Launch (web interface) for generating barebones projects.

Creating a hello world project using the Micronaut CLI

Please take the following steps to create a hello world application using the Micronaut CLI:

  1. Open the terminal (or Command Prompt).
  2. Change the directory to your desired directory where you want to create the hello world project.
  3. Type the following command:
    mn create-app hello-world-maven --build maven
  4. Wait for the Micronaut CLI to finish and it will create a hello-world-maven project. The create-app command will create a boilerplate project for you with a Maven build and your system-installed Java version. It will create Application.java as well as a sample test class called ApplicationTest.java.
  5. To explore your freshly created hello-world-maven project, open this project in your preferred IDE.
  6. To run your project, run the following command in a Bash terminal:
    ./mvnw compile exec:exec

    Please note that if you are copying the project from somewhere else, then it's required to regenerate mvnw by typing the following command:

    mvn -N io.takari:maven:wrapper
  7. The Maven wrapper will build and run your project on http://localhost:8080 by default.

Adding HelloWorldController

To create a simple endpoint, let's add a simple controller to the hello-world-maven project:

  1. Add a web package to our hello-world-maven project.
  2. Add a HelloWorldController Java class. It will contain a simple hello endpoint:
    @Controller("/hello")
    public class HelloController {
        @Get("/")
        @Produces(MediaType.TEXT_PLAIN)
        public String helloMicronaut() {
            return "Hello, Micronaut!";
        }
    }

    HelloController is accessible on the …/hello path. helloMicronaut() will generate a plain text "Hello, Micronaut!" message.

  3. Rerun your application and hit http://localhost:8080/hello/ in a browser window. The server will return the following response:
Figure 1.9 – Hello, Micronaut!

Figure 1.9 – Hello, Micronaut!

By default, the application will be accessible on port 8080, and this port can be changed in the application properties.

So far, we have worked on a hello world project using the Micronaut CLI. Next, we will explore Micronaut Launch, which is a web interface, to generate a boilerplate project.

Creating a hello world project using Micronaut Launch

Micronaut Launch (https://micronaut.io/launch/) is an intuitive web interface that came into existence with Micronaut 2.0.1. We can use this interface to quickly generate boilerplate for different kinds of Micronaut applications (such as server applications, the CLI, serverless functions, a messaging application, and so on). Let's quickly use this to generate a hello world application for us.

Please follow these instructions to generate the hello world project using the Micronaut Launch web interface:

  1. Open Micronaut Launch in a browser window: https://micronaut.io/launch/.
  2. Under Application Type, choose Application.
  3. Under Micronaut Version, choose 2.0.1.
  4. For the Java version, choose Java 14.
  5. For Language, choose Java.
  6. Give a base package name such as com.packtpub.micronaut.
  7. Choose Gradle as the build option.
  8. Give a name to the application, such as hello-world-gradle.
  9. Choose JUnit as the testing framework
  10. After you've finished choosing all the options, click on GENERATE PROJECT.

After choosing the preceding options and providing various inputs, the Micronaut Launch interface should look as follows:

Figure 1.10 – Using Micronaut Launch to generate a boilerplate project

Figure 1.10 – Using Micronaut Launch to generate a boilerplate project

Your project boilerplate source code will be generated into a zipped file. You can unarchive this zipped file into your desired directory and open it in your preferred IDE. Just like the previous example (hello-world-maven), we can add a basic HelloWorldController instance.

To run your project, run the following command in a Bash terminal:

gradlew.bat run

When the project is running, go to http://localhost:8080/hello and you should see the Hello, Micronaut! message in the browser tab.

In this section, we explored how to get started with the Micronaut framework by developing small hello world projects using the Micronaut CLI as well as the Micronaut Launch user interface. This small exercise will be a good preface for what we will cover in the next chapter.

You have been reading a chapter from
Building Microservices with Micronaut®
Published in: Sep 2021
Publisher: Packt
ISBN-13: 9781800564237
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image