Eureka and the Micronaut Framework - Microservices Service Discovery
Use Netflix Eureka service discovery to expose your Micronaut applications.
Authors: Sergio del Amo
Micronaut Version: 4.6.3
1. Getting started
In this guide, we will create three microservices and register them with Netflix Eureka service discovery. You will discover how the Micronaut framework eases Eureka integration.
2. What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOME
configured appropriately
3. Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
4. Writing the Application
Let’s describe the microservices:
-
bookcatalogue
- It returns a list of books. It uses a domain consisting of a book name and ISBN. -
bookinventory
- It exposes an endpoint to check whether a book has sufficient stock to fulfil an order. It uses a domain consisting of a stock level and ISBN. -
bookrecommendation
- It consumes previous services and exposes an endpoint which recommends book names which are in stock.
Initially we will hard-code the addresses where the different services are in the bookcatalogue
service.
As shown in the previous image, the bookcatalogue
hardcodes references to its collaborators.
In the second part of this guide we will use a discovery service.
The services register when they start up:
When a service wants to do a request to other service, it uses the discovery service to retrieve the address.
4.1. Catalogue Microservice
Create the bookcatalogue
microservice using the Micronaut Command Line Interface or with Micronaut Launch.
mn create-app --features=discovery-eureka,graalvm example.micronaut.bookcatalogue --build=maven --lang=kotlin
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
If you use Micronaut Launch, select Micronaut Application as application type and add the discovery-eureka
and graalvm
features.
The previous command creates a directory named bookcatalogue
and a Micronaut application inside it with default package example.micronaut
.
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
Create a BooksController
class to handle incoming HTTP requests into the bookcatalogue
microservice:
package example.micronaut
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
@Controller("/books") (1)
class BooksController {
@Get (2)
fun index(): List<Book> = listOf(
Book("1491950358", "Building Microservices"),
Book("1680502395", "Release It!"),
Book("0321601912", "Continuous Delivery:"))
}
1 | The class is defined as a controller with the @Controller annotation mapped to the path /books . |
2 | The @Get annotation maps the index method to /books requests that use an HTTP GET. |
The previous controller responds a List<Book>
. Create the Book
POJO:
package example.micronaut
import io.micronaut.serde.annotation.Serdeable
import jakarta.validation.constraints.NotBlank
@Serdeable
data class Book(@NotBlank val isbn: String,
@NotBlank val name: String)
Write a test:
package example.micronaut
import io.micronaut.core.type.Argument
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
@MicronautTest (1)
class BooksControllerTest(@Client("/") val client: HttpClient) { (2)
@Test
fun testRetrieveBooks() {
val request: HttpRequest<*> = HttpRequest.GET<Any>("/books") (3)
val books = client.toBlocking().retrieve(request, Argument.listOf(Book::class.java)) (4)
assertEquals(3, books.size)
assertTrue(books.contains(Book("1491950358", "Building Microservices")))
assertTrue(books.contains(Book("1680502395", "Release It!")))
}
}
1 | Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info. |
2 | Inject the HttpClient bean and point it to the embedded server. |
3 | Creating HTTP Requests is easy thanks to the Micronaut framework fluid API. |
4 | Parse easily JSON into Java objects. |
Edit application.yml
micronaut:
application:
name: bookcatalogue (1)
1 | Configure the application name. The name will be use by the discovery service. |
Modify the Application
class to use dev
as a default environment:
The Micronaut framework supports the concept of one or many default environments. A default environment is one that is only applied if no other environments are explicitly specified or deduced.
package example.micronaut
import io.micronaut.context.env.Environment.DEVELOPMENT
import io.micronaut.runtime.Micronaut.build
fun main(args: Array<String>) {
build()
.args(*args)
.packages("example.micronaut")
.defaultEnvironments(DEVELOPMENT)
.start()
}
Create src/main/resources/application-dev.yml
. The Micronaut framework applies this configuration file only for the dev
environment.
micronaut:
server:
port: 8081 (1)
1 | Configure the application to listen on port 8081 |
Create a file named application-test.yml
which is used in the test environment:
eureka:
client:
registration:
enabled: false
Run the unit test:
./mvnw test
4.2. Inventory Microservice
Create the bookinventory
microservice using the Micronaut Command Line Interface or with Micronaut Launch.
mn create-app --features=discovery-eureka,graalvm example.micronaut.bookinventory --build=maven --lang=kotlin
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
If you use Micronaut Launch, select Micronaut Application as application type and add the discovery-eureka
and graalvm
features.
The previous command creates a directory named bookinventory
and a Micronaut application inside it with default package example.micronaut
.
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
Create a Controller:
package example.micronaut
import io.micronaut.http.MediaType.TEXT_PLAIN
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import java.util.Optional
import jakarta.validation.constraints.NotBlank
@Controller("/books") (1)
open class BooksController {
@Produces(TEXT_PLAIN) (2)
@Get("/stock/{isbn}") (3)
open fun stock(@NotBlank isbn: String): Boolean? =
bookInventoryByIsbn(isbn).map { (_, stock) -> stock > 0 }.orElse(null)
private fun bookInventoryByIsbn(isbn: String): Optional<BookInventory> {
if (isbn == "1491950358") {
return Optional.of(BookInventory(isbn, 4))
}
if (isbn == "1680502395") {
return Optional.of(BookInventory(isbn, 0))
}
return Optional.empty()
}
}
1 | The class is defined as a controller with the @Controller annotation mapped to the path /books . |
2 | By default a controller response uses application/json as Content-Type . We are returning a String, not a JSON object. Because of that, we set it to text/plain . |
3 | The @Get annotation maps the index method to /books/stock/{isbn} requests that use an HTTP GET. |
The previous controller uses a POJO:
package example.micronaut
import io.micronaut.serde.annotation.Serdeable
import jakarta.validation.constraints.NotBlank
@Serdeable
data class BookInventory(@NotBlank val isbn: String,
val stock: Int)
Write a test:
package example.micronaut
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpStatus.NOT_FOUND
import io.micronaut.http.HttpStatus.OK
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
@MicronautTest
class BooksControllerTest(@Client("/") val httpClient: HttpClient) {
@Test
fun testBooksController() {
val rsp = httpClient.toBlocking().exchange(
HttpRequest.GET<Any>("/books/stock/1491950358"), Boolean::class.java)
assertEquals(OK, rsp.status())
assertTrue(rsp.body())
}
@Test
fun testBooksControllerWithNonExistingIsbn() {
val thrown = assertThrows(HttpClientResponseException::class.java) {
httpClient.toBlocking().exchange(HttpRequest.GET<Any>("/books/stock/XXXXX"), Boolean::class.java)
}
assertEquals(NOT_FOUND, thrown.response.status)
}
}
Edit application.yml
micronaut:
application:
name: bookinventory (1)
1 | Configure the application name. The name will be used later in the guide. |
Modify the Application
class to use dev
as a default environment:
The Micronaut framework supports the concept of one or many default environments. A default environment is one that is only applied if no other environments are explicitly specified or deduced.
package example.micronaut
import io.micronaut.context.env.Environment.DEVELOPMENT
import io.micronaut.runtime.Micronaut.build
fun main(args: Array<String>) {
build()
.args(*args)
.packages("example.micronaut")
.defaultEnvironments(DEVELOPMENT)
.start()
}
Create src/main/resources/application-dev.yml
. The Micronaut framework applies this configuration file only for the dev
environment.
micronaut:
server:
port: 8082 (1)
1 | Configure the application to listen on port 8082 |
Create a file named application-test.yml
which is used in the test environment:
eureka:
client:
registration:
enabled: false
Run the unit test:
./mvnw test
4.3. Recommendation Microservice
Create the bookrecommendation
microservice using the Micronaut Command Line Interface or with Micronaut Launch.
mn create-app --features=discovery-eureka,reactor,graalvm example.micronaut.bookrecommendation --build=maven --lang=kotlin
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
If you use Micronaut Launch, select Micronaut Application as application type and add the discovery-eureka
, reactor
, and graalvm
features.
The previous command creates a directory named bookrecommendation
and a Micronaut application inside it with default package example.micronaut
.
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
Create an interface to map operations with bookcatalogue
, and a Micronaut Declarative HTTP Client to consume it.
package example.micronaut
import org.reactivestreams.Publisher
interface BookCatalogueOperations {
fun findAll(): Publisher<Book>
}
package example.micronaut
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.retry.annotation.Recoverable
import org.reactivestreams.Publisher
@Client("http://localhost:8081") (1)
@Recoverable(api = BookCatalogueOperations::class)
interface BookCatalogueClient : BookCatalogueOperations {
@Get("/books")
override fun findAll(): Publisher<Book>
}
1 | Use @Client to use declarative HTTP Clients. You can annotate interfaces or abstract classes. You can use the id member to provide a service identifier or specify the URL directly as the annotation’s value. |
The client returns a POJO. Create it in the bookrecommendation
:
package example.micronaut
import io.micronaut.serde.annotation.Serdeable
import jakarta.validation.constraints.NotBlank
@Serdeable
data class Book(@NotBlank val isbn: String,
@NotBlank val name: String)
Create an interface to map operations with bookinventory
, and a Micronaut Declarative HTTP Client to consume it.
package example.micronaut
import reactor.core.publisher.Mono
import jakarta.validation.constraints.NotBlank
interface BookInventoryOperations {
fun stock(@NotBlank isbn: String): Mono<Boolean>
}
package example.micronaut
import io.micronaut.http.MediaType.TEXT_PLAIN
import io.micronaut.http.annotation.Consumes
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.retry.annotation.Recoverable
import reactor.core.publisher.Mono
import jakarta.validation.constraints.NotBlank
@Client("http://localhost:8082") (1)
@Recoverable(api = BookInventoryOperations::class)
interface BookInventoryClient : BookInventoryOperations {
@Consumes(TEXT_PLAIN)
@Get("/books/stock/{isbn}")
override fun stock(@NotBlank isbn: String): Mono<Boolean>
}
1 | Use @Client to use declarative HTTP Clients. You can annotate interfaces or abstract classes. You can use the id member to provide a service identifier or specify the URL directly as the annotation’s value. |
Create a Controller which injects both clients.
package example.micronaut
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
@Controller("/books") (1)
class BookController(
private val bookCatalogueOperations: BookCatalogueOperations,
private val bookInventoryOperations: BookInventoryOperations) { (2)
@Get (3)
fun index(): Publisher<BookRecommendation> =
Flux.from(bookCatalogueOperations.findAll())
.flatMap { b ->
Flux.from(bookInventoryOperations.stock(b.isbn))
.filter { hasStock -> hasStock }
.map { b }
}.map { (_, name) -> BookRecommendation(name) }
}
1 | The class is defined as a controller with the @Controller annotation mapped to the path /books . |
2 | Constructor injection |
3 | The @Get annotation maps the index method to /books requests that use an HTTP GET. |
The previous controller returns a Publisher<BookRecommendation>
. Create the BookRecommendation
POJO:
package example.micronaut
import io.micronaut.serde.annotation.Serdeable
import jakarta.validation.constraints.NotBlank
@Serdeable
data class BookRecommendation(@NotBlank val name: String)
BookCatalogueClient
and BookInventoryClient
will fail to consume the bookcatalogue
and bookinventory
during the tests phase.
Using the @Fallback annotation you can declare a fallback implementation of a client that will be picked up and used once all possible retries have been exhausted
Create @Fallback
alternatives in the test
classpath.
package example.micronaut
import io.micronaut.context.annotation.Requires
import io.micronaut.context.env.Environment.TEST
import io.micronaut.retry.annotation.Fallback
import jakarta.inject.Singleton
import reactor.core.publisher.Mono
import jakarta.validation.constraints.NotBlank
@Requires(env = [TEST]) (1)
@Fallback
@Singleton
open class BookInventoryClientStub : BookInventoryOperations {
override fun stock(@NotBlank isbn: String): Mono<Boolean> {
if (isbn == "1491950358") {
return Mono.just(true) (2)
}
if (isbn == "1680502395") {
return Mono.just(false) (3)
}
return Mono.empty() (4)
}
}
1 | Make this fallback class to be effective only when the Micronaut environment TEST is active |
2 | Here we arbitrarily decided that if everything else fails, that book’s stock would be true |
3 | Similarly, we decided that other book’s stock method would be false |
4 | Finally, any other book will have their stock method return an empty value |
package example.micronaut
import io.micronaut.context.annotation.Requires
import io.micronaut.context.env.Environment.TEST
import io.micronaut.retry.annotation.Fallback
import jakarta.inject.Singleton
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
@Requires(env = [TEST])
@Fallback
@Singleton
class BookCatalogueClientStub : BookCatalogueOperations {
override fun findAll(): Publisher<Book> {
val buildingMicroservices = Book("1491950358", "Building Microservices")
val releaseIt = Book("1680502395", "Release It!")
return Flux.just(buildingMicroservices, releaseIt)
}
}
Write a test:
package example.micronaut
import io.micronaut.core.type.Argument
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable
@MicronautTest
class BookControllerTest(@Client("/") val client: HttpClient) {
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
@Test
fun testRetrieveBooks() {
val books = client.toBlocking().retrieve(HttpRequest.GET<Any>("/books"),
Argument.listOf(BookRecommendation::class.java))
assertEquals(1, books.size)
assertEquals("Building Microservices", books[0].name)
}
}
Edit application.yml
micronaut:
application:
name: bookrecommendation (1)
1 | Configure the application name. The name will be used later in the guide. |
Modify the Application
class to use dev
as a default environment:
The Micronaut framework supports the concept of one or many default environments. A default environment is one that is only applied if no other environments are explicitly specified or deduced.
package example.micronaut
import io.micronaut.context.env.Environment.DEVELOPMENT
import io.micronaut.runtime.Micronaut.build
fun main(args: Array<String>) {
build()
.args(*args)
.packages("example.micronaut")
.defaultEnvironments(DEVELOPMENT)
.start()
}
Create src/main/resources/application-dev.yml
. The Micronaut framework applies this configuration file only for the dev
environment.
micronaut:
server:
port: 8080 (1)
1 | Configure the application to listen on port 8080 |
Create a file named application-test.yml
which is used in the test environment:
eureka:
client:
registration:
enabled: false
Run the unit test:
./mvnw test
4.4. Running the application
Run bookcatalogue
microservice:
./mvnw mn:run
14:28:34.034 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 499ms. Server Running: http://localhost:8081
Run bookinventory
microservice:
./mvnw mn:run
14:31:13.104 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 506ms. Server Running: http://localhost:8082
Run bookrecommendation
microservice:
./mvnw mn:run
14:31:57.389 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 523ms. Server Running: http://localhost:8080
You can run a cURL command to test the whole application:
curl http://localhost:8080/books
[{"name":"Building Microservices"}]
5. Eureka and the Micronaut framework
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.
5.1. Eureka Server
Spring-Cloud-Netflix provides a very neat way to bootstrap Eureka. To bring up Eureka server using Spring-Cloud-Netflix:
-
Clone the sample Eureka server application.
-
Run this project as a Spring Boot application (e.g. import into IDE and run main method, or use
mvn spring-boot:run
or./gradlew bootRun
). It will start up on port 8761 and serve the Eureka API from/eureka
.
5.2. Book Catalogue
Append to bookcatalogue
service application.yml
the following snippet:
eureka:
client:
registration:
enabled: true
defaultZone: "${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}"
Previous configuration registers a Micronaut application with Eureka with minimal configuration. Discover a more complete list of configuration options at EurekaConfiguration.
5.3. Book Inventory
Append to bookinventory
.application.yml
the following snippet:
eureka:
client:
registration:
enabled: true
defaultZone: "${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}"
5.4. Book Recommendation
Append to bookrecommendation
.application.yml
the following snippet:
eureka:
client:
registration:
enabled: true
defaultZone: "${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}"
Modify BookInventoryClient
and BookCatalogueClient
to use the service id instead of a hardcoded URL.
package example.micronaut
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.retry.annotation.Recoverable
import org.reactivestreams.Publisher
@Client(id = "bookcatalogue") (1)
@Recoverable(api = BookCatalogueOperations::class)
interface BookCatalogueClient : BookCatalogueOperations {
@Get("/books")
override fun findAll(): Publisher<Book>
}
1 | Use the configuration value micronaut.application.name used in bookcatalogue as service id . |
package example.micronaut
import io.micronaut.http.MediaType.TEXT_PLAIN
import io.micronaut.http.annotation.Consumes
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.retry.annotation.Recoverable
import reactor.core.publisher.Mono
import jakarta.validation.constraints.NotBlank
@Client(id = "bookinventory") (1)
@Recoverable(api = BookInventoryOperations::class)
interface BookInventoryClient : BookInventoryOperations {
@Consumes(TEXT_PLAIN)
@Get("/books/stock/{isbn}")
override fun stock(@NotBlank isbn: String): Mono<Boolean>
}
1 | Use the configuration value micronaut.application.name used in bookinventory as service id . |
5.5. Running the Application
Run bookcatalogue
microservice:
./mvnw mn:run
14:28:34.034 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 499ms. Server Running: http://localhost:8081
Run bookinventory
microservice:
./mvnw mn:run
14:31:13.104 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 506ms. Server Running: http://localhost:8082
Run bookrecommendation
microservice:
./mvnw mn:run
14:31:57.389 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 523ms. Server Running: http://localhost:8080
You can run a cURL command to test the whole application:
curl http://localhost:8080/books
[{"name":"Building Microservices"}]
Open http://localhost:8761 in your browser.
You will see the services registered in Eureka:
You can run a cURL command to test the whole application:
curl http://localhost:8080/books
[{"name":"Building Microservices"}]
6. Generate a Micronaut Application Native Executable with GraalVM
We will use GraalVM, an advanced JDK with ahead-of-time Native Image compilation, to generate a native executable of this Micronaut application.
Compiling Micronaut applications ahead of time with GraalVM significantly improves startup time and reduces the memory footprint of JVM-based applications.
Only Java and Kotlin projects support using GraalVM’s native-image tool. Groovy relies heavily on reflection, which is only partially supported by GraalVM.
|
6.1. GraalVM Installation
sdk install java 21.0.5-graal
For installation on Windows, or for a manual installation on Linux or Mac, see the GraalVM Getting Started documentation.
The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the GraalVM Free Terms and Conditions.
Alternatively, you can use the GraalVM Community Edition:
sdk install java 21.0.2-graalce
6.2. Native Executable Generation
To generate a native executable using Maven, run:
./mvnw package -Dpackaging=native-image
The native executable is created in the target
directory and can be run with target/micronautguide
.
It is possible to customize the name of the native executable or pass additional build arguments using the Maven plugin for GraalVM Native Image building. Declare the plugin as following:
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.10.3</version>
<configuration>
<!-- <1> -->
<imageName>mn-graalvm-application</imageName> (1)
<buildArgs>
<!-- <2> -->
<buildArg>-Ob</buildArg>
</buildArgs>
</configuration>
</plugin>
1 | The native executable name will now be mn-graalvm-application . |
2 | It is possible to pass extra build arguments to native-image . For example, -Ob enables the quick build mode. |
Start the native executables for the three microservices and run the same curl
request as before to check that everything works with GraalVM.
7. Next Steps
Read more about Eureka Support in the Micronaut framework.
8. Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
9. License
All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…). |