SpringCloud四:hystrix-propagation

注:pom.xml 及配置文件配置与上篇相同

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;
/**
* 短路器第二天
* @author z
*
*/
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate;

@GetMapping("/movie/{id}")
//表示@HystrixCommand与findById方法会在同一个线程中调用
//如果不配合的话findById是一个线程,@HystrixCommand是一个隔离的线程相当于两个线程
//正常情况下不需要配置,等抛异常了在配置
@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;
}
}

原文地址:https://www.cnblogs.com/a8457013/p/7736038.html