0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理。我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加上去:
1、pom引入sentinel依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2、application配置哨兵控制台地址、开启feign+sentinel:
#哨兵
spring.cloud.sentinel.transport.dashboard=localhost:8080
#打开sentinel
feign.sentinel.enabled=true
3、启动类在@FeignClient注解中指定降级处理类和配置类:
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class TransConsumerApplication { public static void main(String[] args) {
SpringApplication.run(TransConsumerApplication.class, args);
} @Slf4j
@RestController
static class TestController { @Autowired
private ApplicationApi applicationApi; @GetMapping("/sayhello")
public String sayhello() {
return "say: " + applicationApi.hello();
} @GetMapping("/sayhey")
public String sayhey() {
return "say: " + applicationApi.hey();
} } @FeignClient(name = "lxytrans-provider", fallback = TestFallback.class, configuration = FeignConfiguration.class)
interface ApplicationApi { @GetMapping("/hello")
String hello(); @GetMapping("/hey")
String hey(); } class TestFallback implements ApplicationApi { @Override
public String hello() {
return "hello feign fallback.";
} @Override
public String hey() {
return "hey feign fallback.";
}
} class FeignConfiguration {
@Bean
public TestFallback testFallback() {
return new TestFallback();
}
} }
跑起来后,调用一把这两个接口,可以发现哨兵控制台多了消费方:

此处无需配置流控、降级,我们的服务方维持原来的流控、降级处理(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例)。调用sayhey,服务方时延2秒,超时直接熔断:

再让消费方通过jmeter调用sayhello,前面3个正常返回,后面两个因为服务方限流而熔断:

0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例的更多相关文章
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例
先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例
sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例
sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例
这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例
gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos实例
简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...
- Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探
目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...
- Spring Cloud Alibaba | Sentinel: 服务限流基础篇
目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...
- Spring Cloud Alibaba | Sentinel: 服务限流高级篇
目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...
随机推荐
- 七.Protobuf3 嵌套类型
Protobuf3 嵌套类型 您可以在其他消息类型中定义和使用消息类型,如下例所示:这里Result消息是在SearchResponse消息中定义的: message SearchResponse { ...
- LSTM神经网络输入输出究竟是怎样的?
LSTM图和词向量输入分析
- drf框架 - JWT认证插件
JWT认证 JWT认证方式与其他认证方式对比: 优点 1) 服务器不要存储token,token交给每一个客户端自己存储,服务器压力小 2)服务器存储的是 签发和校验token 两段算法,签发认证的效 ...
- PCL安装与配置
一.配置环境 1.win7 64位2.Visual Studio 2015 二 .准备工作 安装包准备: 移步:https://www.cnblogs.com/weiyouqing/p/8046387 ...
- oracle: jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
https://www.cnblogs.com/mmlw/p/5808072.html org.mybatis.spring.MyBatisSystemException: nested except ...
- com.alibaba.fastjson把JSONObject转换为Map<String, String>对象
https://www.cnblogs.com/fomeiherz/p/6351287.html JSONObject obj = new JSONObject();{obj.put("ke ...
- 洛谷 P1082 同余方程 题解
每日一题 day31 打卡 Analysis 题目问的是满足 ax mod b = 1 的最小正整数 x.(a,b是正整数) 但是不能暴力枚举 x,会超时. 把问题转化一下.观察 ax mod b = ...
- codevs:1313 质因数分解:已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 。
#include<iostream>#include<cstdio>#include<cmath>using namespace std;int a[2];int ...
- Comet OJ - Contest #11题解
传送门 \(A\) 咕咕咕 const int N=1e6+5; char s[N],t[N];int n,res; inline bool cmp(const int &x,const in ...
- YII框架的模块化技术
一.模块的创建 利用yii的自动生成工具gii生成模块. 1.访问:lcoalhost/web/index.php?r=gii 2.点击 Module Generator 下面的 start 3.填写 ...