本项目演示如何使用 Sentinel 完成 Spring Cloud 应用的熔断降级调用。java
Sentinel 是阿里巴巴开源的分布式系统的流量防卫组件,Sentinel 把流量做为切入点,从流量控制,熔断降级,系统负载保护等多个维度保护服务的稳定性。git
OpenFeign是一款声明式、模板化的HTTP客户端, Feign能够帮助咱们更快捷、优雅地调用HTTP API,须要了解OpenFeign使用基础,能够参考cloud-feign示例源码。github
本项目服务注册中心使用nacos,服务提供者使用Spring Cloud Alibaba(一) 如何使用nacos服务注册和发现建立的ali-nacos-provider服务web
随着微服务的流行,服务和服务之间的稳定性变得愈来愈重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。spring
Sentinel 具备如下特征:json
Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量能够承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。app
Sentinel 同时提供实时的监控功能。您能够在控制台中看到接入应用的单台机器秒级数据,甚至 500 台如下规模的集群的汇总运行状况。框架
Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只须要引入相应的依赖并进行简单的配置便可快速地接入 Sentinel。分布式
Sentinel 提供简单易用、完善的 SPI 扩展接口。您能够经过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。ide
对调用链路中不稳定的资源进行熔断降级是保障高可用的重要措施之一。因为调用关系的复杂性,若是调用链路中的某个资源不稳定,最终会致使请求发生堆积。Sentinel 熔断降级会在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高),对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而致使级联错误。当资源被降级后,在接下来的降级时间窗口以内,对该资源的调用都自动熔断(默认行为是抛出 DegradeException)
建立ali-nacos-provider项目
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
@RestController @Slf4j public class HelloController { @GetMapping(value = "/hello/{str}", produces = "application/json") public String hello(@PathVariable String str) { log.info("-----------收到消费者请求-----------"); log.info("收到消费者传递的参数:" + str); String result = "我是服务提供者,见到你很高兴==>" + str; log.info("提供者返回结果:" + result); return result; } }
建立ali-nacos-sentinel-feign项目
1.首先,pom.xml添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies>
2.定义FeignClient,及其降级配置
package com.easy.ansFeign.service; import com.easy.ansFeign.fallback.HelloServiceFallbackFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "ali-nacos-provider", fallbackFactory = HelloServiceFallbackFactory.class) public interface HelloService { /** * 调用服务提供方的输出接口. * * @param str 用户输入 * @return hello result */ @GetMapping("/hello/{str}") String hello(@PathVariable("str") String str); }
@Component public class HelloServiceFallbackFactory implements FallbackFactory<HelloServiceFallback> { @Override public HelloServiceFallback create(Throwable throwable) { return new HelloServiceFallback(throwable); } }
public class HelloServiceFallback implements HelloService { private Throwable throwable; HelloServiceFallback(Throwable throwable) { this.throwable = throwable; } /** * 调用服务提供方的输出接口. * * @param str 用户输入 * @return */ @Override public String hello(String str) { return "服务调用失败,降级处理。异常信息:" + throwable.getMessage(); } }
@RestController public class TestController { @Autowired private HelloService helloService; @GetMapping("/hello-feign/{str}") public String feign(@PathVariable String str) { return helloService.hello(str); } }
在Spring Cloud Alibaba(一) 如何使用nacos服务注册和发现基础上,咱们新建了ali-nacos-sentinel-feign项目,并调用ali-nacos-provider项目用做该示例的服务提供方,有如下二个项目作测试。
ali-nacos-provider:服务提供者,服务名:ali-nacos-provider,端口:9000
ali-nacos-sentinel-feign:服务消费者,服务名:ali-nacos-sentinel-feign,端口:9102
首先要启动服务注册中心 nacos、ali-nacos-provider服务及ali-nacos-sentinel-feign服务
返回
我是服务提供者,见到你很高兴==>yuntian
表示咱们的服务成功调用到了
返回
服务调用失败,降级处理。异常信息:com.netflix.client.ClientException: Load balancer does not have available server for client: ali-nacos-provider
表示执行了咱们预约的回调,服务成功降级了。