Spring Cloud Alibaba Sentinel 整合 Feign 实现服务熔断保护
<|begin▁of▁sentence|># 1. 概述
本文分享 **Spring Cloud Alibaba 之 Sentinel 与 Feign 的整合**。Sentinel 提供了对 Feign 的支持,让我们可以非常方便的在 Feign 调用时,使用 Sentinel 进行保护。
> 友情提示:对 Sentinel 基本概念不熟悉的胖友,可以阅读[《Sentinel 官方文档 —— 介绍》](https://github.com/alibaba/Sentinel/wiki/介绍)进行学习。考虑到胖友的英语水平,可以阅读[《Spring Cloud Alibaba 官方文档 —— 熔断器 Sentinel》](https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel)的中文版本。
在开始本文之前,胖友需要对 Feign 进行简单的了解。可以阅读[《Spring Cloud 源码分析 —— Feign》](http://www.iocoder.cn/Spring-Cloud/Feign/?self)文章。
# 2. 如何集成
在 Spring Cloud Alibaba 中,Sentinel 和 Feign 的整合,通过 **spring-cloud-alibaba-sentinel** 来完成。那么,我们需要引入该依赖,如下图所示:
```xml
org.springframework.cloud
spring-cloud-starter-alibaba-sentinel
```
同时,因为我们要使用 Feign 进行声明式 HTTP 调用,所以需要引入 **spring-cloud-starter-openfeign** 依赖,如下图所示:
```xml
org.springframework.cloud
spring-cloud-starter-openfeign
```
# 3. 快速入门
> 示例代码对应仓库:
>
> - 服务提供者:`labx-10-sc-sentinel-demo01-provider`
> - 服务消费者:`labx-10-sc-sentinel-feign-demo01-consumer`
本小节,我们来搭建一个 Sentinel 整合 Feign 的示例。步骤如下:
- 首先,搭建一个服务提供者 `demo-provider`,提供 HTTP 接口。
- 然后,搭建一个服务消费者 `demo-consumer`,使用 Feign 调用服务提供者 `demo-provider` 的 HTTP 接口。同时,引入 Sentinel 进行保护。
## 3.1 搭建服务提供者
创建 [**labx-10-sc-sentinel-demo01-provider**](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-10-spring-cloud-alibaba-sentinel/labx-10-sc-sentinel-demo01-provider) 项目,作为服务提供者 `demo-provider`。最终项目代码如下图所示:
### 3.1.1 引入依赖
创建 [`pom.xml`](https://github.com/YunaiV/SpringBoot-Labs/blob/master/labx-10-spring-cloud-alibaba-sentinel/labx-10-sc-sentinel-demo01-provider/pom.xml) 文件,引入 Spring Cloud、Spring Boot、Nacos Discovery 相关依赖。代码如下:
```xml
labx-10
cn.iocoder.springboot.labs
1.0-SNAPSHOT
4.0.0
labx-10-sc-sentinel-demo01-provider
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
```
最新文章
- 2023年自动驾驶与固态电池技术革命性突破
- 电动汽车三大核心技术突破:电池、电驱与充电新趋势
- 电动化与智能化驱动:2023全球汽车产业变革趋势
- 域控制器:智能汽车的大脑中枢与未来演进
- 轮胎压力监测安全带提醒汽车安全驾驶
- 激光雷达与3D点云技术:开启自动驾驶新时代
- 英菲尼迪豪华座驾新体验
- 智能座舱、线控底盘与固态电池:汽车科技三大革新趋势
- 轮胎磨损影响汽车行驶安全定期检查
- 高速公路ABS防抱死系统ESP电子稳定程序智能网联汽车驾驶行为车辆状态保费制动系统第三者责任险车辆损失险盗抢险
- MyBatis入门指南:从零搭建第一个CRUD程序
- 硅基负极技术突破:电动车续航里程将迎革命性提升
- 车龄与保养指南:不同阶段养护重点及费用解析
- 车载摄像头实时监测车道偏离预警
- 电动化浪潮下:电池技术突破与智能驾驶新纪元
- 车损险全解析:保障范围、理赔流程与投保建议
- 三元锂电池VS磷酸铁锂:能量密度如何决定电动车续航里程
- 自动驾驶革命:激光雷达与深度学习重塑未来出行
- 汽车四轮定位全解析:前束角与外倾角调整指南
- 毫米波雷达:智能驾驶的安全守护者与未来趋势
