SPRING CLOUD | MICROSERVICES

Spring Cloud annotations simplified

Spring Cloud has useful annotations that are worth knowing for developing microservices.

SP Sarkar
Dev Genius
Published in
4 min readDec 29, 2021

--

Background Image by Mick De Paola on Unsplash

Spring Cloud module is a great extension of the original Spring framework that provides tools for developers to quickly build some of the common patterns in distributed systems e.g service discovery, circuit breakers, intelligent routing, control bus, tokens, etc.

Just like Spring Boot, Spring Cloud has also some useful annotations that are worth knowing for Spring Cloud developers.

EnableConfigServer

Use of @EnableConfigServer

@EnableConfigServer is class-level annotation. It helps Spring Cloud Config Server to be embedded in Spring Boot application very easily and the @EnableConfigServer annotation makes the Spring Boot application act as a Configuration Server.

Eureka

EnableEurekaServer

In the large application, we might have numerous microservices that should be working together. Those services might have different server addresses. So now we have faced a serious problem, that is how services would talk to each other. A simple hack might be hardcoding all services addresses including port while calling using either RestTemplate or WebClient.

But it considers a bad practice because a single service may have multiple instances running with different addresses, so in this case, how will we know which instances to connect.

To solve this type of specific problem, we can use Service Discovery via Eureka.

Use of @EnableEurekaServer

We can convert a Spring Boot application into a Eureka server by annotating a class with @EnableEurekaServer. Now we can use this server to look upon the other Eureka Client services to manage them. The services must be registered to this server.

EnableEurekaClient

A Eureka Server can not find other services/clients if the services/clients are not annotated with @EnableEurekaClient which comes from spring-cloud-netflix. Each service that we want to register with the server should be annotated with the @EnableEurekaClient annotation. It is class-level annotation.

Use of @EnableEurekaClient

EnableDiscoveryClient

@EnableDiscoveryClient which comes from spring-cloud-commonsis the same as @EnableEurekaClient but it is a more generic implementation of “Discovery Service”.

@EnableEurekaClient only works with Eureka whereas @EnableDiscoveryClient works with eureka, consul, zookeeper. But if Eureka is on the application classpath, they are effectively the same.

EnableCircuitBreaker

Assume in a Movie Rental application, we have three microservices: MovieCatalogService, MovieRatingService, and MovieOrderService.

Uses of @EnableCircuitBreaker

And, these microservices need to interact with each other to process user's requests. For example, MovieOrderService might call MovieCatalogService and MovieRatingService to display movie name (from MovieCatalogService ) and movie rating (from MovieRatingService ) along with orders(from MovieOrderService) to display to the user during the movie renting process.

Suppose, for some reason (network error or an overload) MovieCatalogService completely fails and stops servicing the requests. As a result, the whole Ecosystem will get unstable due to this single point of failure.

In this situation, a circuit breaker pattern comes handy. It redirects traffic to a fallback path once it sees any such scenario. @EnableCircuitBreaker is a class-level annotation, and we need to apply this annotation to each and every service of your application.

There are many tools(Netflix Hystrix, Resilience4J, Sentinel, Spring Retry) available to implement the circuit breaker pattern in Spring. But in this post, we will discuss Netflix Hystrix common annotations.

EnableHystrix & EnableHystrixDashboard

Use of @EnableHystrixDashboard

@EnableHystrix is optional. @EnableCircuitBreaker will scan the classpath for any compatible Circuit Breaker implementation, if Hystrix is in classpath then we need not explicitly enable Hystrix.

@EnableHystrixDashboard adds one useful dashboard running on localhost provided by Hystrix to monitor its status.

HystrixCommand

Use of @HystrixCommand

We have enabled fall back method @HystrixCommand(fallbackMethod = “getMovieOrdersFallback”) with same signature. Now, the fallback method will be invoked if the actual movie order service is down for some reason.

LoadBlanced

Use of @LoadBalanced

@LoadBalanced is a marker annotation. It is used to indicate that RestTemplate or WebClient should use a RibbonLoadBalancerClient or LoadBalancerClient for interacting with the services. In turn, this allows you to use “logical identifiers” for the URLs we pass to the RestTemplate or WebClient. These logical identifiers are typically the name of a service registered in service discovery.

Note:- @RibbonClient(spring-cloud-starter-netflix-ribbon) and @LoadBalancerClient(spring-cloud-commons) are optional. This both works as a load-balancer on the client-side and gives us control over the HTTP and TCP clients. If we are using Service Discovery then we are good to go with default settings. we don't need to explicitly use these annotations.

Use of @RibbonClient/@LoadBalancerClient

But if we are not using any service discovery or need to customize the settings of Ribbon or LoadBalancer for a particular client then we need to use any of the two.

  • name - the service we are calling Ribbon or LoadBlancer but need additional customizations for how Ribbon or LoadBlancer interacts with that service.
  • configuration - set it to an @Configuration class with all of our customizations defined as @Beans .

Is this post is helpful to you? Do not forget to clap for this post and give us some inspiration back!

Are YOU interested in Fullstack Development?

I write about step-by-step coding tutorials on fullstack Web Development REST API, Microservices, architecture in LinkedIn, and Medium. Here is My Linkedin profile.

--

--

Software Engineer. Startups Enthusiastic. I mostly write about Coding and Marketing.