雪崩效应
由于服务之间的调用,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(一秒之内可以请求三次)
第三种: 排队等待