简介
sentinel (分布式系统的流量防卫兵)阿里开发的一个监控流量的框架,用于熔断降级,流量监控,系统负载等场景,和Hystrix一样的角色,下面我是基于spring-boot 对其进行整合,sentinel使用1.7版本。
新手入门
pom文件配置
加入
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.0</version>
</dependency>
示例
创建一个Test测试用例
public class sentinelTest {
public static void main(String[] args){
initFlowerRule();
int i=0;
while(true){
try(Entry entry= SphU.entry("HelloWorld")){
System.out.println(LocalDateTime.now().toString()+":Hello world");
i++;
if(i%10==0){
throw new Exception("null 10");
}
}catch (BlockException ex) {
// 处理被流控的逻辑
// System.out.println("blocked!");
} catch (Exception e) {
Tracer.trace(e); //记录异常
}
}
}
public static void initFlowerRule(){
List<FlowRule> rules=new ArrayList<>();
FlowRule rule=new FlowRule();
rule.setResource("hello"); //资源配置
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(20); //设置流量 限制每秒 请求次数不超过20
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
1.一个try(SphU.entry(“hello”))-catch代表资源监控的作用域
2.如果被限流降级则会抛出BlockException异常
3.业务异常不会被自动记录,需要调用Tracer.trace(e)
部署sentinel控制台(单机)
jar文件地址:https://github.com/alibaba/Sentinel/releases
这里我选1.6.3版本
下载完成后,输入启动命令
java -Dserver.port=8060 -Dcsp.sentinel.dashboard.server=127.0.0.1:8060 -Dproject.name=sentinel-dashboard -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar
控制台端口和地址配置
-Dserver.port=8060 -Dcsp.sentinel.dashboard.server=127.0.0.1:8060
完成后打开网址 http://127.0.0.1:806
默认账号和密码为 sentinel
客户端配置控制台链接
pom.xml 文件加入
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.7.0</version>
</dependency>
配置控制台地址和端口,打开测试方法的 Debug/configurations,配置启动参数
-Dcsp.sentinel.dashboard.server=127.0.0.1:8060 :控制台端口和ip地址
启动后,我们可以在控制台看到监控动态
注解方式配置
除了通过SphU.entry 方式实现监控外,我们还可以通过注解方法的方式进行控制,因为注解的方法需要用到aop,所以我们需要把aop整合进来,pom.xml里加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
//sentinel 注解方式相关jar包
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.7.0</version>
</dependency>
使用前我们还需要创建 SentinelResourceAspect 到spring中,注册注解信息
启动类配置如下
@SpringBootApplication
public class DubboProductApplication {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect(); //创建注解
}
public static void main(String[] args){
SpringApplication.run(DubboProductApplication.class);
}
}
就此工作准备好开始使用注解完成方法监控
sentinelTest.java 修改如下
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DubboProductApplication.class)
public class sentinelTest {
@Autowired
private SentinelService sentinelService; //业务处理类
@Test
public void testBlock(){
int i=0;
while(true){
try{
i++;
sentinelService.hello(i);
} catch (Exception e) {
}
}
}
@Before
public void initFlowerRule(){
List<FlowRule> rules=new ArrayList<>();
FlowRule rule=new FlowRule();
rule.setResource("hello");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
业务类如下
@Service
public class SentinelService {
@SentinelResource(value = "hello",blockHandler = "helloFallback",fallback = "exceptionHandler")
public String hello(long i) throws Exception {
if(i%10==0){ //模拟其他异常
throw new Exception("null 10");
}
return String.format("hello at %d"+i);
}
public String helloFallback(long s,Throwable e){
return String.format("fb:error ");
}
public String exceptionHandler(long s,Throwable e){
System.out.println("project is block:"+s);
return String.format("fb:block ");
}
}
@SentinelResource
配置到这个注解的方法将会加入监控队列中,其中主要参数:
value:资源名称
blockHandler : 处理 BlockException 异常的方法,方法需要为public
fallback :处理所有异常的方法(除了excptionsToIgnore外),方法需要为public
当blockHandler和fallback都配置时,业务抛出BlockException异常只会被blockHandler处理
配置完毕,结果与上一个截图一致
备注:spring boot环境下我们还何以集成alibab cloud 里的 Sentinel进行配置,这个更简单一些