Micronaut is a modern, JVM-based, full stack microservices framework designed for building modular, easily testable microservice applications. Micronaut is developed by the creators of Grails and takes inspiration from lessons learnt over the years building real-world applications from monoliths to microservices using Spring, Spring Boot and Grails.
This session covers the current features of Particle for building microservices, such as:
– Dependency Injection and Inversion of Control (IoC).
– Configuration system.
– HTTP services.
– Cloud and serverless deployments.
– Management & Monitoring.
2. About me
— Developer since 2001.
— Founded Salenda in 2007.
— Grails user since v0.4.
— Working @ OCI since 2015.
— Father since 2017!
!
@alvaro_sanchez
5. Introducing Micronaut
The productivity of Grails with the performance
of a compile-time, non-blocking framework.
— Designed from the ground up with microservices and the
cloud in mind.
— Ultra-lightweight and Reactive (based on Netty)
— Integrated AOP and Compile-Time DI for Java and Groovy.
— HTTP Client & Server.
@alvaro_sanchez
10. Inversion of Control /
Dependency Injection
— Inspired from Spring.
— Compile-time: no reflection, no runtime proxies.
— JSR-330 (javax.inject) or Spring-like annotations.
— Implemented via annotation processors (Java) and
AST transformations (Groovy) that use ASM to
generate bytecode.
@alvaro_sanchez
12. Implicit injection via constructor
@Singleton
class WeatherService {}
@Controller('/weather')
class WeatherController {
final WeatherService weatherService
WeatherController(WeatherService weatherService) {
this.weatherService = weatherService
}
}
@alvaro_sanchez
13. Injectable types
— An Optional of a bean. If the bean doesn’t exist
empty() is injected. Alternatively, use @Nullable.
— An Iterable or subtype of Iterable (eg List, Collection
etc).
— A lazy Stream of beans.
— A native array of beans of a given type (Engine[]).
— A javax.inject.Provider if a circular dependency
requires it.
@alvaro_sanchez
23. Bean configurations
— Grouping of multiple bean definitions within a
package:
//file: src/main/groovy/com/example/json/package-info.groovy
@Configuration
@Requires(classes = com.fasterxml.jackson.databind.ObjectMapper)
package com.example.json
— All the beans from the package can be required with
@Requires(configuration='com.example.json')
@alvaro_sanchez
24. Life-cycle methods and events
— javax.annotation.PostConstruct is supported.
— For events:
@Singleton
class EngineInitializer implements BeanInitializedEventListener<EngineFactory> {
@Override
EngineFactory onInitialized(BeanInitializingEvent<EngineFactory> event) {
EngineFactory engineFactory = event.bean
engineFactory.rodLength = 6.6
return event.bean
}
}
@alvaro_sanchez
46. Reactive responses
— Any type that implements the
org.reactivestreams.Publisher:
Flowable<String> hello() {}
— A Java's CompletableFuture instance:
CompletableFuture<String> hello() {}
— An io.micronaut.http.HttpResponse and optional
response body:
HttpResponse<Flowable<String>> hello() {}
@alvaro_sanchez
47. Non-reactive responses
— Any implementation of CharSequence:
String hello()
— Any simple POJO type
Book show()
@alvaro_sanchez
48. Threading model
The response type determines the thread pool used to
execute the request:
— Non-blocking requests will be executed in the Netty
event loop thread pool.
— Blocking requests will be executed on the I/O thread
pool.
@alvaro_sanchez
49. Reactive JSON parsing with
Jackson
@Post
public Single<HttpResponse<Person>> save(@Body Single<Person> person) {
return person.map(p -> {
//do something blocking, and then:
return HttpResponse.created(p);
}
);
}
@alvaro_sanchez
50. Non-reactive JSON parsing
If your method does not do any blocking I/O:
@Post
public HttpResponse<Person> save(@Body Person person) {
//do something, and then:
return HttpResponse.created(person);
}
@alvaro_sanchez
54. Features
— Service discovery aware: Consul and Eureka support.
— Load balancing: round robin, Netflix's Ribbon.
— Reactive: based on the return types.
— Fault tolerant: retry, fallback, circuit breaker.
@alvaro_sanchez