Table of Contents
What have we learned so far,
PART 1 – MICROSERVICES INTRODUCTION – In this tutorial, we discussed what microservices architecture is and how its different from monolithic architecture.
Microservices Scenario to develop
In this part we will discuss about the example that we are going to develop in our microservices architecture.Suppose a customer have an bank account and they both are interlinked with a has a relationship.Customer has a account and account has a customer. We will create 2 different services which will serve 2 resources as customer and account.
1. Lets create account-service and customer-service Spring Boot project with below information
2. Lets write rest controller and bean for customer resource
Customer Bean
package com.onlyfullstack.customerservice.bean; public class Customer { private Long id; private String name; private String address; public Customer(Long id, String name, String address) { super(); this.id = id; this.name = name; this.address = address; } 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; } }
Customer Rest Controller
package com.onlyfullstack.customerservice.controller; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.onlyfullstack.customerservice.bean.Customer; @RestController public class CustomerController { private List<Customer> listOfCustomers = new ArrayList<>(5); public CustomerController(){ Customer customer1 = new Customer(new Long(1), "Saurabh Oza", "Pune"); Customer customer2 = new Customer(new Long(2), "Amit Sharma", "Delhi"); Customer customer3 = new Customer(new Long(3), "Vivek Sinha", "Mumbai"); this.listOfCustomers.add(customer1); this.listOfCustomers.add(customer2); this.listOfCustomers.add(customer3); } @GetMapping("/customers") public List<Customer> getAllCustomers(){ return this.listOfCustomers; } @GetMapping("/customers/{customer_id}") public Customer getCustomerDetails(@PathVariable Long customer_id) { return this.listOfCustomers.stream().filter(customer -> customer.getId().equals(customer_id)).findAny().get(); } }
package com.onlyfullstack.accountservice.bean; public class Account { private Long id; private Customer customer; private Double balance; public Account(Long id, Customer customer, Double balance) { super(); this.id = id; this.balance = balance; this.customer = customer; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } }
Account Rest Controller
package com.onlyfullstack.accountservice.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.onlyfullstack.accountservice.bean.Account; import com.onlyfullstack.accountservice.bean.Customer; @RestController() public class AccountController { private List<Account> listOfAccounts = new ArrayList<>(5); @Autowired RestTemplate restTemplate; public AccountController(){ Account account1 = new Account(new Long(1), null, new Double(1111.50)); Account account2 = new Account(new Long(2), null, new Double(1112.50)); Account account3 = new Account(new Long(3), null, new Double(1113.50)); this.listOfAccounts.add(account1); this.listOfAccounts.add(account2); this.listOfAccounts.add(account3); } @GetMapping("/accounts") public List<Account> getAllCustomers(){ return this.listOfAccounts; } @GetMapping("/accounts/{account_id}") public Account getCustomerDetails(@PathVariable Long account_id) { Account account = this.listOfAccounts.stream().filter(acc -> acc.getId().equals(account_id)).findAny().orElse(null); Map<String, Long> params = new HashMap<>(); params.put("customer_id", account.getId()); Customer customer = restTemplate.getForObject("http://localhost:8081/customers/{customer_id}" , Customer.class, params); System.out.println("************** Cust found : "+customer); account.setCustomer(customer); return account; } }
Output
Lets run them with spring boot starter class.
customer-service
Source Code
Download source code of microservice with spring cloud from below git repository :
Lets go to our next tutorial where we will discuss
Part 3 – Feign Client
In this tutorial we will understand below topics
– What is Feign Client?
– How microservices interact with each other?
– How to use Feign Client in microservices?
PART 3 – MICROSERVICES WITH SPRING CLOUD : FEIGN CLIENT
very helpful, very nice explanation. Thanks
Very much neatly explained