前言
- 仅做集成参考
- 项目为maven项目
- 版本如下:
组件 | 版本 |
---|---|
springcloud | Hoxton.SR8 |
springboot | 2.3.2.RELEASE |
springcloud-alibaba | 2.2.4.RELEASE |
alibaba-sentinel-dashborad | 1.8.0 |
jdk | 1.8+ |
整合
step1: sentinel-client
配置如下
父pom,无则加入入pom.xml中 去除
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-boot.version>2.3.2.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.4.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
pom.xml
<dependencies>
<!-- spring cloud alibaba sentinel 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- spring boot web 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yaml
server:
port: 8080
spring:
application:
name: sentinel # 必须命名
cloud:
sentinel:
transport:
dashboard: localhost:9001 # sentinel服务端地址
HelloController.java
@RestController
public class HelloController {
@GetMapping("/hello")
@SentinelResource(value = "helloworld") //指定使用流控资源名称
public String hello(){
return "hello";
}
}
启动spring http://localhost:8080/hello
step2: sentinel- dashboard
docker部署
docker run --name sentinel-standalone -p 9001:8858 bladex/sentinel-dashboard
http://localhost:9001 账号 sentinel 密码 sentinel
验证
无配置情况:
配置限流:
配置限流后疯狂刷新 http://localhost:8080/hello