Spring Cloud Feign 整合 Sentinel 實現服務降級與熔斷保護
在微服務架構中,服務之間的調用往往依賴 Feign,而服務調用的穩定性又至關重要。本文將介紹如何將 Feign 與 Sentinel 結合使用,實現服務的容錯保護(如降級與熔斷),提升系統的健壯性與可用性。
一、引入依賴
我們創建一個新的微服務,作為 Feign 調用方。pom.xml
中添加如下依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.1.RELEASE</version>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.2.1.RELEASE</version>
</dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.2.RELEASE</version>
</dependency>
二、配置 application.yml
在配置文件中開啟 Feign 對 Sentinel 的支持,并配置基本信息:
server:port: 8380feign:sentinel:enabled: true # 開啟 Sentinel 對 Feign 的支持spring:application:name: feigncloud:sentinel:transport:dashboard: localhost:8080 # Sentinel 控制臺地址nacos:discovery:server-addr: 127.0.0.1:8848 # Nacos 注冊中心地址
三、創建 Feign 接口
我們通過 Feign 調用名為 provider
的服務:
package com.southwind.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient("provider")
public interface ProviderFeign {@GetMapping("/index")String index();
}
四、編寫 Controller
通過注入 Feign 接口,實現接口調用:
package com.southwind.controller;import com.southwind.feign.ProviderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class FeignController {@Autowiredprivate ProviderFeign providerFeign;@GetMapping("/index")public String index(){return providerFeign.index();}
}
五、自定義 Fallback 實現服務降級
當服務不可用或 Sentinel 觸發熔斷時,我們希望返回友好的降級提示,這就需要自定義 fallback 類:
1. 創建 fallback 實現類
package com.southwind.fallback;import com.southwind.feign.ProviderFeign;
import org.springframework.stereotype.Component;@Component
public class ProviderFeignFallback implements ProviderFeign {@Overridepublic String index() {return "index-fallback";}
}
2. 修改 FeignClient 注解,指定 fallback
package com.southwind.feign;import com.southwind.fallback.ProviderFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value = "provider", fallback = ProviderFeignFallback.class)
public interface ProviderFeign {@GetMapping("/index")String index();
}
Spring Cloud Feign 整合 Sentinel 實現服務降級與熔斷保護
在微服務架構中,服務之間的調用往往依賴 Feign,而服務調用的穩定性又至關重要。本文將介紹如何將 Feign 與 Sentinel 結合使用,實現服務的容錯保護(如降級與熔斷),提升系統的健壯性與可用性。
一、引入依賴
我們創建一個新的微服務,作為 Feign 調用方。pom.xml
中添加如下依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.1.RELEASE</version>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId><version>2.2.1.RELEASE</version>
</dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.2.RELEASE</version>
</dependency>
二、配置 application.yml
在配置文件中開啟 Feign 對 Sentinel 的支持,并配置基本信息:
server:port: 8380feign:sentinel:enabled: true # 開啟 Sentinel 對 Feign 的支持spring:application:name: feigncloud:sentinel:transport:dashboard: localhost:8080 # Sentinel 控制臺地址nacos:discovery:server-addr: 127.0.0.1:8848 # Nacos 注冊中心地址
三、創建 Feign 接口
我們通過 Feign 調用名為 provider
的服務:
package com.southwind.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient("provider")
public interface ProviderFeign {@GetMapping("/index")String index();
}
四、編寫 Controller
通過注入 Feign 接口,實現接口調用:
package com.southwind.controller;import com.southwind.feign.ProviderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class FeignController {@Autowiredprivate ProviderFeign providerFeign;@GetMapping("/index")public String index(){return providerFeign.index();}
}
五、自定義 Fallback 實現服務降級
當服務不可用或 Sentinel 觸發熔斷時,我們希望返回友好的降級提示,這就需要自定義 fallback 類:
1. 創建 fallback 實現類
package com.southwind.fallback;import com.southwind.feign.ProviderFeign;
import org.springframework.stereotype.Component;@Component
public class ProviderFeignFallback implements ProviderFeign {@Overridepublic String index() {return "index-fallback";}
}
2. 修改 FeignClient 注解,指定 fallback
package com.southwind.feign;import com.southwind.fallback.ProviderFeignFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value = "provider", fallback = ProviderFeignFallback.class)
public interface ProviderFeign {@GetMapping("/index")String index();
}
3.啟動類添加@EnableFeignClients注解
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {public static void main(String[] args) {SpringApplication.run(FeignApplication.class, args);}}
六、運行與測試
- 啟動 Sentinel Dashboard(默認端口為 8080)。
- 啟動被調用服務
provider
。 - 啟動當前項目
feign
。 - 瀏覽器訪問:
http://localhost:8380/index
- 停止
provider
服務,再次訪問,即返回 fallback 內容:index-fallback
。
七、總結
模塊 | 說明 |
---|---|
Feign | 簡化 HTTP 請求調用 |
Sentinel | 實現限流、熔斷與降級保護 |
fallback | 實現服務不可用時的降級邏輯 |
配置關鍵點 | 開啟 feign.sentinel.enabled=true 并配置 fallback |
通過本文的實踐,我們實現了 Feign + Sentinel 的無縫集成,使服務調用具備更強的容錯能力。建議大家在生產環境中為每個 Feign 接口都配置 fallback,防止服務雪崩。
如有幫助,歡迎點贊、評論、收藏!
后續將分享更多微服務高可用相關內容,敬請關注。
六、運行與測試
- 啟動 Sentinel Dashboard(默認端口為 8080)。
- 啟動被調用服務
provider
。 - 啟動當前項目
feign
。 - 瀏覽器訪問:
http://localhost:8380/index
- 停止
provider
服務,再次訪問,即返回 fallback 內容:index-fallback
。
七、總結
模塊 | 說明 |
---|---|
Feign | 簡化 HTTP 請求調用 |
Sentinel | 實現限流、熔斷與降級保護 |
fallback | 實現服務不可用時的降級邏輯 |
配置關鍵點 | 開啟 feign.sentinel.enabled=true 并配置 fallback |
通過本文的實踐,我們實現了 Feign + Sentinel 的無縫集成,使服務調用具備更強的容錯能力。建議大家在生產環境中為每個 Feign 接口都配置 fallback,防止服務雪崩。
如有幫助,歡迎點贊、評論、收藏!
后續將分享更多微服務高可用相關內容,敬請關注。