Important Spring cloud annotation developers should know.

Important Spring cloud annotation developers should know.

Maheshwar Ligade's photo
Maheshwar Ligade
·May 28, 2022·

3 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Table of contents

  • EnableConfigServer:
  • EnableEurekaServer:
  • EnableEurekaClient:
  • EnableCircuitBreaker:
  • EnableHystrix & EnableHystrixDashboard:
  • HystrixCommand:
  • LoadBlanced:
  • Conclusion:

In this article, you are going to learn some of the important annotations for spring cloud. Spring cloud is now a daily bread for the spring boot-microservices developers. If you are doing that it's better to understand some of the most useful annotations.

image.png

Spring Cloud is one of the projects of the Spring family, that provides tools for developers to quickly build some of the common patterns in distributed systems e.g circuit breakers, service discovery, intelligent routing, control bus, API gateway, etc.

Let us see some annotation:

EnableConfigServer:

This is the class-level annotation, which indicates that this spring boot application will act as the configuration server. Or we can say it Spring Cloud Config Server to be embedded in Spring Boot application

@SpringBootApplication
@EnableConfigServer
public class EnableConfigServerApppliction { 
public static void main(String[] args) { 

      SpringApplication.run(EnableConfigServerApppliction.class, args);   
 }
}

**@EnableConfigServer** annotation makes the Spring Boot application act as a Configuration Server.

EnableEurekaServer:

With the use of this annotation, you can convert a Spring Boot application into a Eureka server by annotating a class with @EnableEurekaServer.


@SpringBootApplication
@EnableEurekaServer
public class EnableEurekaServerApplication {    
public static void main(String[] args) { 
       SpringApplication.run(EnableEurekaServerApplication.class, args);    
}
}

EnableEurekaClient:

@EnableDiscoveryClient which comes from spring-cloud-common is 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, and zookeeper.

@SpringBootApplication
@EnableEurekaClient
public class EnableEurekaClientApplication {    
public static void main(String[] args) { 
       SpringApplication.run(EnableEurekaClientApplication.class, args);        
}
}

EnableCircuitBreaker:

To enable the Circuit Breaker, add an @EnableCircuitBreaker annotation on the catalog-service entry-point class.


@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ProductCatalogService {

public static void main(String[] args) {

SpringApplication.run(ProductCatalogService .class, args);
}
}

// ProductRatingService.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ProductRatingService{

public static void main(String[] args) {

SpringApplication.run(ProductRatingService.class, args);
}
}

// ProductOrderService.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ProductOrderService{

public static void main(String[] args) {

SpringApplication.run(ProductOrderService.class, args);
}
}

EnableHystrix & EnableHystrixDashboard:

The @EnableHystrixDashboard will give a dashboard view of the Hystrix stream. The @EnableHystrix annotation is used to enable the Hystrix functionalities in your Spring Boot application


@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard
public class EnableHystrixDashboardApplication {

public static void main(String[] args) {
SpringApplication.run(EnableHystrixDashboardApplication.class, args);
}
}

HystrixCommand:

Spring Cloud automatically wraps Spring beans with that annotation in a proxy that is connected to the Hystrix circuit breaker. The circuit breaker calculates when to open and close the circuit and what to do in case of a failure.


@Service
public class ProductOrderService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getProductOrdersFallback")
    public List<Order> getProductOrders(String userId) {
        // Logic goes here
    }

    @SuppressWarnings("unused")
    private List<Order> getProductOrdersFallback(String userId) {
        System.out.println("Product order Service is down!!! fallback route enabled...");
        // Logic goes here
    }

    // RestTeplate Bean and other code
}

LoadBlanced:

The @LoadBalanced annotation will make an instance of created RestTemplate load-balanced. There is no code you need to write to make the RestTemplate load-balance HTTP request it sends to an internal microservice. The RestTemplate bean will be intercepted and auto-configured by Spring Cloud.

@LoadBalanced is a marker annotation.


@Configuration
public class WebClientConfig {

  @LoadBalanced
  @Bean
  WebClient.Builder webClientBuilder() {
    return WebClient.builder();
  }

}

Conclusion:

This article introduced you that, most of the useful and basic annotations every developer must know when you want to develop the cloud-agnostic microservices application using spring cloud.

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

==========================**=========================

If this article adds any value for you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this