In this course, we will learn the concepts of microservice and spring framework and focus on Microservice concerns.
Learning Objectives
- Health Checks
- Alerts
- Error Handling
- Security
Intended Audience
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
Prerequisites
- Basic Java knowledge
Hello, my dear friends. In this lesson, we will learn error handling in the microservice environment. In addition to the error handling mechanism of the used programming language, a microservice environment should provide some fundamental control and prevention mechanisms to ensure a sustainable and resilient application. This is known as fault tolerance. We can enable Microprofile fault tolerance in our service by adding microprofile-5.0 to our server.xml file.
It is in fact a parent library that contains several libraries such as security, open API, fault tolerance and so on. If you only want to add a fault tolerance property, you must include the mpFaultTolerance feature. Microservices have some policies in place to deal with fault tolerance mechanisms. These are timeout, retry, fallback, bulkhead, circuit breaker, asynchronous. Timeout is applied with the @Timeout annotation. It allows you to generate a timeout event for a request when the specified time finishes. Retry is applied with @Retry annotation.
When a service makes a request to another service, it enables the source service to make additional requests in case of an access problem. Fallback is applied with @Fallback annotation. It defines the function to execute if the current operation fails. Bulkhead is applied with the @Bulkhead annotation. It lets you restrict the number of simultaneous accesses to service, so you can prevent the delays of a bottleneck. Circuit breaker is applied with the @CircuitBreaker annotation. It provides automatic fast failure feedback to prevent overloading the system. Asynchronous is applied with the @Asynchronous annotation. It makes the current operation asynchronous.
We can use asynchronous in a multi-services environment so it can help us to complete a transaction without having to wait for a sequential order. I'd like to demonstrate the use of retry and fallback methods. First, I start with retry. I add the retry annotation, it has some specifications. They are as follows: abortOn, delayUnit, durationUnit, jitter, jitterDelayUnit, maxDuration and retryOn. It's described well for each specification. When you move up between them, you can see the descriptions. I will use max, retries, and duration. Give a value of 2 for maximum retries and a value of 1,000 for duration.
It means the service will retry two times and wait up to 2 seconds in total. Okay. Make sure the citizen-service is running and the nin-service is not. Now, make a CURL for the citizen service. As you can see, it waits about two seconds before reporting an error to the console. Let's define a fallback function in case of no access to the nin-service. I'm copying the entire post function. I only change the "set NINumber" part.
Now I'm adding the fallback annotation and giving the name of the newly described function. Okay, as you can see, it tried to reach the nin-service for two seconds and then called the fallback function. When we run the nin-service, everything will be okay. One more thing about bulkhead, we can use it to limit access to the other services so we can scale the load on the services. Okay, that's all for now. See you in the next lesson dear friends.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.