【微服务~Sentinel】Sentinel降级、限流、熔断_云原生

🔎这里是【微服务~Sentinel】,关注我学习微服务不迷路
👍如果对你有帮助,给博主一个免费的点赞以示鼓励
欢迎各位🔎点赞👍评论收藏⭐️

👀专栏介绍

【微服务~Sentinel】 目前主要更新微服务,一起学习一起进步。

👀本期介绍

本期主要介绍Sentinel

文章目录

​微服务常见概念​

​服务雪崩​

​服务熔断​

​服务降级​

​熔断和降级的区别​

​Sentinel介绍​

​core降级​

​现象1​

​现象2​

​降级操作​

微服务常见概念

官网:​​quick-start​

服务雪崩

  • 服务雪崩:在整条链路的服务中,一个服务失败,导致整条链路的服务都失败的情形。
  1. 存在整条链路服务(Service A、Service B、Service C)
  2. Service A 流量突然性增加,导致Service B 和Service C 流量也增加。
  3. Service C 因为抗不住请求,变得不可用。导致Service B的请求变得阻塞。
  4. 当Service B的资源耗尽,Service B就会变得不可用。
  5. 最后 Service A 不可用。

【微服务~Sentinel】Sentinel降级、限流、熔断_原力计划_02

服务熔断

  • 服务熔断:当下游的服务因为某种原因突然变得不可用响应过慢,上游服务为了保证自己整体服务的可用性,不再继续调用目标服务,直接返回,快速释放资源。如果目标服务情况好转则恢复调用。

【微服务~Sentinel】Sentinel降级、限流、熔断_云原生_03

  1. 最开始处于​​closed​​状态,一旦检测到错误到达一定阈值,便转为​​open​​状态;
  2. 这时候会有个 reset timeout,到了这个时间了,会转移到​​half open​​状态;
  3. 尝试放行一部分请求到后端,一旦检测成功便回归到​​closed​​状态,即恢复服务;  

服务降级

  • 什么是服务降级呢?
  • 当下游的服务因为某种原因响应过慢,下游服务主动停掉一些不太重要的业务,释放出服务器资源,增加响应速度!
  • 当下游的服务因为某种原因不可用,上游主动调用本地的一些降级逻辑,避免卡顿,迅速返回给用户!

熔断和降级的区别

  • 服务熔断和服务降级的区别?
  • 服务降级有很多种降级方式!如开关降级、限流降级、熔断降级!
  • 服务熔断属于降级方式的一种!
  • 当发生下游服务不可用的情况,熔断和降级必定是一起出现。
  • 服务降级大多是属于一种业务级别的处理,熔断属于框架层级的实现
  • 开关降级

在配置中心配置一个开关(变量),在配置中心更改开关,决定哪些服务进行降级

Sentinel介绍

  • Sentinel :一个高可用的流量控制与防护组件,保障微服务的稳定性。
  • Sentinel分为两个部分,sentinel-core与sentinel-dashboard。
  • sentinel-core 部分能够支持在本地引入sentinel-core进行限流规则的整合与配置。
  • sentinel-dashboard 则在core之上能够支持在线的流控规则与熔断规则的维护与调整等。

core降级

【微服务~Sentinel】Sentinel降级、限流、熔断_云原生_04

现象1

  • 提供者搭建集群(8170/8270),调用者调用,此时关闭提供者的一个服务(8270)
  • 存在现象:访问8170成功访问,不能访问8270
  • 略有卡顿,稍等片刻后,不再卡顿。

现象2

  • 提供者搭建集群(8170/8270),调用者调用,此时关闭提供者的所有服务
  • 现象:无法访问

【微服务~Sentinel】Sentinel降级、限流、熔断_Sentinel_05

降级操作

添加坐标

【微服务~Sentinel】Sentinel降级、限流、熔断_原力计划_06

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

修改yml文件,开启feign对sentinel的支持

【微服务~Sentinel】Sentinel降级、限流、熔断_微服务_07

feign:
sentinel:
enabled: true

 修改启动类,开启feign

【微服务~Sentinel】Sentinel降级、限流、熔断_原力计划_08

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
@EnableDiscoveryClient //服务发现
@EnableFeignClients //远程调用
public class TestNacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(TestNacosConsumerApplication.class, args );
}
}

 修改Feign实现

package com.czxy.feign;

import org.springframework.stereotype.Component;

@Component
public class EchoFeignFallback implements EchoFeign {
@Override
public String echo(String string) {
return "降级处理:" + string;
}
}
package com.czxy.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


// @FeignClient(value = "服务名", path = "controller配置的路径" )
@FeignClient(value = "service-provider", fallback = EchoFeignFallback.class )
public interface EchoFeign {

// 与 nacos-provider-2.1>EchoController声明的方法的完全一致
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string);
}

关闭服务提供者,测试

  • 注意:需重启服务

【微服务~Sentinel】Sentinel降级、限流、熔断_云原生_09

 

【微服务~Sentinel】Sentinel降级、限流、熔断_微服务_10