1、下载Sentinel控制台
下载地址: https://github.com/alibaba/Sentinel/releases
启动 : java -jar sentinel-dashboard-1.6.3.jar 默认端口为8080
或 java -Dserver.port=8088 -jar sentinel-dashboard-1.6.3.jar 指定端口
访问:http://localhost:8080 , 用户名和密码:sentinel
.
2、springboot pom.xml
使用 com.alibaba.cloud 下的 spring-cloud-starter-alibaba-sentinel
引入:<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version> 对应SpringBoot 2.x
</dependency>
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.mmt</groupId>
<artifactId>SpringBootWeb</artifactId>
<version>0.01</version>
<name>SpringBootWeb</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring-cloud-starter-alibaba-sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.application.name: SpringBootWeb
#server
server.port=80
server.servlet.context-path=/
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=false
一个普通的controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello Sentinel !" ;
}
}
3、测试
启动工程后,访问: http://localhost/hello ,不论怎样刷新都是能正常返回结果的。 查看sentinel控制面板,拒绝QPS始终都是0。
开启流量控制: 设置QPS值为1 ,
再访问: http://localhost/hello , 快速访问时会出现下图提示
sentinel限流起到了作用。