通过@SentinelResource来指定出现限流和降级时候的异常处理策略。
<?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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sentinelresource</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sentinelresource</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</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>
</project>
spring.application.name=sentinel-sentinelresource
server.port=8087
spring.cloud.sentinel.transport.dashboard=http://10.17.12.158:8084
package com.example.sentinelresource;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SentinelresourceApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelresourceApplication.class, args);
}
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}
package com.example.sentinelresource.service;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class SentinelService {
protected Logger logger = LoggerFactory.getLogger(getClass());
// 限流与阻塞处理
@SentinelResource(value = "doSomeThing", blockHandler = "exceptionHandler")
public void doSomeThing(String str) {
logger.info(str);
}
//实现限流的异常处理
public void exceptionHandler(String str, BlockException ex) {
logger.error("blockHandler:" + str, ex);
}
// 熔断与降级处理
@SentinelResource(value = "doSomeThing2", fallback = "fallbackHandler")
public void doSomeThing2(String str) {
logger.info(str);
throw new RuntimeException("发生异常");
}
public void fallbackHandler(String str) {
logger.error("fallbackHandler:" + str);
}
}
package com.example.sentinelresource.controller;
import com.example.sentinelresource.service.SentinelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class HelloController {
@Autowired
private SentinelService sentinelService;
@GetMapping("/hi")
public String hi() {
sentinelService.doSomeThing("hi," + new Date());
return "hi,somebody";
}
@GetMapping("/hello")
public String hello() {
sentinelService.doSomeThing2("hello " + new Date());
return "Hello,World!";
}
}
多次请求后
发送一次localhost:8087/hello
请求(控制台将输出异常信息),比如
curl localhost:8087/hello
在Sentinel-Dashboard上可以看到名为doSomeThing2的资源点,然后点击”降级“按钮,为该资源设置降级规则。 这里选择异常数为3,时间窗口为3秒
验证熔断降级:
每请求一次localhost:8087/hello
请求,控制台均将输出异常信息;
当访问次数超过3次后,将调用将直接出发熔断降级。