SpringCloudAlibaba的熔断降级规则

雪崩效应

由于服务之间的调用,B调用A;由于A服务出现故障,导致B请求的A的线程阻塞等待,当超过一定线程数量时候,B服务的内存达到最大值,最总导致B服务挂掉!

雪崩效应解决方案

设置线程超时
设置限流
熔断器Sentinel Hystrix
     降级
     限流
     熔断

第一种方案

pom.xml文件配置

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-actuator</artifactId>
		</dependency>

application的配置文件

# 激活所有的端点的web方式请求
management.endpoints.web.exposure.include=*
# sentinel的服务地址
spring.cloud.sentinel.transport.dashboard=localhost:8080

配置sentinel监控服务器

下载sentinel-dashboard-1.8.0.jar,并且运行 java -jar sentinel-dashboard-1.8.0.jar
输入请求的服务器地址: http://localhost:8080/#/dashboard/home
输入用户名字和密码:sentinel sentinel
进入
需要请求访问的地址连接,那么才会在控制台显示监控

输入请求连接
http://localhost:8084/index

在sentinel 的监控页面可以看到请求QPS;
在这里插入图片描述

服务降级学习(默认是直连)

服务降级,就是对不怎么重要的服务进行低优先级的处理   

在这里插入图片描述
限制请求的次数 一秒中只能有一次请求

关联关联

请求的路径list超过单机阈值,那么直接index这边报错
模拟发送list请求

 public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        for (int i = 0; i < 1000; i++) {
            restTemplate.getForObject("http://localhost:8084/list",String.class);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

那么在请求index会直接报错
在这里插入图片描述

链路限流

需要引入相关的jar
<dependency>
	<groupId>com.alibaba.csp</groupId>
	<artifactId>sentinel-web-servlet</artifactId>
	<version>1.7.1</version>
</dependency>
<dependency>
	<groupId>com.alibaba.csp</groupId>
	<artifactId>sentinel-core</artifactId>
	<version>1.7.1</version>
</dependency>

配置文件增加关闭

# 过滤器关闭
spring.cloud.sentinel.filter.enabled=false

增加一个配置类

@Configuration
public class FilterConfiguration {
    /**
     * 链路规则
     * @return
     */
    @Bean
    public FilterRegistrationBean registration(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new CommonFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY,"false");
        registrationBean.setName("sentinelFilter");
        return registrationBean;
    }
}

在Service层增加代码

@Service
public class ProviderService {
    @SentinelResource("test")
    public void test(){
        System.out.println("test");
    }
}

控制层增加代码

 @GetMapping("/test1")
    public String test1(){
        this.providerService.test();
        return "test1";
    }
    @GetMapping("/test2")
    public String test2(){
        this.providerService.test();
        return "test2";
    }

现在sentinel控制面板中的数据
在这里插入图片描述
配置链路限制流量
在这里插入图片描述
限制test1访问,直接报错!
在这里插入图片描述

流控效果(限流之后如何处理)

在这里插入图片描述
第一种: fastFail 就是直接报错

第二种:WarmUp(预热)
在这里插入图片描述
五秒之内的阈值是1,当过五秒之后恢复正常的阈值3(一秒之内可以请求三次)
第三种: 排队等待
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Cloud Alibaba提供了Sentinel作为熔断降级的解决方案。下面是使用Spring Cloud Alibaba进行熔断降级的步骤: 1. 添加依赖 在pom.xml中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> ``` 2. 配置Sentinel 在application.properties或application.yml中添加以下配置: ```yaml spring.cloud.sentinel.transport.dashboard=localhost:8080 spring.cloud.sentinel.datasource.ds1.nacos.server-addr=localhost:8848 spring.cloud.sentinel.datasource.ds1.nacos.dataId=${spring.application.name}-sentinel spring.cloud.sentinel.datasource.ds1.nacos.groupId=DEFAULT_GROUP ``` 其中,`spring.cloud.sentinel.transport.dashboard`为Sentinel Dashboard的地址,`spring.cloud.sentinel.datasource.ds1.nacos.server-addr`为Nacos服务的地址,`spring.cloud.sentinel.datasource.ds1.nacos.dataId`为Sentinel规则持久化在Nacos上的Data ID。 3. 编写熔断降级规则 在Sentinel Dashboard中编写熔断降级规则,例如: - 熔断规则:当QPS超过阈值时,进行熔断 - 降级规则:当服务出现异常时,进行降级处理 4. 使用@SentinelResource注解 在需要进行熔断降级的方法上使用@SentinelResource注解,例如: ```java @SentinelResource(value = "hello", fallback = "fallback") public String hello() { // ... } public String fallback() { // ... } ``` 其中,value为资源名称,fallback为降级处理的方法名。 这样,当资源名称为hello的方法发生熔断降级时,会自动调用fallback方法进行处理。 5. 启动应用 启动应用后,可以在Sentinel Dashboard中查看实时的熔断降级信息和统计数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值