"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : MicroServices Communication Date : 01/07/2025 (Session - 78) _____________________________________________________________________________________________________________________________ Feign Client / Open Feign: ========================= * It is also one type of Rest Client given by Netflix Organization. * Earlier Netfilx Organization used the Feign client for their Internal projects for communication between microservices. * In the last two years Netflix organization stopped the feign client Development because They moved this Project Netflix open source software Community that why name of Feign client got changed to open Feign. * Spring provided abstraction layer on open Feign under sub project of "Spring-cloud" module. * Spring cloud open Feign is an declartive type of developing Rest Client. * As programmer we just need to develop Feign clients as simple "interfaces" and we no need to worry implementation of this interfaces will be taken care Feign. * we need to add the following starter to work with Feigh client i.e.,openfeign * When working with spring cloud open feign we are using main two anotations 1) @EnableFeignClients >>>>> It will scan & identify all the Feign clients available in our project. 2) @FeignClient("name") >>>>> This annotation is used to represent interface as Feign Client. * UpStream & DownStream DownStream Customer Microservices ---------------------> Address Microservices UpStream <-------------------- Address Microservices Example ======= AddressClient.java ================== package com.ashokit.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import com.ashokit.response.AddressResponse; import com.ashokit.response.AddressResponseList; @FeignClient(url= "${address.service.name.url}",name="Address-Service") public interface AddressClient { @GetMapping(value = "/") public ResponseEntity fetchAllAddresses(); @GetMapping(value = "/customer/{customerId}") public ResponseEntity fetchAddressByCustomerId(@PathVariable("customerId") Integer customerId); } application.properties ====================== #server port changing server.port=9977 #Database Configuration spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe spring.datasource.username=system spring.datasource.password=manager #Hibernate Configuration spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true #Application name configuration spring.application.name=Customer-Service #Address Microservices Configuration address.service.name.url=http://localhost:9966/api/address/ Service Discovery ================= * Typically in microservice development we do have multiple microservices as programmer it is very hard to remember everything about Microservice. * To overcome the problem the service registry will maintain all microservices related information i.e.,API URL's,How many Instances are running for that service,Information about Service,Service is up or down and also used for communicating from one microservice to another Microservices using Service Names instead of Hard coded URL's. * Spring cloud provides the multiple Service discovery API's such as Eureka,ZooKeeper,Consul,Cloud foundry etc., * As part of this ServiceDiscovery we will creating three spring boot applications 1) Service Registry Application : To Track all the Microservices in our project i.e.,Eureka Server Application 2) Customer-Service : This Application need to be registered as one of Eureka Client 3) Address-Service : This Application need to be registered as one of Eureka Client. Developing the Service Registry Application(40_MicroServices_ServiceDiscovery_App) ================================================================================== 1) Create the Spring Boot Application with Below straters * Spring Web * Eureka Server 2) Add the following annotation i.e.,@EnableEurekaServer top of main class Application.java ================ package com.ashokit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //Activates the Eureka Server and its configuration @EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 3) Add the eureka server configuration entries in application.yml/application.properties application.properties ====================== #Default Port number for Eureka Server server.port=8761 #Disabling the Registration of this project with Eureka Server eureka.client.register-with-eureka=false #Disabling the Discovery operation of this project with Eureka Server eureka.client.fetch-registry=false NOTE: ===== **** By Default Eureka Server always run on port number is 8761 4) Run the Spring Boot Applcation and open your browser and hit below URL http://localhost:8761 >>>>>>>>>>>>>>> We can see the Eureka Server Dashboard Screen +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++