#Ask2Shamik Presents Microservice communication using Feign Template Spring boot
In this Video, we will learn What do you mean by Feign client?
Step by Step implementation of Sync communication via Feign client using Spring Cloud


Ask2Shamik Presents a simple way to understand Microservice communication by Live coding

#Ask2Shamik Presents Microservice communication via Rest Template using Spring boot
In this Video, we will learn What do you mean by Microservice Communication?
Step by Step implementation of Sync communication via Resttemplate using Spring Cloud
How to Communicate with the config server to the resolute property using Spring-boot?

Ask2SHamik Presents Eureka server Live coding with Step by Step explanation

Ask2SHamik Presents Eureka server Live coding with Step by Step explanation, to grasp Microservice concepts come and subscribe right now.
#Ask2shamik #javaonfly #shamik #microservice #springboot #java #Dzone #JCG
Ask2Shamik presents Key points you want to know about Service discovery(Eureka server).
Take away from this video::
What is service discovery pattern?
Why is it needed in Microservice?
Demerits of the Traditional Load Balancer.
What is Eureka server?
How Multiple Eureka server communicate with each other.
Microservices comminucation.

Ask2Shamik Presents a brand new video on Microservice Pivot components(English Version).

Microservice is Hot Cake now To cook Microservice best way, you want to know what ingredients are most important? So you get an optimum experience, Check this video Which Touch bases on Each important components like Zuul, Eureka, Consul, Zookeeper, Ribbon, Zipkin, Hytrix, Splunk, ELK

Have a dilemma What to choose for your new project Monolith or Microservice? Check this Video on Ask2Shamik.

Have a dilemma What to choose for your new project Monolith or Microservice? Ask2Shamik presents 5 Checklists to choose wisely Monolith or Microservice.

Ask2Shamik Presents, A talk on Microservice Characteristics.

Ask2Shamik Presents, A talk on Microservice Characteristics, where you can learn what are the common misconceptions on Microservices.
Microservices Characteristics and deep dive to each character.
Subscribe to Ask2shamik to get more latest videos on Microservices.
#ask2shamik #javaonfly #shamik #Microservices #Springboot #dzone #jcg

Ask2Shamik Presents Why Microservice?


Ask2Shamik Presents a video called Why Microservices,

 Take away from this videos

1. Why Microservices needed?
2. What are the pain points of Monolith Architecture?

3. What are the benefits of Microservices? Please see and subscribe on Ask2Shamik, it is a brand new Youtube channel from Javaonfly production.

Hope you enjoy it!!!

Building Layered Architecture just in 3 minutes: Final Part

I am sure that you have enjoyed the part1 where just a blink of an eye we set up an in-memory database, create a Repository class using Spring Data JPA and insert data using initDataloader by Spring boot.
In this tutorial, we will expose those employee data to UI, The Classic  MVC pattern using Spring boot.
The Service Part:: In my previous article I have created an EmployeeRepository interface & Spring boot provide its implementation at runtime, now I want this data to be guided to the Service Layer. To achieve that, I need to inject that Repository to Service layer, and define the CRUD methods which are doing nothing but calls the Repositories CRUD methods, Now you may be wondering, the EmployeeRepository interface  is empty and it extends CRUDRepository, where you can only find the declaration of CRUD methods, Now the question is, where are the Implementations ?

Again the Answer is Spring-boot on the runtime it creates an Implementation class which has all the CRUD methods implementations. Interesting isn't it,  We just declare an interface and Container creates  all the stuff for us, and we save lots of our precious time, get rid of silly mistakes etc. really an IOC(Inversion of Control)
Let's see How the Service part looks like, Please note for simplicity, I print the messages in the console using Sysout, But in the real project don't ever do this, use Logger always.

package com.example.layerdArchitechture.service;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.layerdArchitechture.entity.Employee;
import com.example.layerdArchitechture.repository.EmployeeRepositiry;

@Service
public class EmployeeService {
@Autowired
EmployeeRepositiry repo;
public Employee addEmployee(Employee emp) {
emp = repo.save(emp);
System.out.println("Employee saved::" + emp);
return emp;
}
public Optional<Employee> findEmployeeById(Long empId) {
Optional<Employee> emp = repo.findById(empId);
System.out.println("Employee found::" + emp);
return emp;
}
public Iterable<Employee> findAllEmployee() {
return repo.findAll();
}
public void deleteEmployeeById(Employee emp) {
repo.delete(emp);
System.out.println("Employee deleted::" + emp);
}
}

