在消費端 cloud-comsumer-feign-hystrix-order80 進行演示。
<br/>
步驟如下:
**1. 封裝Feign接口用來訪問服務端**
```java
/**
* fallback:指定異常處理類,PaymentHystrixService接口的某一個方法發生異常時
* 就會去調用fallback類里面對應的方法。
*/
@Component
@FeignClient(value = "${provider.payment.name}", fallback = PaymentHystrixServiceFallback.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/circuit/{id}")
String paymentCircuitBreaker(@PathVariable("id") Integer id);
}
```
**2. 封裝異常處理類`PaymentHystrixServiceFallback`**
```java
@Slf4j
@Component
public class PaymentHystrixServiceFallback implements PaymentHystrixService {
/**
* 當方法 PaymentHystrixService.paymentCircuitBreaker發生異常時就會自動調用該方法
* 做后續處理。
*/
@Override
public String paymentCircuitBreaker(Integer id) {
String info = "paymentCircuitBreaker is exception!";
log.warn(info);
return info;
}
}
```
**3. controller層**
```java
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Resource
private PaymentService paymentService;
@GetMapping("/hystrix/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
String res = paymentService.paymentCircuitBreaker(id);
return res;
}
}
```
**4. 測試**
(1)啟動2個Eureka注冊中心。
```
cloud-eureka-server7001
cloud-eureka-server7002
```
(2)啟動服務端和客戶端。
```
cloud-provider-hystrix-payment8001
cloud-comsumer-feign-hystrix-order80
```
(3)服務端正常,就會得到正常的響應。http://localhost/order/hystrix/circuit/10
```json
hystrix-PaymentServiceImpl-1,成功調用,流水號是:ff2a715e779e4cd68be299b1a17c9ef5
```
(4)將服務端關閉,就會得到如下的響應。http://localhost/order/hystrix/circuit/10
```json
paymentCircuitBreaker is exception!
```
<br/>
可見,當服務端關閉后,消費端調用了方法`PaymentHystrixServiceFallback.paymentCircuitBreaker`進行異常處理。
這樣做就是告訴客戶端,這個服務暫時關閉了,請到其它服務端獲取服務,不要在我這邊等待了,避免不必要的資源浪費和擁堵。
- 微服務
- 微服務是什么?
- 微服務架構
- 微服務優缺點
- 微服務技術棧
- 微服務框架對比
- SpringCloud
- SpringCloud是什么
- SpringCloud與SpringBoot對比
- SpringCloud與Dubbo對比
- Rest微服務案例
- 總體介紹
- 父工程構建步驟
- 公共模塊構建步驟
- 服務端模塊構建步驟
- 消費端模塊構建步驟
- Eureka服務注冊與發現
- Eureka是什么
- Eureka原理
- Eureka注冊服務中心構建
- 向Eureka注冊已有微服務
- Eureka的自我保護機制
- Eureka服務發現
- Eureka集群配置
- Eureka與Zookeeper對比
- Ribbon負載均衡
- Ribbon是什么
- Ribbon負載均衡演示
- 構建服務端模塊
- 構建消費端模塊
- Ribbon核心組件IRule
- 自定義負載均衡策略
- Ribbon均衡策略優先級
- 輪詢策略算法
- OpenFeign負載均衡
- OpenFeign是什么
- 負載均衡演示
- 日志打印功能
- 導出功能
- Hystrix斷路器
- Hystrix是什么
- 服務熔斷
- Hystrix服務端構建
- 服務熔斷演示
- 服務熔斷類型
- HystrixProperty配置匯總
- 服務降級
- Hystrix客戶端構建
- 服務降級演示
- fallbackFactory
- 熔斷與降級
- 服務監控
- 網關服務Zuul
- Zuul是什么
- Zuul路由服務構建
- 設置訪問映射規則
- Config分布式配置中心
- Config分布式配置中心是什么
- Config服務端與Git通信
- Config客戶端獲取配置
- Config客戶端動態刷新
- Bus消息總線
- Bus消息總線是什么
- Bus消息總線原理
- 廣播通知設計思想
- 廣播通知演示
- 定點通知演示
- Stream消息驅動
- 為什么要引入Stream
- Stream消息驅動是什么
- Stream設計思想
- Stream流程和注解
- Stream案例演示
- 重復消費問題
- 消息持久化
- Sleuth分布式鏈路跟蹤
- Sleuth是什么
- 搭建鏈路監控
- SpringCloud Alibaba
- Nacos注冊與配置中心
- Nacos是什么
- 安裝并運行Nacos
- Nacos注冊中心
- 服務端入住Nacos
- 消費端入住Nacos
- Nacos負載均衡演示
- 服務注冊中心對比
- Nacos的AP和CP轉化
- Nacos配置中心
- 基礎配置演示
- Nacos分類配置
- Nacos集群搭建
- Sentinel實現熔斷與限流
- Sentinel是什么
- Sentinel環境搭建
- Sentinel監控微服務演示
- Sentinel流控規則
- 流量監控的作用
- 設置流控規則
- Sentinel降級規則
- 熔斷降級作用
- 設置降級規則
- Sentinel熱點限流
- 什么是熱點
- 設置熱點限流
- Sentinel系統限流
- @SentinelResource
- @SentinelResource屬性
- @SentinelResource限流演示
- @SentinelResource熔斷演示
- 規則持久化
- 熔斷框架比較
- Seata分布式事務
- 分布式事務問題
- Seata是什么
- Seata分布式事務過程
- Seata環境搭建
- 演示示例
- 業務說明
- 數據庫環境準備
- 微服務環境準備
- 測試
- Consul服務注冊與發現
- Consul是什么
- Consul能做什么
- 環境搭建
- Windows平臺
- 服務端入住Consul
- 消費端入住Consul
- 注冊中心對比
- Zookeeper服務注冊與發現
- Zookeeper是什么
- 環境搭建
- 服務端入住Zookeeper
- 消費端入住Zookeeper
- 網關服務Gateway
- Gateway是什么
- Gateway能做什么
- Gateway對比Zuul
- 三大核心概念
- Gateway工作流
- 環境搭建
- 網關路由配置方式
- 配置文件配置
- 代碼中配置
- 動態路由
- Predicate斷言
- 斷言是什么
- 常用斷言
- Filter過濾器
- 過濾器是什么
- 過濾器種類
- 自定義過濾器