既然用到了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降级处理实例的更多相关文章

  1. 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例

    先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...

  2. 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例

    sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例

    sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...

  4. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  5. 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例

    gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...

  6. 0.9.0.RELEASE版本的spring cloud alibaba nacos实例

    简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...

  7. Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探

    目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...

  8. Spring Cloud Alibaba | Sentinel: 服务限流基础篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...

  9. Spring Cloud Alibaba | Sentinel: 服务限流高级篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...

随机推荐

  1. 题解 洛谷P2258 【子矩阵】

    应该很容易想到暴力骗分. 我们考虑暴力\(dfs\)枚举所有行的选择,列的选择,每次跑一遍记下分值即可. 时间复杂度:\(O(C_n^r \times C_m^c \times r \times c) ...

  2. DT6.0下搜索页解决canonical获取乱码问题

    最近研究dt6.0,官方内核默认是把搜索页屏蔽的,但是做seo的人都知道,搜索页聚合是争取排名的好地方,所以我就二次开发搜索页,具体可以查看前几期分享的,今天说说关于搜索的canonical的url乱 ...

  3. 样条函数后续(java)--可在hive中执行的函数

    之前写的样条插值算法只能在本地执行,但是我想要的是可在hive中执行的jar包,为了符合我的要求,经过痛苦.气愤.悲伤等一系列过程,终于实现了: 想要实现可在hive中执行的jar包,以下是具体步骤: ...

  4. C#向数据库中插入或更新null空值

    一.在SQL语句中直接插入null或空字符串“” int? item = null; item == null ? "null" : item.ToString(); item = ...

  5. 学习Spring-Data-Jpa(六)---spring-data-commons中的repository

    1.spring-data-commons项目 spring-data-commons项目是所有spring-data项目的核心,我们来看一下该项目下的repository包中的接口和注解. 2.Re ...

  6. ML,DL核心数学及算法知识点总结

    ML,DL核心数学及算法知识点总结:https://mp.weixin.qq.com/s/bskyMQ2i1VMNiYKIvw_d7g

  7. HTTP文件上传

    看到网上很多链接文件(word.pdf...)可以下载,想制作http下载链接. 其实是将某文件直接放在服务器上搭建的网站上某目录下即可,例如:http://xxx:port/UpgradePack/ ...

  8. AtCoder Grand Contest 004题解

    传送门 \(A\) 咕咕 int a,b,c; int main(){ scanf("%d%d%d",&a,&b,&c); if((a&1^1)|( ...

  9. JS 星号 * 处理手机号和名称

    有时候为了不让用户看到手机号和姓名,需要针对手机号和姓名进行 * 处理. 比如: 13140121671  =>  "131****1671" '王宝宝'  =>  & ...

  10. php md5算法

    <!DOCTYPE html> <html> <body> <?php $str = "Shanghai"; echo md5($str) ...