在微服务架构中,我们将业务拆分成很多的服务,服务与服务之间可以互相调用,但是由于一些原因,服务并不能保证服务的100%可用,如果单个服务出现问题,调用这个服务就会出现网络延迟,此时如果有大量的请求,会形成任务堆积,导致服务雪崩。
如何解决服务雪崩?
Spring Cloud提供了一系列的组件。
本章代码已分享至Gtiee :https://gitee.com/lengcz/springcloudalibaba01.git
Sentinel是阿里开源的一套用于服务容错的解决方案。它以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来保护服务的稳定性。
Sentinel分为两部分
下载地址:https://github.com/alibaba/Sentinel/releases

启动dashboard,sentinel-dashboard 是一个springboot项目
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar
直接访问 http://localhost:8080/,默认账号名sentinel,密码是sentinel

在order模块中
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
/**
* 服务容错示例
*/
@RestController
@Slf4j
public class HelloController {
@GetMapping("/hello1")
public String hello() {
log.info("say hello------------1");
return "hello1";
}
@GetMapping("/hello2")
public String hello2() {
log.info("say hello------------2");
return "hello2";
}
}
spring:
cloud:
sentinel:
transport:
port: 12345 #与控制台交流的端口,可随意指定,不冲突即可。
dashboard: localhost:8080 # sentinel-dashboard的地址

可以看到实时请求的数据
