微服務之間調用 這一篇寫了經過ribbon、feign、http方式實現微服務通訊,這一篇斷路器基於上篇git
pom依賴:spring
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
啓動類添加 @EnableHystrix 註解bash
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableHystrix public class SalesApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SalesApplication.class,args); } }
接口以下,爲了演示不一樣熔斷方式,這裏有兩個 Feign 方式接口app
@RequestMapping("/sales") public interface SalesRest { @RequestMapping(value = "/queryGoodsListByRibbon", method = RequestMethod.GET) Object queryGoodsListByRibbon(); @RequestMapping(value = "/queryGoodsListByFeign", method = RequestMethod.GET) Object queryGoodsListByFeign(); @RequestMapping(value = "/queryGoodsListByFeign2", method = RequestMethod.GET) Object queryGoodsListByFeign2(); @RequestMapping(value = "/queryGoodsListByHttp", method = RequestMethod.GET) Object queryGoodsListByHttp(); }
通用方式熔斷:在方法上添加 @HystrixCommand(fallbackMethod = "CommonFallback") ,指定目標方法調取不到後執行 CommonFallback() 熔斷方法ide
@RestController public class SalesRestImpl implements SalesRest { @Autowired private SalesService salesService; @Override @HystrixCommand(fallbackMethod = "CommonFallback") public String queryGoodsListByHttp() { return salesService.queryGoodsListByHttp(); } @Override @HystrixCommand(fallbackMethod = "CommonFallback") public String queryGoodsListByRibbon() { return salesService.queryGoodsListByRibbon(); } @Override @HystrixCommand(fallbackMethod = "CommonFallback") public String queryGoodsListByFeign() { return salesService.queryGoodsListByFeign(); } @Override public String queryGoodsListByFeign2() { return salesService.queryGoodsListByFeign(); } public String CommonFallback() { return "CommonFallback"; } }
具體方法調用:微服務
@Service public class SalesService { @Autowired RestTemplate restTemplate; @Autowired UserFeignClient userFeignClient; private static final String RIBBON_URL = "http://user:8082/user/getUserInfo"; private static final String HTTP_URL = "http://127.0.0.1:8082/user/getUserInfo"; private static final String IP = IpUtil.getIp(); public String queryGoodsListByRibbon() { String sales_result = "queryGoodsListByRibbon success : [sales_ip:" + IP + "] "; String result = restTemplate.getForObject(RIBBON_URL, String.class); return sales_result + result; } public String queryGoodsListByFeign() { String sales_result = "queryGoodsListByFeign success : [sales_ip:" + IP + "] "; String result = (String) userFeignClient.getUserInfo(); return sales_result + result; } public String queryGoodsListByHttp() { String sales_result = "queryGoodsListByHttp success : [sales_ip:" + IP + "] "; String result = HttpClientUtil.doGet(HTTP_URL); return sales_result + result; } }
Feign 熔斷:給 FeignClient 接口指定一個 fallback 實現類,而後該類實現 FeignClient 的接口,這樣接口調不通時便會調用 fallback 實現類中的相應方法:spa
@FeignClient(name = "USER", fallback = UserFeignClientFallback.class) public interface UserFeignClient { @RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET) String getUserInfo(); }
UserFeignClientFallback.class.net
@Component public class UserFeignClientFallback implements UserFeignClient { @Override public String getUserInfo() { return "feign client fallback"; } }
啓動 eureka 和 sales,不啓動 user ,觀察接口輸出rest
http://127.0.0.1:8081/sales/queryGoodsListByRibbon --> CommonFallback http://127.0.0.1:8081/sales/queryGoodsListByHttp --> CommonFallback http://127.0.0.1:8081/sales/queryGoodsListByFeign --> CommonFallback http://127.0.0.1:8081/sales/queryGoodsListByFeign2 --> queryGoodsListByFeign success : [sales_ip:192.168.0.102] feign client fallback
core-simple:https://code.aliyun.com/995586041/core-simple.gitcode
eureka:https://code.aliyun.com/995586041/eureka.git
hystrix-sales:https://code.aliyun.com/995586041/hystrix-sales.git
hystrix-user:https://code.aliyun.com/995586041/hystrix-user.git