Implementing Cloud-Native
Architectural Patterns with
Micronaut
Naresha K

@naresha_k

https://blog.nareshak.com/
About me
Developer, Coach, Consultant
Founder & Organiser
Bangalore Groovy User Group
https://github.com/cncf/toc/blob/master/DEFINITION.md
https://www.redhat.com/en/topics/cloud-native-apps#
https://www.redhat.com/en/topics/cloud-native-apps#
Externalised Configuration
Configuration coupled with code
https://blog.nareshak.com/if-you-are-building-your-artifacts-per-environment-you-are-doing-it-wrong/
“Configuration should be
version controlled”
Configuration coupled with code
Externalising the configuration
@Controller("/hello")
public class HelloController {
@Value("${greeting.message}")
private String message;
@Get("/")
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return message;
}
}
micronaut:
application:
name: hello-service
greeting:
message: Hello from Micronaut
java -Dmicronaut.environments=uat -jar
hello-service-0.1.jar
Configuration Injection
java -Dgreeting.message="Hello from External Config”
-jar hello-service-0.1.jar
java -Dmicronaut.config.files=“/tmp/external-config.yml"
-jar hello-service-0.1.jar
Is this not enough?
Config Server
Config
Server
Application
Application
Application
docker run -p 8500:8500 consul
bootstrap.yml
curl -X PUT -d @- localhost:8500/v1/kv/config/greet-service/greeting.message <<< "Hello from Consul"
~ curl http://localhost:8080/greet
Hello from Consul
Service Discovery
Cloud Infrastructure
Microservices
Service
Registry
Service
X
Service A
Service
Registry
Service
X
Service A
Register
Service
Registry
Service
X
Service A
Register
Service
Registry
Service
X
Service A
Get routing information
Service
Registry
Service
X
Service A
REST call
---
consul:
client:
registration:
enabled: true
defaultZone: "${CONSUL_HOST:localhost}:${CONSUL_PORT:8500}"
application.yml
sample-service ./gradlew run
> Task :run
05:12:07.121 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 1260ms. Server Running: http://localhost:8080
05:12:07.251 [nioEventLoopGroup-1-3] INFO i.m.d.registration.AutoRegistration - Registered service [sample-service] with Consul
http://localhost:8080/greet
@Controller("/hello")
class HelloController {
@Inject
GreetClient greetClient
@Get("/")
String index() {
greetClient.index()
}
}
Resiliency /
Fault Tolerance
https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing
07:19:19.838 [pool-1-thread-2] ERROR i.m.r.intercept.RecoveryInterceptor - Type
[com.nareshak.demo.GreetClient$Intercepted] attempting to resolve fallback for unavailable service
[sample-service]
07:19:19.843 [pool-1-thread-2] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred:
No available services for ID: sample-service
io.micronaut.discovery.exceptions.NoAvailableServiceException: No available services for ID: sample-
service at
io.micronaut.http.client.loadbalance.AbstractRoundRobinLoadBalancer.getNextAvailable(AbstractRoundRo
binLoadBalancer.java:50)
Timeout
07:33:47.976 [pool-1-thread-3] ERROR i.m.r.intercept.RecoveryInterceptor - Type [com.nareshak.demo.GreetClient$Intercepted]
executed with error: Read Timeout
io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout
Retry
@Retryable
String callExtService(){
}
@Retryable(attempts = "3", delay = "5s")
String callExtService(){
}
07:26:27.347 [pool-1-thread-2] ERROR i.m.r.intercept.RecoveryInterceptor - Type [com.nareshak.demo.GreetClient$Intercepted]
attempting to resolve fallback for unavailable service [sample-service]
07:26:37.355 [pool-1-thread-2] ERROR i.m.r.intercept.RecoveryInterceptor - Type [com.nareshak.demo.GreetClient$Intercepted]
attempting to resolve fallback for unavailable service [sample-service]
07:26:52.361 [pool-1-thread-2] ERROR i.m.r.intercept.RecoveryInterceptor - Type [com.nareshak.demo.GreetClient$Intercepted]
attempting to resolve fallback for unavailable service [sample-service]
07:27:12.373 [pool-1-thread-2] ERROR i.m.r.intercept.RecoveryInterceptor - Type [com.nareshak.demo.GreetClient$Intercepted]
attempting to resolve fallback for unavailable service [sample-service]
07:27:12.378 [pool-1-thread-2] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: No available services for
ID: sample-service
io.micronaut.discovery.exceptions.NoAvailableServiceException: No available services for ID: sample-service
at
io.micronaut.http.client.loadbalance.AbstractRoundRobinLoadBalancer.getNextAvailable(AbstractRoundRobinLoadBalancer.java:50)
Circuit Breakers
@CircuitBreaker
String callExtService(){
}
@CircuitBreaker(reset = "40s")
String callExtService(){
}
Fallback
@Fallback
class FallbackClient implements GreetClient{
@Override
String index() {
return "Default Message"
}
}
API Versioning
@Get(“/greet")
String index() {
"Hello"
}
@Get(“/greet")
Single<String> indexV2() {
Single.just("Hello V2")
}
@Get(“/greet")
@Version("1")
String index() {
"Hello"
}
@Get(“/greet")
@Version("2")
Single<String> indexV2() {
Single.just("Hello V2")
}
router:
versioning:
enabled: true
header:
enabled: true
names:
- 'X-API-VERSION'
- 'Accept-Version'
application.yml
~curl http://localhost:8080/greet -H "X-API-VERSION: 1"
Hello%
~ curl http://localhost:8080/greet -H "X-API-VERSION: 2"
Hello V2%
@Client("sample-service")
@Version("1")
interface GreetClient {
@Get("/greet")
String index()
@Version("2")
@Get("/greet")
Single<String> indexV2()
}
When you in are in cloud-native world, features like fault tolerance,
service discovery, externalised configuration are necessary
Micronaut makes it easier to implement these patterns with its built-in support
Conclusion
Thank You

Implementing Cloud-native Architectural Patterns with Micronaut