在ribbon使用断路器
导入Hystrix包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
启动类加@EnableHystrix注解开启Hystrix
在调用服务的方法上添加 @HystrixCommand(fallbackMethod = "调用失败的方法")注解
在feign使用断路器
application.yml
feign:
hystrix:
enabled: true
调用服务接口中写入实现错误返回的类
@FeignClient(value = "服务名",fallback = SchedualServiceHiHystric.class//错误返回的类)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "sorry "+name;
}
}