Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Sunday, June 16, 2019

Netflix Hystrix Circuit Breaker

Netflix Hystrix Circuit Breaker


In one of my previous blogs, I have already discussed the Circuit breaker pattern and its usage, Today, we will see how can we implement it in our application using Spring Cloud Netflix Hystrix.

In this document, I’ll walk you through the process of applying circuit breakers to potentially-failing method calls using the Netflix Hystrix fault tolerance library.

Hystrix is watching methods for failing calls to related services. If there is such a failure, it will open the circuit and forward the call to a fallback method.

To understand it in a better way, I’ll take the same problem statement that I have already discussed in my previous blog i.e. E-commerce Portal. 



I am assuming you must be aware of Spring boot framework as this implementation is completely based on it.

As per problem statement, we need two applications i.e. Product Service & Price Service but in this tutorial, I’ll talk about just Product Service as Price Service is simple web application exposing a single API and you can clone it from GitHub.

I’ll talk about Product Service in which we will integrate Circuit breaker pattern and Cache Service implementation and internally, it will invoke Price Service to get the Price Detail.

Optional: You can download the below project from GitHub or you can try it by creating a new one. I would recommend you to try it from scratch.

Product Service:

Create Spring boot application and add below dependency to your pom.xml file.


<!-- Optional. Application health monitor check -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<!-- Compulsory for circuit breaker implementation -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

<!-- Optional. Dashboard in case you want to see the circuit state UI -->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

</dependency>


To enable Circuit Breaker in spring boot application, add @EnableCircuitBreaker annotation on product-service entry-point class. 



Now use @HystrixCommand annotation on themethod we want to apply timeout and fallback method.

I have annotated @HystrixCommand(fallbackMethod = “getProductDetailFromCache”) on getProduct(String Id) so that if it doesn’t receive the response within the certain time limit or request get failed while calling price service API then fallback() should get called over here and fetch the price value from Cache Service. 

Make sure, the fallback method should be defined in the same class and should have the same signature.


We can customize the @HystrixCommand default behavior by configuring properties using @HystrixProperty annotations. Will check in depth in another blog. 

Add RestController class and other required class as per source code present in GitHub.

Add server.port to the application.properties file.

Start both applications and try to fetch the product Detail. In our case, our product-service app URL is:

http://localhost:7001/products/1



Now try to fetch product which does not exist


You will observe that over here circuit breaker is still in the closed state even after the exception. 

Why? The reason is that we have added  ignoreExceptions = { ProductNotFoundException.class } to @HystrixCommand so that it should not trip circuit if the product is not present.

Now, Stop the price-service application and hit the product URL again:


Check the log:


Congratulations! Our application is working as expected.

Monitoring Circuit Breakers using Hystrix Dashboard

Hystrix comes with a decent dashboard where we can monitor the status of Hystrix commands

To enable it, Add Hystrix dashboard dependency to the pom.xml file 


<!-- Optional. Application health monitor check -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Optional. Dashboard in case you want to see the circuit state UI -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>


Add @EnableHystrixDashboard annotation on the entry point class

Add management.endpoints.web.exposure.include=hystrix.stream to application.properties file.



Start the product-service application and then go to http://localhost:port/hystrix to view the dashboard. 



In Hystrix Dashboard home page enter http://localhost:7001/actuator/hystrix.stream as stream URL and give Product Service as Title and click on Monitor Stream button. 




Now, we can see that the Circuit status along with how many calls succeed and how many failures occurred, etc. 


Spring-boot microservices can be downloaded from GITHUB

Happy Coding...!!!



Saturday, July 8, 2017

FAQ Questions on Spring Boot

How to control logging with Spring Boot?

By default, the SLF4j Logging is included in the Spring Boot starter package. To enable logging, create a application.properties file in the root of the resources folder.

1. application.properties

logging.level.org.springframework.web=ERROR
logging.level.com.waheedtechblog=DEBUG

# Logging pattern for the console
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

# Logging pattern for file
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"

logging.file=/Users/waheed/application.log

Similarly we can configure in application.yml as well.

2. application.yml
logging:
  level:
    org.springframework.web: ERROR
    com.waheedtechblog: DEBUG
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
    file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
  file: /Users/waheed/application.log

3.  Classic Logback.xml

If you don’t like the Spring Boot logging template, just create a standard logback.xml in the root of the resources folder or root of the classpath. This will override the Spring Boot logging template.

How to run Spring boot application to custom port ?
In application.properties, add following property.
            Server.port=9090

