Posts

Showing posts with the label Spring Boot

Spring Boot Tutorial

Prerequisite: Basic knowledge of Spring boot application I am working on a series of implementing frameworks with Spring boot application but not getting enough time to blog it and post it here or on my LinkedIn profile. So, I have started uploading my work on my GitHub repository from where it can be downloaded easily. I tried my best to add short notes for each annotation/configuration/properties in README and even I have uploaded a few screenshots to understand in a more better way. Try it out and Please do let me know in case of any confusion. Spring Boot Actuator Spring Boot Ehcache Spring Boot Swagger Spring Boot JPA Will keep uploading with others framework as well. Feedback is also most welcome. Thank you. Happy Learning!

MicroServices: Spring cloud ribbon with Discovery Server

Image
In this article, I am going to share my knowledge on Spring Cloud Ribbon and how can we use Ribbon with RestTemplate as well as with Feign Client. We will also see how Enabling discovery Sever will improve the scalability of Microservice. Before jumping into Spring Cloud, I am assuming you must be having knowledge of Eureka Server, Feign Client, and Client-Side load balancer. If not then read my below blog before jumping to Spring Cloud ribbon. Also, I am going to use my existing code to implement Ribbon . URLs: Declarative REST Client: Feign Microservices: Service Registry and Discovery Netflix Hystrix Circuit Breaker In my previous blog, I have already talked about the Eureka Server and how other applications are taking advantages of Eureka Server to fetch the host/port of client application. We have seen that three microservices application are up and running i.e. Discovery Server Product Service Price Service Where Product and Price service will register themselves to...

Declarative REST Client: Feign

Image
We already know that how microservices communicate with each other using RestTemplate. In this blog, we will see that how this can happen using Feign Client as well.  What is a Feign Client? Feign is an abstraction over REST based call. It makes writing web service clients easier. It Is as easy as creating interface and then annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. It also supports pluggable encoders and decoders and has supports for Spring MVC annotation. Using Feign, microservices can easily communicate with each other and developers don't have to bother about REST internal details and can only concentrate on business logic. Implementation. I am going to use my previous applications to demonstrate the working of Feign Client. So before starting feign-client application. Make sure all the below three applications are already up and running.  Discovery-server Product-server Price-server Feign Client Impl...

Microservices: Service Registry and Discovery

Image
In my previous blog, I have already talked about Netflix Hystrix Circuit Breaker Microservices and explained it by creating two applications i.e. Product Service and Price Service. In this post, I’ll use both applications to explain what is the Service Registry and Discovery Server. Circuit Breaker and Microservices Architecture Netflix Hystrix Circuit Breaker   What is Service Registry and Discovery and why do we need it in the first place? In our monolithic application, mostly service invoke one another through language methods or procedure calls and even in traditional distributed system deployment, services run at fixed, well-known locations (hosts and ports) and so can easily call one another using HTTP/REST. Over here the network locations of service instances are relatively static. With the microservice architecture system, this is a much more difficult problem as service instances have dynamically assigned network locations. Service instances change dynamicall...

Netflix Hystrix Circuit Breaker

Image
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 ...

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{3...

Database access with Spring boot

Image
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 a...

Spring Boot application Example

Image
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.  T o 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....