SpringCloudAlibaba的服务限流,降级,热点,授权
- 安装部署sentinel-dashboard-1.6.0.jar
https://github.com/alibaba/Sentinel/releases
https://github.com/alibaba/Sentinel/releases/download/1.6.0/sentinel-dashboard-1.6.0.jar
- 部署sentinel
找到jar包目录执行:java -jar -Dserver.port=8888 sentinel-dashboard-1.6.0.jar
说明成功了
开始编码,引入POM文件。
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</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>
1. 服务限流
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.didispace.alibaba.sentinel.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@Slf4j
@RestController
public class TestController {
@Autowired
private TestService testService;
/**
* 限流配置:
* @return
*/
@GetMapping("/current-limiting")
public String currentLimiting() {
testService.currentLimiting("hello " + new Date());
return "didispace.com";
}
}
@Slf4j
@Service
public class TestService {
// 限流与阻塞处理
@SentinelResource(value = "currentLimiting", blockHandler = "exceptionHandler")
public void currentLimiting(String str) {
System.out.println(str);
}
public void exceptionHandler(String str, BlockException ex) {
log.error("blockHandler:" + str, ex);
}
}
- 使用测试工具Jmeter
- 熔断和降级
/**
* 降级设置:
* @return
*/
@GetMapping("/fusing")
public String fusing() {
String result = testService.fusing("hello2 " + new Date());
return result;
}
// 熔断与降级处理
@SentinelResource(value = "fusing", fallback = "fallbackHandler")
public String fusing(String str) {
log.info(str);
throw new RuntimeException("发生异常");
}
public String fallbackHandler(String str) {
log.error("fallbackHandler:" + str);
return "异常了,兄弟";
}
- 热点数据处理
/**
* 热点设置:
* @return
*/
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1", required = false) String p1) {
return testService.hotspot(p1);
}
/**
* 对于热点数据的处理(能否等待或者重新尝试,等待2秒钟后,重新尝试。)
* @param p1
* @param exception
* @return
*/
public String deal_testHotKey(String p1, BlockException exception) {
return "你请求的是热点数据,请耐心等待哟。";
}
/**
* 测试热点数据。
* @param str
* @return
*/
public String hotspot(String str) {
log.info(str);
return "你成功调用到了我 : " + str;
}
线程数:20
疑惑点:如果有多个参数,应该z怎么处理?
个人建议:并发测试的时候,右键选择启动
代码下载地址:https://gitee.com/li_mingxing_admin/SpringCloudAlibaba