Spring Cloud(七)不使用starter的情况下使用Alibaba Sentinel
我们在使用Alibaba Sentinel时一般都是在pom文件中引入spring-cloud-starter-alibaba-sentinel,这个工程会带来非常多的jar包,这里面有些是和可视化监控有关的,有些是和webflux有关的,等等。如果我们并不需要可视化监控,不涉及到webflux,那么这个starter虽然给我们带来方便,但同时也使得无用的依赖jar包数量增加不少。如何让jar包尽可能少?本文尝试通过只添加两个和sentinel有关的包来实现一个流控规则。
1、配置pom.xml文件
<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>com.github.ralgond</groupId>
<artifactId>sentinel-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、编写主类
package com.github.ralgond.sentineldemo.e1;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
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;
@SpringBootApplication
@RestController
public class FlowController {
public AtomicLong pass = new AtomicLong(0);
public AtomicLong block = new AtomicLong(0);
@SentinelResource(value="get", blockHandler="blockHandler")
@RequestMapping(method=RequestMethod.GET, value="/get")
public String get() {
pass.incrementAndGet();
return "success";
}
public String blockHandler(BlockException ex) {
block.incrementAndGet();
return "failed";
}
@RequestMapping(method=RequestMethod.GET, value="/metric")
public String metric() {
return ""+pass.get()+"|"+block.get();
}
public static void main(String args[]) {
initFlowRules();
SpringApplication.run(FlowController.class, args);
}
private static void initFlowRules() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("get");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(1);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
3、配置和@SentinelResource有关的AOP类
@Configuration
public class SentinelE1Configuration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}
4、运行
启动主类,在浏览器输入http://localhost:8080/get,快速点击,你会发现时而出现success,大部分时间出现failed,再在浏览器输入http://localhost:8080/metric可以看到通过数和block数。