好的营销网站,百度网站收录提交,怎么查询公司企业邮箱,wordpress 企业版文章目录 一、sentinel简介Sentinel 是什么#xff1f;Sentinel安装 二、sentinel整合工程新建cloudalibaba-sentinel-service8401微服务引入依赖yml配置主启动类添加EnableDiscoveryClient业务类测试 三、sentinel流控规则基本介绍流控模式直接#xff08;默认#xff09;关… 文章目录 一、sentinel简介Sentinel 是什么Sentinel安装 二、sentinel整合工程新建cloudalibaba-sentinel-service8401微服务引入依赖yml配置主启动类添加EnableDiscoveryClient业务类测试 三、sentinel流控规则基本介绍流控模式直接默认关联链路 流控效果快速失败Warm Up预热/冷启动方式排队等待 三、sentinel降级规则Sentinel降级简介Sentinel降级-RTSentinel降级-异常比例Sentinel降级-异常数Sentinel热点keySentinel系统规则 四、Sentinel服务熔断Ribbon环境服务提供者服务消费者Sentinel服务熔断配置fallbackSentinel服务熔断配置blockhandlerSentinel服务熔断exceptionsTolgnore 五、 Sentinel服务熔断OpenFeign六、Sentinel持久化规则# 一、sentinel简介
分布式系统的流量防卫兵sentinel官网地址 添加链接描述 sentinel - github地址 添加链接描述
Sentinel 是什么
随着微服务的流行服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点从流量控制、流量路由、熔断降级、系统自适应过载保护、热点流量防护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
丰富的应用场景Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景例如秒杀即突发流量控制在系统容量可以承受的范围、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
完备的实时监控Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据甚至 500 台以下规模的集群的汇总运行情况。
广泛的开源生态Sentinel 提供开箱即用的与其它开源框架/库的整合模块例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C 等多语言的原生实现。
完善的 SPI 扩展机制Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。Sentinel 的主要特性 Sentinel安装
1、下载Sentinel控制台程序jar文件
Sentinel下载 添加链接描述 2、将控制台程序上传的Linux服务器, 并且通过命令启动
java -jar sentinel-dashboard-1.8.1.jar
命令 nohup java -jar sentinel-dashboard-1.8.1.jar sentinel.log 21
该命令可以让jar程序在后台运行
注意需要服务器预留8080端口3、访问sentinel控制台程序
http://192.168.195.135:8080
注意账号密码都为sentinel二、sentinel整合工程
新建cloudalibaba-sentinel-service8401微服务 引入依赖
!--SpringCloud ailibaba nacos --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId
/dependency
!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到--
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId
/dependency
!--SpringCloud ailibaba sentinel --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependencyyml配置
server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: 192.168.10.132:8848 #Nacos服务注册中心地址sentinel:transport:dashboard: 192.168.10.132:8081 #配置Sentinel dashboard地址port: 8719 #默认8719端口,加入被占用会自动从87191扫描,直到找到未被占用的端口management:endpoints:web:exposure:include: *feign:sentinel:enabled: true # 激活Sentinel对Feign的支持
主启动类添加EnableDiscoveryClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;EnableDiscoveryClient
SpringBootApplication
public class SentinelApp8401 {public static void main(String[] args) {SpringApplication.run(SentinelApp8401.class, args);}
}
业务类
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** 流空*/
RestController
Slf4j
public class FlowLimitController {GetMapping(/testA)public String testA() {return ------testA;}GetMapping(/testB)public String testB() {log.info(Thread.currentThread().getName() \t ...testB);return ------testB;}}测试
1.启动sentinel服务 2.启动 cloudalibaba-sentinel-service8401
访问 http://192.168.10.132:8081 查看服务 因为sentinel采用的是懒加载所以需要执行一次访问即可 访问
http://localhost:8401/testA 三、sentinel流控规则
基本介绍 资源名唯一名称默认请求路径
针对来源Sentinel可以针对调用者进行限流填写微服务名指定对哪个微服务进行限流 默认default(不区分来源全部限制)阈值类型/单机阈值
QPS(每秒钟的请求数量)当调用该接口的QPS达到了阈值的时候进行限流
线程数【关门打狗】当调用该接口的线程数达到阈值时进行限流是否集群
不需要集群流控模式
直接接口达到限流条件时直接限流
关联当关联的资源达到阈值时就限流自己
链路只记录指定链路上的流量指定资源从入口资源进来的流量如果达到阈值就可以限流[api级别的针对来源]流控效果
快速失败直接失败抛异常
Warm Up即请求 QPS 从 threshold / 3 开始经预热时长逐渐升至设定的 QPS 阈值
排队等待匀速排队让请求以匀速的速度通过阈值类型必须设置为QPS否则无效流控模式
直接默认 快速访问 http://localhost:8401/testA 关联
当关联的的资源达到阈值时就限制自己当与A关联的资源B达到阀值后就限流A自己。 使用posaman或者jmter并发访问 testB 访问testA B满了导致A不可用
链路
多个请求调用了同一个微服务
流控效果
快速失败
直接抛出异常Warm Up预热/冷启动方式
官网介绍地址https://github.com/alibaba/Sentinel/wiki/流量控制
默认 coldFactor 为 3即请求QPS从(threshold / 3) 开始经多少预热时长才逐渐升至设定的 QPS 阈值。设置Warm Up
案例阀值为10预热时长设置5秒。
系统初始化的阀值为10 / 3 约等于3,即阀值刚开始为3然后过了5秒后阀值才慢慢升高恢复到10使用场景
如秒杀系统在开启的瞬间会有很多流量上来很有可能把系统打死预热方式就是把为了保护系统可慢慢的把流量放进来慢慢的把阀值增长到设置的阀值。
源码在 com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpController public WarmUpController(double count, int warmUpPeriodInSec) { this.construct(count, warmUpPeriodInSec, 3); } 排队等待 源码在 com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController 排队等待设置:
匀速排队让请求以均匀的速度通过阀值类型必须设成QPS否则无效
设置含义/testA每秒1次请求超过的话就排队等待等待的超时时间为20000毫秒在请求A接口上添加日记打印
log.info(Thread.currentThread().getName() ...TestA);
postman或者jmter设置并发线程 观察日记输出查看等待输出时间
输出的时间是随机的只要看到A后面的资源确实是等待了就可以
2022-09-28 10:27:36.051 INFO 9972 --- [io-8401-exec-10] c.z.s.controller.FlowLimitController : http-nio-8401-exec-10...TestA
2022-09-28 10:27:38.268 INFO 9972 --- [nio-8401-exec-1] c.z.s.controller.FlowLimitController : http-nio-8401-exec-1...TestA
2022-09-28 10:27:40.469 INFO 9972 --- [nio-8401-exec-4] c.z.s.controller.FlowLimitController : http-nio-8401-exec-4...TestA
2022-09-28 10:27:42.653 INFO 9972 --- [nio-8401-exec-6] c.z.s.controller.FlowLimitController : http-nio-8401-exec-6...TestA
2022-09-28 10:27:44.835 INFO 9972 --- [nio-8401-exec-9] c.z.s.controller.FlowLimitController : http-nio-8401-exec-9...TestA
2022-09-28 10:27:47.055 INFO 9972 --- [io-8401-exec-10] c.z.s.controller.FlowLimitController : http-nio-8401-exec-10...TestA
2022-09-28 10:27:49.255 INFO 9972 --- [nio-8401-exec-1] c.z.s.controller.FlowLimitController : http-nio-8401-exec-1...TestA
2022-09-28 10:27:51.467 INFO 9972 --- [nio-8401-exec-4] c.z.s.controller.FlowLimitController : http-nio-8401-exec-4...TestA
2022-09-28 10:27:53.653 INFO 9972 --- [nio-8401-exec-6] c.z.s.controller.FlowLimitController : http-nio-8401-exec-6...TestA
2022-09-28 10:27:55.839 INFO 9972 --- [nio-8401-exec-9] c.z.s.controller.FlowLimitController : http-nio-8401-exec-9...TestA
三、sentinel降级规则
Sentinel降级简介
Sentinel熔断降级会在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高)对这个资源的调用进行限制让请求快速失败避免影响到其它的资源而导致级联错误。
当资源被降级后在接下来的降级时间窗口之内对该资源的调用都自动熔断(默认行为是抛出DegradeException)。
RT(平均响应时间秒级)
平均响应时间 超出阈值且在时间窗口内通过的请求5两个条件同时满足后触发降级窗口期过后关闭断路器
RT最大4900(最大的需要通过-Dcsp.sentinel.statistic.max.rtXXXX才能生效)
异常比例秒级
QPS5且异常比例(秒级统计)超过阈值时触发降级窗口期结束后关闭降级。
异常数
异常数(分钟统计)超过阈值时触发降级时间窗口期结束后关闭降级。
Sentinel降级-RT
1.修改子项目(8401)的FlowLimitController GetMapping(/testD)public String testD() {try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) { e.printStackTrace(); }return ------testD;}2.在sentinel的窗口设置降级
当一秒钟打进来十个线程来调用testD我们希望200毫秒处理完本次任务如果超过200毫秒还没处理完在未来1秒钟的时间窗口内断路器打开服务不可用。
Sentinel降级-异常比例
异常比例当资源的每秒请求量5并且每秒异常总数占通过量的比值超过阈值之后资源进入降级状态即在接下来的时间窗口之内对这个方法的调用都会自动返回异常比例的阈值范围[0.01.0]代表0%-100%。
1.修改子项目(8401)的FlowLimitController
GetMapping(/testD)public String testD() {/*try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}*/int age 10/0;return **********testD异常比例***********;}2.新增降级规则 Sentinel降级-异常数
异常数当资源近1分钟的异常数目超过阈值滞后进行熔断。由于统计时间窗口是分钟级别的若时间窗口小于60s则结束熔断状态后仍可能再进入熔断状态。
时间窗口一定要大于60秒
修改子项目(8401)的FlowLimitController
GetMapping(/testE)public String testE() {int age 10/0;return ********testE异常数********;}运行报错。
2.设置异常数 Sentinel热点key
热点参数限流会统计参数中的热点参数并根据配置的限流阈值与模式对包含热点参数的资源调用进行限流热点参数限流可以看作是一种特殊的流量控制仅包含热点参数的资源调用生效
1.修改子项目(8401)的FlowLimitController
GetMapping(/testHotKey)SentinelResource(value testHotKey,blockHandler deal_testHotKey)public String testHotKey(RequestParam(value p1,required false) String p1,RequestParam(value p2,required false) String p2) {return *****testHotKey successful**********;}public String deal_testHotKey(String p1, String p2, BlockException ex){return ********deal_testHotKey**********;}2.运行 3.配置热点规则 参数索引对访问的第一个参数进行限流
当每秒访问多次的时候就会进行服务熔断
当把blockHandler参数去掉一秒一次就会显示成功页面当一秒访问多次的时候就会直接弹出错误页面(Error Page)。
参数例外项#
普通超过1秒钟一个之后达到阈值1后马上被限流 我们希望p1参数当它是摸个特殊值时它的限流值和平时不一样 假如当p1的值等于5时它的阈值可以达到200
配置参数例外项 当p1的值不等于5时阈值是1
当p1的值等于5时阈值为200
当代码中有错误它不会去报deal_testHotKey,而是会直接显示错误界面。
Sentinel系统规则
系统自适应限流
Sentinel系统自适应限流从整体维度对应用入口流量控制结合应用的Load、CPU使用率、总体平均RT、入口QPS和并发线程数等几个维度的监控指标通过自适应的流控策略、让系统的入口流量和系统的负载达到一个平衡让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。
Load自适应(仅对Linux/Unix-like机器生效)系统的load作为启发指标进行自适应系统保护。当系统load超过设定的启发值且系统当前的并发线程数超过估算的系统容量时才会触发系统保护(BBR阶段)。系统容量有系统的maxQps*minRt估算得出设定参考值一般是CPU cores * 2.5。
CPU usage(1.5.0版本)当系统CPU使用率超过阈值即触发系统保护(取值范围0.0-1.0)比较灵敏。
平均RT当单台机器所有入口流量的平均RT达到阈值即触发系统保护单位是毫秒
并发线程数当单台机器所有入口流量的并发线程数达到阈值即触发系统保护
入口QPS当单台机器上所有入口流量的QPS达到阈值即触发系统保护
SentinelResource配置
修改一个RateLimitController
GetMapping(/byResource)SentinelResource(value byResource,blockHandler handleException)public CommonResult byResource() {return new CommonResult(200,按资源名称限流测试OK,new Payment(2020L,serial001));}public CommonResult handleException(BlockException exception){return new CommonResult(444,exception.getClass().getCanonicalName()\t 服务不可用);}运行8401 设置限流规则 按照Url地址限流
修改RateLimitController
//按照URl限流GetMapping(/rateLimit/byUrl)SentinelResource(value byUrl)public CommonResult byUrl() {return new CommonResult(200,按Url限流测试OK,new Payment(2020L,serial002));}设置限流规则 上面兜底方案面临的问题
系统默认的没有体现我们自己的业务要求。 2.依照现有条件我们自定义的处理方法又和业务代码耦合在一块不直观。每个业务方法都添加一个兜底的那会造成代码膨胀加剧。全局统一的处理方法没有体现。
修改一个RateLimitController
//CustomerBlockHandlerGetMapping(/rateLimit/customerBlockHandler)SentinelResource(value customerBlockHandler,blockHandlerClass CustomerBlockHandler.class,blockHandler handlerException1)public CommonResult customerBlockHandler() {return new CommonResult(200,按客户自定义,new Payment(2020L,serial003));}新建一个CustomerBlockHandler.java
public class CustomerBlockHandler {public static CommonResult handlerException1(BlockException exception){return new CommonResult(444,按客户自定义global handlerException-------1);}public static CommonResult handlerException2(BlockException exception){return new CommonResult(444,按客户自定义global handlerException-------2);}
}配置限流规则 四、Sentinel服务熔断Ribbon环境
服务提供者
1.新建子项目(cloudalibaba-provider-payment9003)
2.引入依赖
dependencies!--SpringCloud ailibaba nacos --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- SpringBoot整合Web组件 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--引入自己定义的api通用包--dependencygroupIdcom.my.springcloud/groupIdartifactIdcloud-api-commons/artifactIdversion${project.version}/version/dependency!--日常通用jar包配置--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdcom.atguigu.springcloud/groupIdartifactIdcloud-api-commons/artifactIdversion1.0-SNAPSHOT/versionscopecompile/scope/dependency/dependencies3.配置文件application.yml
server:port: 9003
spring:application:name: nacos-payment-providercloud:nacos:discovery:server-addr: localhost:8848 #配置nacos地址management:endpoints:web:exposure:include: *4.主启动类
SpringBootApplication
EnableDiscoveryClient
public class PaymentMain9003 {public static void main(String[] args) {SpringApplication.run(PaymentMain9003.class,args);}
}5.新建业务类PaymentController
RestController
public class PaymentController {Value(${server.port})private String serverPort;public static HashMapLong, Payment hashMap new HashMap();static {hashMap.put(1L,new Payment(1L,从入门到放弃));hashMap.put(2L,new Payment(2L,从删库到跑路));hashMap.put(3L,new Payment(3L,从进门到坐牢));}GetMapping(/paymentSQL/{id})public CommonResultPayment paymentSQL(PathVariable(id) Long id) {Payment payment hashMap.get(id);CommonResultPayment result new CommonResult(200,from mysql,serverPort: serverPort,payment);return result;}
}服务消费者
1.新建子项目(cloudalibaba-consumer-nacos-order84)
2.引入依赖
dependenciesdependencygroupIdcom.atguigu.springcloud/groupIdartifactIdcloud-api-commons/artifactIdversion${project.version}/version/dependency!--SpringCloud ailibaba nacos --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- SpringBoot整合Web组件 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!--SpringCloud ailibaba sentinel --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency!--日常通用jar包配置--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies3.application.yml配置文件
server:port: 84spring:application:name: nacos-order-consumercloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8080port: 8719service-url:nacos-user-service: http://nacos-payment-provider4.创建主启动类OrderNacosMain84
SpringBootApplication
EnableDiscoveryClient
public class OrderNacosMain84 {public static void main(String[] args) {SpringApplication.run(OrderNacosMain84.class,args);}
}5.负载均衡配置类ApplicationContextConfig
Configuration
public class ApplicationContextConfig {BeanLoadBalancedpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}6.创建业务类CircleBreakerController
public class CircleBreakerController {public static final String SERVICE_URL http://nacos-payment-provider;Resourceprivate RestTemplate restTemplate;RequestMapping(/consumer/fallback/{id})SentinelResource(value fallback)public CommonResultPayment fallback(PathVariable Long id){CommonResultPayment result restTemplate.getForObject(SERVICE_URL/paymentSQL/id,CommonResult.class,id);if(id4) {throw new IllegalArgumentException(非法参数异常);}else if(result.getData()null) {throw new NullPointerException(该ID没有对应的记录空指针异常);}return result;}
}Sentinel服务熔断配置fallback
在子项目84上业务类CircleBreakerController加上兜底方法
RestController
public class CircleBreakerController {public static final String SERVICE_URL http://nacos-payment-provider;Resourceprivate RestTemplate restTemplate;RequestMapping(/consumer/fallback/{id})SentinelResource(value fallback,fallback handerFallback)public CommonResultPayment fallback(PathVariable Long id){CommonResultPayment result restTemplate.getForObject(SERVICE_URL/paymentSQL/id,CommonResult.class,id);if(id4) {throw new IllegalArgumentException(非法参数异常);}else if(result.getData()null) {throw new NullPointerException(该ID没有对应的记录空指针异常);}return result;}public CommonResult handlerFallback(PathVariable Long id,Throwable e) {Payment payment new Payment(id, null);return new CommonResult(444,兜底异常异常内容e.getMessage(),payment);}
}fallback只负责业务异常。
Sentinel服务熔断配置blockhandler
修改子项目84上业务类CircleBreakerController
RestController
public class CircleBreakerController {public static final String SERVICE_URL http://nacos-payment-provider;Resourceprivate RestTemplate restTemplate;RequestMapping(/consumer/fallback/{id})//SentinelResource(value fallback,fallback handerFallback) //fallback只处理业务出现异常SentinelResource(value fallback,blockHandler blockHandler) //blockHandler只负责sentinel控制台配置违规public CommonResultPayment fallback(PathVariable Long id){CommonResultPayment result restTemplate.getForObject(SERVICE_URL/paymentSQL/id,CommonResult.class,id);if(id4) {throw new IllegalArgumentException(非法参数异常);}else if(result.getData()null) {throw new NullPointerException(该ID没有对应的记录空指针异常);}return result;}/*public CommonResult handlerFallback(PathVariable Long id,Throwable e) {Payment payment new Payment(id, null);return new CommonResult(444,兜底异常异常内容e.getMessage(),payment);}*/public CommonResult handlerFallback(PathVariable Long id, BlockException blockException) {Payment payment new Payment(id, null);return new CommonResult(445, blockHandler-sentinel限流无此流水 blockException.getMessage(), payment);}
}添加降级规则 blockHandler只负责sentinel控制台配置违规 Sentinel服务熔断配置fallback和blockhandler#
若blockHandler和fallback都进行了配置则被限流降级而抛出BlockException时只会进入blockHandler处理逻辑。
Sentinel服务熔断exceptionsTolgnore
修改子项目84上业务类CircleBreakerController
RestController
public class CircleBreakerController {public static final String SERVICE_URL http://nacos-payment-provider;Resourceprivate RestTemplate restTemplate;RequestMapping(/consumer/fallback/{id})//SentinelResource(value fallback,fallback handerFallback) //fallback只处理业务出现异常//SentinelResource(value fallback,blockHandler blockHandler) //blockHandler只负责sentinel控制台配置违规//SentinelResource(value fallback,fallback handerFallback,blockHandler blockHandler)SentinelResource(value fallback,fallback handerFallback,blockHandler blockHandler,exceptionsToIgnore {IllegalArgumentException.class})public CommonResultPayment fallback(PathVariable Long id){CommonResultPayment result restTemplate.getForObject(SERVICE_URL/paymentSQL/id,CommonResult.class,id);if(id4) {throw new IllegalArgumentException(非法参数异常);}else if(result.getData()null) {throw new NullPointerException(该ID没有对应的记录空指针异常);}return result;}public CommonResult handlerFallback(PathVariable Long id,Throwable e) {Payment payment new Payment(id, null);return new CommonResult(444,兜底异常异常内容e.getMessage(),payment);}public CommonResult handlerFallback(PathVariable Long id, BlockException blockException) {Payment payment new Payment(id, null);return new CommonResult(445, blockHandler-sentinel限流无此流水 blockException.getMessage(), payment);}
}运行 假如4报异常不再有fallback方法兜底没有降级效果了。
五、 Sentinel服务熔断OpenFeign
修改子项目(84)
pom.xml
!--SpringCloud openfeign --
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId
/dependency修改application.yml
# 激活Sentinel对Feign的支持
feign:sentinel:enabled: true在主启动类上面加上注解开启Feign
EnableFeignClients
创建service接口
FeignClient(value nacos-payment-provider,fallback PaymentFallbackService.class)
public interface PaymentService {GetMapping(value /paymentSQL/{id})public CommonResultPayment paymentSQL(PathVariable(id) Long id);
}创建接口实现类PaymentFallbackService
Component
public class PaymentFallbackService implements PaymentService {Overridepublic CommonResultPayment paymentSQL(Long id) {return new CommonResult(444,服务降级返回----PaymentFallbackService,new Payment(id,errorService));}
}在业务类CirleBreakerController添加
Resourceprivate PaymentService paymentService;GetMapping(value /paymentSQL/{id})public CommonResultPayment paymentSQL(PathVariable(id) Long id){return paymentService.paymentSQL(id);}运行
当9003停止运行后84会 六、Sentinel持久化规则#
一旦我们重启应用sentinel规则将会消失生产环境需要将配置规则进行持久化。
将限流配置规则持久化进Nacos保存只要刷新8401某个rest地址sentinel控制台的流控规则就能看到只要Nacos里面的配置不删除针对8401上的sentinel上的流控规则持续有效。
修改子项目(8401)
application.yml
server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: localhost:8848sentinel:transport:dashboard: localhost:8080 #配置Sentinel dashboard地址port: 8719 #默认8719端口如果被占用会自动从8719开始依次1直到找到未被占用的端口#8719端口是应用和Sentinel控制台交互的端口datasource:ds1:nacos:server-addr: localhost:8848dataId: cloudalibaba-sentinel-servicegroupId: DEFAULT_GROUPdata-type: jsonrule-type: flow
management:endpoints:web:exposure:include: *在nacos里面新建配置 resource资源名称
limitApp来源应用
grade阈值类型0表示线程数、1表示QPS
count单机阈值
strategy流控模式0表示直接1表示关联2表示链路
controlBehavior流控效果0表示快速失败1表示Warm up2表示排队等待
clusterMode是否集群
启动8401后刷新sentinel发现业务规则有了