Yeah, Our service Layer is ready, Now I need a Controller part which will act as a delegator, takes data from UI send it to Service and Vice versa, I personally recommend to use Controller as a thin layer which just porting data from UI to service and service to UI and maybe doing some data format conversion stuffs nothing more than that.
Before showing you the code of the controller let me remind you I have not use a proper Spring controller which forward the response to a view rather, I use a RestController which directly deliver the response payload in JSON / XML format, based on the Produces property. by default it is JSON.
In Controller, we map the URL pattern to a controller method bind any data coming from a form into a  Model the "M " part of MVC.
Let's see the code.
package com.example.layerdArchitechture.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.layerdArchitechture.entity.Employee;
import com.example.layerdArchitechture.service.EmployeeService;
@RestController
public class EmployeeController {
@Autowired
EmployeeService service;
@RequestMapping("/TCS/employees")
public Iterable<Employee> findAllEmployee() {
return service.findAllEmployee();
}
@RequestMapping("/TCS/employee/{id}")
public Employee findbyId(@PathVariable Long id) {
Optional<Employee> emplyoeeContainer = service.findEmployeeById(id);
return emplyoeeContainer.isPresent()?emplyoeeContainer.get():null;
}
@RequestMapping("/TCS/employee/addDemoEmployee")
public Employee addEmployee() {
Employee emp = new Employee();
emp.setName("Demo Demo");
emp.setSex("M");
emp.setAddress("Demo");
return service.addEmployee(emp);
}

@RequestMapping("/TCS/employee/delete/{id}")
public String delete(@PathVariable Long id) {
Optional<Employee> emp = service.findEmployeeById(id);
if(emp.isPresent()) {
 service.deleteEmployeeById(emp.get());
 return "deleted Successfully";
}
return "Employee Not Exists, Not able to delete";
}
}

If you look at the Controller class, You can see I autowired the Service class then wrote CRUD methods(also map them with Url pattern) as a wrapper which internally calls Service Wrapper method which calls the Spring boot's Repository Implementation class.
Now If I run the spring boot application class and hit following URL in the browser
http://localhost:8080/TCS/employees
I can see the following response in Browser in JSON format.
[
  {
    "id": 1,
    "name": "Shamik Mitra",
    "address": "BagBazar",
    "sex": "M"
  },
  {
    "id": 2,
    "name": "Samir Mitra",
    "address": "BagBazar",
    "sex": "M"
  },
  {
    "id": 3,
    "name": "Swastika Basu",
    "address": "Baranagar",
    "sex": "F"
  }
]
Hope you enjoyed the post.

Layered Architecture Up and Running just in 5 minutes:: Spring Boot Part 1

This is a two-part series where I will show how to create a Layered architecture with Spring Boot. 
What is a Layered Architecture: In a simple term when we building an enterprise application, we maintain different layers to encapsulate layers specific logic so that, it can't be spill over to another layer. When we think about an enterprise application we can imagine three important layers of the architecture. 
1. User Interface: Which interact with the end user, shows data to them, take user input, take command from them etc.
2. Business Layer: Based on the User command and the data captured from the user(AKA Form), It takes a domain-specific decision, like what to do with the data, which table to look, how to manipulate the data which comes from the database, so it can be presented in UI.
3. Persistence layer: This layer captures the data and persists it, same also capture any updation, deletion, and change of the state of the data, so you can consider this layer as, maintaining a state of the Application Specific Data.
Irrespective of your application is up or down it stores the state of the data once committed.

By layered architecture, we create a logical encapsulation of each layer like all the code, respect to UI stays in the UI layer, all code regarding business logic stays in Business Layer etc.
Each layer communicates with its adjacent layer, but never communicate with another layer which is not adjacent.
So if you have an application which has three layers UI, Business, DAO, UI communicate with Business, Business communicate with UI and DAO and DAO communicate with Business. By this way, we can reduce coupling, makes layers reusable and welcoming the future change in the architecture. Each layer has it's own pattern to accommodate future change an make the layer reusable.

We all know Spring provide different components for each layer like, for UI you can use Thymleaf or Spring template or any other UI framework like JSF, for Business layer,  you can use Controller and service, Also you can inject different framework like Struts in to it, For Persistence layer you can use Spring data JPA, Hibernate, JDBC template whatever. But the problem is you need to add all the plugins/Jars in your pom.xml find the right version of the dependencies in the classpath, If version mismatches it will not going to work also you need to add many Spring specific annotations or XML entries in Spring XML files to use those component/plugins in your layered architecture, which is a cumbersome method also you need to package them and deploy them in an application server, so many manual interventions needed. Spring addresses this problem and comes out with a solution which they Called Spring Boot.
"Spring-boot works by convention over configuration" --  It means you don't have to think about configuration entry, only pay attention to your business logic, whatever the component you want to use if those are mentioned in your classpath Spring boot is so smart that it will understand you want to use the same and configure a fully working components for you. Say you want to use JPA in your project, if you import Spring boot starter JPA module, it understand you want to use it and on the fly it creates the Spring template a repository and utility CRUD methods for you, without Spring-boot you need to configure the JPA template, Initiate a session factory from template, get the session etc., those are just not required here, Spring boot is so powerful that it can do it for you, and of course If you want to control the configuration by yourself you can override them and use your own configuration.
In this tutorial, I will show you How to create an MVC Layered architecture Step by Step using Spring-boot, and you will be amazed within five minutes you can create an MVC architecture Up and running, which previously take lots of time, and lots of head scratching in case of version mismatch.
As it is a two-part series, In the first part we will setup a database, insert Employee data into the database using JPA repository.

