前言:Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。
1,到官网下载sentinel-jar包,运行命令启动:
java -jar sentinel-dashboard-1.8.4.jar
2,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sentinelDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3,application.yaml如下:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
server:
port: 9000
4,添加2个测试接口代码如下:
package com.muke.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author yhq
* @version 1.0
* @date 2022/7/19 19:09
*/
@RestController
public class TestController {
@Autowired
private TestController testController;
@RequestMapping("/list")
public String list(){
testController.queryGoods();
return "list";
}
@RequestMapping("/list1")
public String list1(){
//这里不能直接调用,需要用代理对象调用,否则sentinel无法识别,测试链路模式
testController.queryGoods();
return "list1";
}
@SentinelResource("goods")
public void queryGoods(){
System.err.println("查询商品");
}
/**
* 模拟0.5秒请求一次list1接口,测试流控模式:关联
*/
public static void main(String[] args) throws InterruptedException {
while (true){
RestTemplate restTemplate = new RestTemplate();
String forObject = restTemplate.getForObject("http://localhost:9000/list1", String.class);
System.out.println(forObject);
Thread.sleep(500L);
}
}
}
5,浏览器运行:http://localhost:8080/#/dashboard/home
账号密码:sentinel/sentinel (默认)
注意:首次进入需要请求一次对应接口sentinel才行读取到
一,流控模式:关联
1,测试流控模式:关联
2,启动main方法
3,接着请求list接口,出现bolck
二,流控模式:链路
1,添加对应配置bean
package com.muke;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/**
* @author yhq
* @version 1.0
* @date 2022/7/19 19:04
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
// 注解支持的配置Bean
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}
2,添加yaml配置
spring:
cloud:
sentinel:
web-context-unify: false # 关闭context整合 让链路模式生效;默认为true,链路模式失效。
3,添加@SentinelResource到非Springmvc的方法上
注意:
Sentinel
控制对Spring mvc接口层级的控制,那么在我们实际项目开发时不仅仅限于接口,可能对于某个方法的调用限流,对于某个外部资源的调用限流等都希望做到控制。
那么如何使用@SentinelResource
注解灵活的定义控制资源以及如何配置控制策略。
4,添加sentinel配置
实现效果为:
1,list ---> googs qps必须小于12,list1 ---> goods 不影响
达到阈值如下图: