参考:
https://www.cnblogs.com/crazymakercircle/p/14285001.html
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2.2.1.RELEASEversion>
dependency>
spring: # 增加sentinel相关配置
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8080 # sentinel 控制台配置
port: 8719 #sentinel 对外服务监听端口
web-context-unify: false
# 整合feign
feign:
sentinel:
enabled: true
@SentinelResource(value = “buy”) 用来标识资源是否被限流,降级
返回值类型必须与原函数返回值类型一致;
方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 函数签名要求:
exceptionsToIgnore(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出
//===========controller======//
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/hello")
public String hello(){
return userService.sayHello();
}
}
//===========Service======//
@Service
public class UserService {
//定义了降级接口以及降级回调
@SentinelResource(value = "sayHello",fallback = "sayHellofail")
public String sayHello(){
return "Hello,World";
}
public String sayHellofail(){
return "I'am sorry";
}
}
spring:
application:
name: nacos-provider
cloud:
# 配置的是ncoas服务端地址
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml #指定配置中心里面配置文件格式
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
dashboard: 127.0.0.1:8080
port: 8719
datasource:
ds1:
nacos:
server-addr: 127.0.0.1:8848
dataID: alibaba-sentinel-service
groupId: DEFAULE_GROUP
data-type: json
rule-type: flow
[
{
"reource":"/rateLimit/byUrl",
"limitApp":"default",
"grade":1,
"count":1,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
}
]
resource:资源名
limit:来源应用
grade:阈值类型,O标识线程数,1标识QPS
count:单机阈值
strategy:流控模式,0直接,1关联,2链路
controlBehavior:流控效果,0快速失败,1warmup,2排队等待
clusterMode:是否集群
<dependency>
<groupId>com.tulinggroupId>
<artifactId>03-ms-alibaba-feign-apiartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
2、添加降级回调
@FeignClient(name = "product-center",fallback = ProductCenterFeignApiWithSentinelFallback.class)
/*
@FeignClient(name = "product-center",fallbackFactory = ProductCenterFeignApiWithSentielFallbackFactory.class)
*/
public interface ProductCenterFeignApiWithSentinel {
/**
* 声明式接口,远程调用http://product-center/selectProductInfoById/{productNo}
* @param productNo
* @return
*/
@RequestMapping("/selectProductInfoById/{productNo}")
ProductInfo selectProductInfoById(@PathVariable("productNo") String productNo);
}
//===============指定回调类的方式====================//
@Component
public class ProductCenterFeignApiWithSentinelFallback implements ProductCenterFeignApiWithSentinel {
@Override
public ProductInfo selectProductInfoById(String productNo) {
ProductInfo productInfo = new ProductInfo();
productInfo.setProductName("默认商品");
return productInfo;
}
}
//============指定回调工厂的方式==============//
@Component
@Slf4j
public class ProductCenterFeignApiWithSentielFallbackFactory implements FallbackFactory<ProductCenterFeignApiWithSentinel> {
@Override
public ProductCenterFeignApiWithSentinel create(Throwable throwable) {
return new ProductCenterFeignApiWithSentinel(){
@Override
public ProductInfo selectProductInfoById(String productNo) {
ProductInfo productInfo = new ProductInfo();
if (throwable instanceof FlowException) {
log.error("流控了....{}",throwable.getMessage());
productInfo.setProductName("我是被流控的默认商品");
}else {
log.error("降级了....{}",throwable.getMessage());
productInfo.setProductName("我是被降级的默认商品");
}
return productInfo;
}
};
}
}
https://www.cnblogs.com/crazymakercircle/p/14285001.html
https://blog.csdn.net/weixin_40990818/article/details/107617857
https://blog.csdn.net/Delicious_Life/article/details/106967199
https://blog.csdn.net/ai_xao/article/details/107682626
https://cloud.tencent.com/developer/article/1452017
https://www.yisu.com/zixun/318190.html