- API Gateway
- Eureka
- Hystrix
https://m.youtube.com/watch?v=nElpCWmpSew&list=PLyHJZXNdCXsd2e3NMW9sZbto8RB5foBtp&index=1
Estimated time: 105 minutes;
CitizenService VaccinationCenter
service name: EurekaServer url: http://localhost:8761/
remember to visit the url and check the Eureka page, you will see the service in that page
org.springframework.cloud spring-cloud-starter-netflix-eureka-serverserver:
port:
8761
eureka:
client:
fetch-registry: false
register-with-eureka: false
Only need to add @EnableEurekaServer
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Enable CircuitBreaker and LoadBalanced
@SpringBootApplication @EnableCircuitBreaker public class VaccinationCenterApplication {
public static void main(String[] args) {
SpringApplication.run(VaccinationCenterApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Call another service
@RestController @RequestMapping("/vaccinationcenter") public class VaccinationCenterController { @Autowired private RestTemplate restTemplate;
@GetMapping("/id/{id}")
@HystrixCommand(fallbackMethod = "handleCitizenDownTime")
public ResponseEntity<RequiredResponse> getAllDataBasedonCenterId(@PathVariable Integer id) {
RequiredResponse response = new RequiredResponse();
//get vaccination center detail
VaccinationCenter center = centerRepository.findById(id).get();
response.setCenter(center);
//then get all citizen registered in that center
List<Citizen> listCitizen = restTemplate.getForObject("http://CITIZEN-SERVICE/citizen/id/" + id, List.class);
response.setCitizens(listCitizen);
return new ResponseEntity<RequiredResponse>(response, HttpStatus.OK);
}
public ResponseEntity<RequiredResponse> handleCitizenDownTime(@PathVariable Integer id) {
RequiredResponse response = new RequiredResponse();
//get vaccination center detail
VaccinationCenter center = centerRepository.findById(id).get();
response.setCenter(center);
return new ResponseEntity<RequiredResponse>(response, HttpStatus.OK);
}
}