Created
June 10, 2018 23:10
-
-
Save OmarElgabry/1b8c733483f3366962ad0d4191ace89d to your computer and use it in GitHub Desktop.
Eureka Gallery Service Controller with Hystrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.eureka.gallery.controllers; | |
| import java.util.List; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.core.env.Environment; | |
| import org.springframework.web.bind.annotation.PathVariable; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RestController; | |
| import org.springframework.web.client.RestTemplate; | |
| import com.eureka.gallery.entities.Gallery; | |
| import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; | |
| @RestController | |
| @RequestMapping("/") | |
| public class HomeController { | |
| @Autowired | |
| private RestTemplate restTemplate; | |
| @HystrixCommand(fallbackMethod = "fallback") | |
| @RequestMapping("/{id}") | |
| public Gallery getGallery(@PathVariable final int id) { | |
| // create gallery object | |
| Gallery gallery = new Gallery(); | |
| gallery.setId(id); | |
| // get list of available images | |
| @SuppressWarnings("unchecked") // we'll throw an exception from image service to simulate a failure | |
| List<Object> images = restTemplate.getForObject("http://image-service/images/", List.class); | |
| gallery.setImages(images); | |
| return gallery; | |
| } | |
| // a fallback method to be called if failure happened | |
| public Gallery fallback(int galleryId, Throwable hystrixCommand) { | |
| return new Gallery(galleryId); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment