版权声明:本人博客欢迎转载,但是请表明原链接和出处! https://blog.csdn.net/Phone_1070333541/article/details/87979438
修改服务提供者项目,此处只修改8001,其他的照葫芦画瓢即可
一、pom.xml
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
二、controller
修改 controller
@GetMapping("/get/{id}")
@HystrixCommand(fallbackMethod = "processHystrix_get") // 出错则调用指定的方法
public Dept getDept(@PathVariable("id") Long id){
Dept dept = deptService.findById(id);
if(dept == null){
throw new RuntimeException("数据库中没有查到对应信息");
}
return dept;
}
public Dept processHystrix_get(@PathVariable("id")Long id){
return new Dept().setDeptno(id).setDname("该id"+id+"没有对应信息-->Hystrix").setDb_source("no this database in MYSQL");
}
三、启动类
@EnableCircuitBreaker // 开启熔断器
四、启动测试
- 访问服务消费者
- http://localhost/consumer/dept/get/342
- 返回自己新建的错误信息的dept类