为了防止雪崩效应,需要增加断熔器
jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
#
@Controller
@RequestMapping("/dept")
public class DeptController {
@Autowired
private EurakeService eurakeService;
@RequestMapping(value="find",method=RequestMethod.GET)
@ResponseBody
@HystrixCommand(fallbackMethod="fallBackFind")
//当访问这个方法失败的时候、访问fallBackFind方法、但是两个方法返回值传参必须一样
public List<Dept> find(){
List<Dept> list = eurakeService.findAll();
System.out.println(list.size());
return list;
}
public List<Dept> fallBackFind(){
List<Dept> list = new ArrayList<Dept>();
Dept dept = new Dept();
dept.setDeptno(0);
dept.setDname("测试部门");
dept.setLoc("武汉");
list.add(dept);
return list;
}
}
启动文件
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableDiscoveryClient
public class StartProject {
public static void main(String[] args) {
SpringApplication.run(StartProject.class, args);
}
}