一 说明
1.1 架构说明
本案例实现原理: 采用alibaba的nacos,openfegin,sentinel,gateway等组件实现熔断限流。
主要理解sentinel的@ResouceSentinel和fallback的区别联系。
@ResourceSentinel 主要是页面配置熔断限流规则;
fallback主要是针对 jvm业务io异常进行逻辑处理。

1.2 工程结构
1.消费者

2.openfeginApi

3.gateway

4.提供者

1.3 注意点
openapi这里请求路径,多了一个空格,提示服务fallback,注意路径要写正确。


二 实施案例
2.1 消费者
1.pom文件

2.核心业务代码

2.2 工具open-api
1.核心接口
注意: 这里@Feginclients 配置的服务为网关的服务名称:ms-alibaba-sentinel-gateway

2.3 网关Gateway
1.pom配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
</dependency>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 一定要加这个依赖,否则 网关配置lb://服务名 不起作用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.核心业务代码: 这里配置请求,1s超过2次,触发网关设置限流规则。

3.application配置文件
2.4 服务提供者
1.核心业务代码

2.5 启动服务
1.nacos
E:\nacos-server2.2.3\bin>startup.cmd -m standlone
2.sentinel
F:\>java -jar sentinel-dashboard-1.8.6.jar
3.业务服务启动

2.6 验证
1.正常访问:通过8081 访问网关7777 转发到 提供端9091
http://localhost:8081/consumer/pay/nacos/sgo/444

2.频繁刷新**:触发网关配置的限流规则**

3.在openfeigin设置fallback策略
实现类

打包,重新启动业务服务:
正常访问

频繁刷新:触发fallback处理异常
