/**
提供方是在运行,但是不允许自己的业务逻辑,返回的是默认的降级数据(限流的数据)
try (Entry entry = SphU.entry("seckillSkus")) {
//业务逻辑
} catch(Exception e) {}
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动的时候: --service port 8888 设置启动端口
sentinel:
transport:
#配置sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
#暴露所有端点
management:
endpoints:
web:
exposure:
include: '*'


需要限制的业务类加上注解;
/**
* 获取到当前可以参加秒杀商品的信息
* @return
*/
@SentinelResource(value = "getCurrentSeckillSkusResource",blockHandler = "blockHandler")
@Override
public List<SeckillSkuRedisTo> getCurrentSeckillSkus()
自定义异常信息:
/**
* @Description: 自定义阻塞返回方法
* @Created: with IntelliJ IDEA.
* @author: 夏沫止水
* @createTime: 2020-07-13 11:30
**/
@Configuration
public class GulimallSeckillSentinelConfig {
public GulimallSeckillSentinelConfig() {
WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMessage());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().write(JSON.toJSONString(error));
}
});
}
}