How to configure datasource using Spring boot?
• Use either spring-boot-starter-jdbc or spring-boot-starterdata-jpa and include a JDBC driver on classpath
• Declare properties

1.      spring.datasource.url=jdbc:mysql://localhost/test
2.      spring.datasource.username=dbuser
3.      spring.datasource.password=dbpass
4.      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
– Spring Boot will create a DataSource with properties set
– Will even use a connection pool if the library is found on the classpath!

How to change Context Path?
To change the context path, update server.contextPath properties either in application.properties file or application.yaml file.

Application.properties

server.port=8080
server.contextPath=/application

application.yaml

server:
  port: 8080
  contextPath: /application



Database access with Spring boot

The simplest way of creating Spring boot with MySQL application is via Spring starter project using STS (Spring Tool Suite) IDE.

Steps:
  • Create a new project by selecting spring starter project wizard
  • Select project type as Maven, provide project name, packaging, and Java version etc.

  • Select spring boot version and project dependencies

  • After finishing, It will create the project structure with all required dependency.
  • Configure MySQL configuration in application.properties file
  • Now, Create the @Entity model (User) which should be persisted in the database. Hibernate will automatically translate entity class into a table.

  • Create the repository (UserRepository.java). The Repository interface will be automatically implemented by Spring in a bean with the same name with changing case. For UserRepository, the bean name will be userRepository.

  • To handle HTTP request, Create a Controller class (UserController.java) having method POST and GET operations.

  • Now run the SpringBootMySQLApplication.java as a Java application. It will start the tomcat server on default port i.e. 8080
  • Open browser and hit url http://localhost:8080/users

  • As there is no user present into the database that is why we see empty list. Let's add few users using POSTMAN.
  • To add a user, Open Postman and run below http request


  • Congratulations! We've just developed a Spring application which is bound to a MySQL database, Ready for production! Source code can be downloaded from GITHUB.
Happy Coding...!!!

Friday, July 7, 2017

Spring Boot application Example

As we know that there are various ways for creating Spring Boot application. For this sample application, I am going to use STS (Spring Tool Suite) IDE.

Steps:
  • Create a new project by selecting spring starter project wizard


  • Select project type as Maven, provide project name, packaging, and Java version etc.

  • Select spring boot version and project dependencies

  • After finishing, you can see the project structure as shown below

  • Spring boot generates a Java file in the src/main/java directory, pom.xml file with all required dependency. 


  • To handle HTTP request, Create a Controller class

  • Now run the SpringBootExampleApplication.java as a Java application. It will start the tomcat server on default port i.e. 8080

  • Open browser and hit url http://localhost:8080/application
Note:

By Default, Application will start on port 8080 but can override by adding server.port to application.properties file.


Happy Coding..!!!

What is Spring Boot Initilizr?

Spring Boot Initilizr is a web tool which is provided by Spring on official site using which Spring Boot project can be created by providing project details. It simplifies Spring Applications Development by providing initial project structure and build scripts which reduces development time thus increase productivity.
Steps to create Spring Boot project via initilizr
·        Select Maven project and dependencies. Fill other details as shown below and click on generate project.


·        Download the project, extract and now import this project As Maven by using Import option from the STS (Spring Tool Suite) IDE.
·        After finishing, You can see the project structure as shown below:

·    Spring boot generates a Java file in the src/main/java directory, pom.xml file with all required dependency. The default created Java file is the below.

·    Now run this project by selecting Java Application from run options and you will see the following output.


 Happy Coding…!!!

Introduction to Spring Boot

Spring Boot is another module provided by Spring Framework which provides RAD (Rapid Application Development) feature to Spring framework. Using boot, we can create standalone Spring based application that you can “just run” in no time and Most Spring Boot applications need very little Spring configuration and it does not require any XML configuration. It uses convention over configuration software design paradigm that means it decrease the effort of developer.

Why Spring Boot?

As we know Spring framework provides flexibility to configure the beans in multiple ways such as XML, Annotations and JavaConfig. With the number of features increased the complexity also gets increased and configuring Spring applications becomes tedious and error-prone.
Spring Boot:
·         Ease the dependency Management, Java-based applications Development, Unit Test and Integration Test Process.
    • Eg: By adding springboot-starter-web dependency to pom.xml file will pull by default all the commonly used libraries such as spring-webmvc, jackson-json, validation-api and tomcat into your Spring MVC applications.
  • Do the Auto Configuration
    • Eg: By Adding above dependency will adds all these libraries but also configures the commonly registered beans like DispatcherServlet, ResourceHandlers, MessageSource etc beans with sensible defaults. Will see JDBC configuration blog in next blog.
  • Support for Embedded Servlet (Tomcat, Jetty )
    • It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  • Easy to develop Spring Based applications with Java or Groovy and it reduces lots of development time and increases productivity. It also avoids writing lots of boilerplate Code, Annotations and XML Configuration.
  • Easy to integrate Spring Boot Application with its Spring Ecosystem like Spring-jdbc, Spring-orm, Spring-data-jpa, Spring security etc.

