Seninel是面向分布式服务框架的轻量级限流控制组件,主要以流量为切入点,从限流,流量,整形,服务降级,系统负载保护等多个维度来帮助我们保障微服务的稳点性。
主要特性:
Sentinel 分为核心库和控制台两个部分。


终端执行以下命令:
java -jar sentinel-dashboard-1.8.6.jar

启动成功后默认端口号为8080:访问:
http://localhost:8080,页面如图所示,从Sentinel 1.6.0开始,Sentinel引入了基本的登录功能,默认用户名和密码都是sentinel。

输入用户名和密码后页面如下:

Spring Cloud Aliibaba默认Sentinel整合了Servlet,RestTemplate,FeignClient和Spring WebFlux,它不仅补全了Hystrix在Selevet和RestTemplete这一块的空白,而且还完善兼容了Hystrix在FeignClient中限流降级的用法,并支持灵活配置和调整流控规则。
本次演示使用的springboot和springcloud版本均为:
2.2.1.RELEASE
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2.2.1.RELEASEversion>
dependency>
spring:
application:
name: student-server
cloud:
sentinel:
transport:
# 配置可视化界面地址
dashboard: localhost:8080
/**
* @author gf
* @date 2022/11/13
*/
@RestController
@RequestMapping(path = "/api/v1/student")
public class StudentController {
@GetMapping("/list")
public MultiResponse<String> getList() {
List<String> list = new ArrayList<String>();
list.add("springboot");
list.add("springcloud");
return MultiResponse.buildSuccess(list);
}
}
用postman对接口进行请求,然后查看可视化界面


从界面上可以看到,我们能对接口一分钟请求了4次
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>2.2.1.RELEASEversion>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>2.2.1.RELEASEversion>
dependency>
dependencies>
OpenFeign中开启Sentinel
feign:
sentinel:
enabled: true
OpenFeign远程调用接口(为了方便测试,这里的url可以随便写一个):
@FeignClient(name = "teacher-server", url = "localhost:8085/api/v1/teacher", fallback=StudentSentinelService.class)
@Component
public interface StudentService {
@GetMapping("/getNotice")
public String getNotice();
}
熔断器接口接口:
@Component
public class StudentSentinelService implements StudentService {
@Override
public String getNotice() {
return "sentinel熔断";
}
}
测试controller接口:
@Component
public class StudentSentinelService implements StudentService {
@Override
public String getNotice() {
return "sentinel熔断";
}
}
SpringCloud Alibaba Sentinel整合SpringCloud OpenFeign,启动报错解决方案

熔断接口执行了。