SpringCloud 4: hystrix-propagation, springcloud
Note: The configuration of pom. xml and configuration file is the same as that in the previous article.
Package com. itmuch. cloud. controller;
Import org. springframework. beans. factory. annotation. Autowired;
Import org. springframework. web. bind. annotation. GetMapping;
Import org. springframework. web. bind. annotation. PathVariable;
Import org. springframework. web. bind. annotation. RestController;
Import org. springframework. web. client. RestTemplate;
Import org. springframework. web. context. annotation. SessionScope;
Import com. itmuch. cloud. entity. User;
Import com. netflix. hystrix. contrib. javanica. annotation. HystrixCommand;
Import com. netflix. hystrix. contrib. javanica. annotation. HystrixProperty;
/**
* The second day of the short term
* @ Author z
*
*/
@ RestController
Public class MovieController {
@ Autowired
Private RestTemplate restTemplate;
@ GetMapping ("/movie/{id }")
// @ HystrixCommand and findById are called in the same thread
// If not, the findById is a thread, and @ HystrixCommand is an isolated thread, which is equivalent to two threads.
// Normally, no configuration is required. If an exception is thrown
@ HystrixCommand (fallbackMethod = "findByIdFallback", commandProperties = @ HystrixProperty (name = "execution. isolation. strategy", value = "SEMAPHORE "))
Public User findById (@ PathVariable Long id ){
Return this. restTemplate. getForObject ("http: // microservice-provider-user/simple/" + id, User. class );
}
Public User findByIdFallback (Long id ){
User user = new User ();
User. setId (0L );
Return user;
}
}