Spring Boot Components

·         Spring Boot Starter

    • Spring boot starter are just JAR Files or set of convenient dependency descriptors which we can include in our application to makes development easier and faster. It follows a naming pattern like: spring-boot-starter-*, where * is a particular type of application. This naming structure is intended to help when you need to find a starter. For example, if we want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project.

·        Spring Boot AutoConfigurator

·   It used by Spring Boot Framework to provide “Auto-Configuration. It attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then we will auto-configure an in-memory database.
·      You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configurationclasses.

·        Spring Boot Actuator

·         It is used to expose different types of information about the running application – health, metrics, info, dump, env etc.
·         We can choose to manage and monitor our application using HTTP endpoints, with JMX or even by remote shell (SSH or Telnet). Auditing, health and metrics gathering can be automatically applied to your application.
·         To start using the existing actuators in Boot – we’ll just need to add the spring-boot-actuator dependency to the pom:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>1.4.2.RELEASE</version>
</dependency>

·        Spring Boot CLI

·         Tool to run and test Spring Boot applications from command prompt. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all dependencies and execute the application.
·         It internally contains Groovy and Grape (JAR Dependency Manager) to add Spring Boot Defaults and resolve all dependencies automatically.

·        Spring Boot Initilizr

·         It is a web tool which is provided by Spring on official site using which Spring Boot project can be created by providing project details. Check here in depth details.


That’s it all about Spring Boot Framework. Will add few more blogs for depth details.





Tuesday, February 14, 2017

Singleton Class Vs Singleton bean scope

I have seen people getting confused between singleton scope vs singleton design pattern. Basically, there is a bit difference between these two.

Singleton scope: The spring support five different scopes and it is used to decide which type of bean instance should be returning from Spring container back to the caller. One of the scope is Singleton and the by default scope too. It returns a single bean instance per Spring IoC container.

<bean id=”object1” class=“com.package.classname”/>

When I said, single bean instance per spring Ioc Container i.e. you will always get the same object regardless of the number of call of the same bean but if you declare another bean for the same class then you will get another object for another bean.

Let’s understand this with an example:

<bean id=”object1” class=“com.package.classname”/>
<bean id=”object2” class=“com.package.classname” scope=”prototype”/>
<bean id=”object3” class=“com.package.classname”/>

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(APP_FILE);
Classname name1 = (Classname) context.getBean(“object1”);
Classname name2 = (Classname) context.getBean(“object2”);
Classname name3 = (Classname) context.getBean(“object3”);


name1==name1 // true, object1 is singleton, calling again n again will give the same object.
name2==name2 //false, object2 is prototype, calling again n again will give the different object
name1==name3 // false, object1 & object3 is singleton but two different bean.

So, the question arise how will you achieve the Singleton design pattern in Spring?

Spring framework provides facility to inject bean using factory method i.e. that returns an instance of its own class and can be used in singleton design pattern.

public class Singleton {
         
                private static volatile Singleton instance = null;
                private Singleton(){        
                }
                                public static Singleton  getInstance(){
                                if(instance == null) {
                                                synchronized (Singleton.class) {
                                                                if(instance == null){
                                                                                instance = new Singleton();
                                                                }
                                                }
                                }
                                return instance;
                }
           }

<bean id="object4" class="com.package.Singleton" factory-method="getInstance"/>
       <bean id="object5" class="com.package.Singleton" factory-method="getInstance"/>
Singleton singleton1= (Singleton) context.getBean(“object4”);
Singleton singleton2= (Singleton) context.getBean(“object5”);

singleton1==singleton1 // true, object1 has singleton scope, calling again n again will give the same object.
singleton1==singleton2 //true, Class is a singleton and we are getting an object from getInstance using factory-method.

Conclusion: Singleton scope is a bit different from a single design pattern, Returns a single bean per Spring Ioc Container whereas singleton design pattern will always return the same object.
Singleton scope can be useful where you are creating multiple datasource in your application where each datasource point to a different object.

You can download the code source from my GitHub repository.

Happy coding!!!




How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...