You are on page 1of 4

Date : 02/02/2021

Spring Boot 7AM


Mr. RAGHU
---------------------------
intra communication :MS
Full Code Link:
https://www.mediafire.com/file/n7f67xf7tsbvdia/
SpringCloud_7AM_02022021_RAGHU_DC.zip/file

*) Client :
a. DiscoveryClient
b. LoadBalancerClient
c. **FeignClient**

=> DiscoveryClient will never make any HTTP request with Producer App.
It only gets data(ServiceInstances) from Eureka Server using ServiceId.
=> By using this data, we can use RestTemplate and make HTTP calls.

---------DiscoveryClient-------------------------------------------
=> DiscoveryClient object is auto-configured by Eureka Discovery Client
Dependency. So, we can use it with @Autowired.

*)Note:
------------------------------------------------
Interface Impl class
------------------------------------------------
DiscoveryClient EurekaDiscoveryClient (netflix)
ServiceInstance EurekaServiceInstance (netflix)
------------------------------------------------

--coding flow----
a. Autowire DiscoveryClient object in consumer.

b. use method : getInstances(serviceId) by passing serviceId


that gets List<ServiceInstance> from Eureka Server.

c. Read one ServiceInstance object using List#index '0'(zero).

d. call getUri() method that returns URI object at runtime


(Example URI => http://localhost:8080/ , No Controller, method paths)

e. Add Paths(of Producer), ie Controller Path and Method Path to make


URL (ex: http://localhost:8080/emp/show)

f. Use RestTemplate and make HTTP call using URL and ResponseType.

g. Finally return Response back to ConsumerApp RestController

_______________________________________________________________________
***********************************************************************
Spring Cloud : DiscoveryClient (DC-Full Code)
***********************************************************************
------------------------------------------------------------------------
1. Eureka Server
Name : SpringCloudDCEurekaServer
Dep : Eureka Server

> At starter class : @EnableEurekaServer


--application.properties----
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
--------------------------------------------------------------------

2. Producer Application
Name : SpringCloudDCEmpApp
Dep : Eureka Discovery Client, Spring web

> At starter class : @EnableEurekaClient

---application.properties--
server.port=9900
#ServiceId
spring.application.name=EMPLOYEE-APP
#Eureka Details
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
-------------------------

> RestController class


package in.nareshit.raghu.rest;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/emp")
public class EmployeeRestController {

@GetMapping("/show")
public ResponseEntity<String> showMsg() {
return ResponseEntity.ok("FROM EMPLOYEE SERVICE!!");
}
}
--------------------------------------------------
3. Consumer Application
Name : SpringCloudDCDeptApp
Dep : Eureka Discovery Client, Spring web

> At starter class : @EnableEurekaClient

---application.properties--
server.port=8086
#ServiceId
spring.application.name=DEPT-APP
#Eureka Details
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
-------------------------

> RestController class


package in.nareshit.raghu.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import in.nareshit.raghu.consumer.EmployeeRestConsumer;

@RestController
@RequestMapping("/dept")
public class DeptRestController {

@Autowired
private EmployeeRestConsumer consumer;

@GetMapping("/info")
public ResponseEntity<String> getInfo() {
String data = consumer.getEmpData();
String body = "FROM DEPT APP!" + data;
return ResponseEntity.ok(body);
}
}

***RestConsumer***
package in.nareshit.raghu.consumer;

import java.net.URI;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class EmployeeRestConsumer {

//1. Autowire Discovery Client object


@Autowired
private DiscoveryClient client;

public String getEmpData() {


//2. Fetch List<SI> using Producer ServiceId
List<ServiceInstance> list = client.getInstances("EMPLOYEE-APP");

//3. Read one instance from list index#0


ServiceInstance si = list.get(0);

//4. Read URI from SI


URI uri = si.getUri();

//5. Create URL


//String url = "http://localhost:9900/emp/show";
String url = uri + "/emp/show";

//6. Use RestTemplate class obj


RestTemplate rt = new RestTemplate();

//7. Make HTTP call


ResponseEntity<String> resp = rt.getForEntity(url, String.class);
//8. Read Reponse Body
String respBody = resp.getBody();

//9. return back to consumer's RestController


return respBody;
}
}
--------Execution Flow-----------------------------------------
1. Run Eureka Server App
2. Run Producer App
3. Run Consumer App
4. Goto Eureka check all instances
(http://localhost:8761/)
5. Click on Consumer URL
(ex: http://192.168.0.8:8086/actuator/info)
6. Modify Full URL
http://192.168.0.8:8086/dept/info
7. Validate Response
------------------------------------------------------------

You might also like