Professional Documents
Culture Documents
*) 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.
f. Use RestTemplate and make HTTP call using URL and ResponseType.
_______________________________________________________________________
***********************************************************************
Spring Cloud : DiscoveryClient (DC-Full Code)
***********************************************************************
------------------------------------------------------------------------
1. Eureka Server
Name : SpringCloudDCEurekaServer
Dep : Eureka Server
2. Producer Application
Name : SpringCloudDCEmpApp
Dep : Eureka Discovery Client, Spring web
---application.properties--
server.port=9900
#ServiceId
spring.application.name=EMPLOYEE-APP
#Eureka Details
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
-------------------------
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
---application.properties--
server.port=8086
#ServiceId
spring.application.name=DEPT-APP
#Eureka Details
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
-------------------------
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 {