第一步:创建项目,引入依赖
为了方便后期sentinel和springcloud以及springcloud alibaba其他组件整合开发讲解,我们这边规范好springboot,springcloud,springcloud alibaba的版本,选用稳定的组合版本;
具体查看:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
先创建父pom项目 sentinel-test
属性,依赖管理以及插件定义;
<properties>
<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-cloud.version>Hoxton.SR8</spring-cloud.version>
<springboot.version>2.3.2.RELEASE</springboot.version>
<springcloudalibaba.version>2.2.4.RELEASE</springcloudalibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${springcloudalibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
新建module项目 sentinel-helloworld
引入web和sentinel依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
</dependencies>
application.yml配置
server:
port: 80
servlet:
context-path: "/"
新建启动类:SentinelHelloWorldApplication
package com.java1234;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2021-05-04 15:18
*/
@SpringBootApplication
public class SentinelHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelHelloWorldApplication.class,args);
}
}
第二步:定义规则和使用限流规则
package com.java1234.controller;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2021-05-04 14:35
*/
@RestController
public class SentinelHelloWorld {
@RequestMapping("helloWorld")
public String helloWorld(){
try(Entry entry=SphU.entry("HelloWorld")){ // 使用限流规则 HelloWorld
return "Sentinel 大爷你好!"+System.currentTimeMillis();
}catch (Exception e){
e.printStackTrace();
return "系统繁忙,请稍后!"; // 降级处理
}
}
/**
* 定义限流规则
* PostConstruct 构造方法执行完后执行方法 定义和加载限流规则
*/
@PostConstruct
public void initFlowRules(){
List<FlowRule> rules=new ArrayList<>(); // 定义限流规则集合
FlowRule rule=new FlowRule(); // 定义限流规则
rule.setResource("HelloWorld"); // 定义限流资源
rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 定义限流规则类型
rule.setCount(2); // 定义QPS阈值 每秒最多通过的请求个数
rules.add(rule); // 添加规则到集合
FlowRuleManager.loadRules(rules); // 加载规则集合
}
}
第三步:测试
浏览器请求:http://localhost/helloWorld
正常请求:
当访问频率超过QPS阈值2,则sentinel降级返回异常信息;
测试通过;
关于锋哥,java大佬界里的渣渣辉,微信:java3459
关注锋哥公众号:【 java1234 】 每天推送干货!