既然用到了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. zmq使用记录

    zmq套接字介绍 https://www.cnblogs.com/fengbohello/p/4354989.html zmq示例 https://github.com/booksbyus/zguid ...

  2. 用1 x 2的多米诺骨牌填满M x N矩形的方案数(完美覆盖)

    题意 用 $1 \times 2$ 的多米诺骨牌填满 $M \times N$ 的矩形有多少种方案,$M \leq 5,N < 2^{31}$,输出答案模 $p$. 分析 当 $M=3$时,假设 ...

  3. Tensorflow细节-P84-梯度下降与批量梯度下降

    1.批量梯度下降 批量梯度下降法是最原始的形式,它是指在每一次迭代时使用所有样本来进行梯度的更新.从数学上理解如下: 对应的目标函数(代价函数)即为: (1)对目标函数求偏导: (2)每次迭代对参数进 ...

  4. (转载) 搭建非域AlwaysOn win2016+SQL2016

    非域搭建Alwayson只是省去搭建域控那一部分,其他大同小异 条件: 操作系统:windows server 2016 数据库:SQL Server 2016 SSMS版本:17.3 节点1:HDD ...

  5. 使用session存储数据

    @WebServlet("/reply") public class ReplyServlet extends HttpServlet { @Override protected ...

  6. mac 安装 报错 "/usr/local/include/stdint.h:2:10: error: #include nested too deeply"

    报错详细信息 构建错误 - “#include嵌套太深” /usr/local/include/stdint.h:2:10: error: #include nested too deeply #in ...

  7. leetcode解题报告(31):Kth Largest Element in an Array

    描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

  8. 2017.10.3 国庆清北 D3T2 公交车

    题目描述 LYK在玩一个游戏. 有k群小怪兽想乘坐公交车.第i群小怪兽想从xi出发乘坐公交车到yi.但公交车的容量只有M,而且这辆公交车只会从1号点行驶到n号点. LYK想让小怪兽们尽可能的到达自己想 ...

  9. 安装pdo_dblib扩展连接SQLserver

    1.先得安装freetdswget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.1.5.tar.gztar zxvf freetds-1.1. ...

  10. [golang]golang 汇编

    https://lrita.github.io/2017/12/12/golang-asm/#why 在某些场景下,我们需要进行一些特殊优化,因此我们可能需要用到golang汇编,golang汇编源于 ...