For this tutorial, we will use the following components

1. H2 Database: Which is an in-memory database, It persists the data until the application closed. 

2. Spring Data JPA: we will use Spring data JPA component for CRUD operation in Database.

3. Rest Controller: Which will show the data in a JSON format rather than forwarding response into a View, unlike traditional MVC.

Step 1: Go to start.spring.io and download a template project by selecting Web, JPA, H2 Module.
Step  2: Import that project into eclipse as maven project.
Step 3: Check the pom.xml, spring-starter-web,spring-starter-data-jpa,h2 module entry will be there. These are Spring boot packages which contain all the necessary dependencies for jpa and web and maintaining right version of dependent jars so that there will be no version mismatch problem.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>layerdArchitechture</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>layerdArchitechture</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>


Step 4: Go to application.properties file under src/main/resources and make the h2 console enabled true so we can see the data inserted in the h2 database.
spring.h2.console.enabled=true

Step 5 : Let's create an Employee entity.
package com.example.layerdArchitechture.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Employee {
@Id
    @GeneratedValue
private Long id;
private String name;
private String address;
private String sex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", address=" + address + ", sex=" + sex + "]";
}

}


Step 6: Now create an EmployeeRepositiry interface which will extend the CrudRepository interface, Spring-boot on the fly creates an implementation and create all the utility(crud) methods we don't have to do anything but just declare the interface.

package com.example.layerdArchitechture.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.layerdArchitechture.entity.Employee;

@Repository
public interface EmployeeRepositiry extends CrudRepository<Employee, Long> {


}


Step 7. Now create a Data Loader class which will insert few employees into the H2 database using the above-created repository. For that, I have to auto-wire the EmployeeRepository interface. Please pay attention to loadData method here I create a list of the employee then iterate of the list and save those data in H2 database by using of lambda expression in Java8.

package com.example.layerdArchitechture;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.layerdArchitechture.entity.Employee;
import com.example.layerdArchitechture.repository.EmployeeRepositiry;

@Component
public class InitDataLoader {
@Autowired
private EmployeeRepositiry rep;

public void loadData() {
createData().forEach(
emp->{
rep.save(emp);
System.out.println("Successfully saved ::" + emp);
}

);

}

private List<Employee> createData() {

List<Employee> employees = new ArrayList<Employee>();

Employee emp = new Employee();
emp.setName("Shamik Mitra");
emp.setSex("M");
emp.setAddress("BagBazar");
Employee emp1 = new Employee();
emp1.setName("Samir Mitra");
emp1.setSex("M");
emp1.setAddress("BagBazar");
Employee emp2 = new Employee();
emp2.setName("Swastika Basu");
emp2.setSex("F");
emp2.setAddress("Baranagar");

employees.add(emp);
employees.add(emp1);
employees.add(emp2);
return employees;
}

}

Step9 : We need to tell our application to scan all Spring beans and find the JPA repository and entity files and register them as Spring bean, so we will use two special annotations 
@SpringBoot and @EnableJpaRepositories, on top of the Spring boot main class. Please note that in @EnableJpaRepositories annotation,  I mentioned the root package from where it start finding the repository and entities
package com.example.layerdArchitechture;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories("com.example.layerdArchitechture")
public class LayerdArchitechtureApplication {

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

@Bean
public CommandLineRunner loadData(InitDataLoader loader) {
return args->loader.loadData();
}
}
Pay attention to the loadData method, this is Java style bean configuration where I call InitLoader's load data method. CommandLiner bean will be invoked when the application starts and inject InitDataLoader as a Spring bean(IOC), So I can expect upon starting my Spring boot application I will insert all the data into the database.
Step10 : Start the application and we can see employees are inserted in the database to verify that go to the following url http://localhost:8080/h2-console we can see the following 
Conclusion: we have successfully insert data using Spring boot, one thing to observe here, To insert data into the database we don't have to write a single line of CRUD code and JPA template configuration code. Spring boot does the same behalf of us. In the second part, I will show you how to expose hose data as a JSON format so any UI framework(Angular5) consume data as JSON or use this layered architecture as a Microservice(Few Changes needs to make it Microservice component), Stay tuned.