一、热点规则
热点参数流控是一种更为细粒度的流控规则,它允许将规则具体到参数上。
热点规则简单使用
1、新建Controller
package com.chenchen.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.chenchen.entity.User;
import com.chenchen.service.impl.OrderServiceImpl3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Sentinel服务容错测试 热点规则
*/
@RestController
@Slf4j
public class OrderController5 {
/**************************************************************************/
/**
* @return
*/
@RequestMapping("/order/message5")
@SentinelResource("message5")//此注解是必须的,否则热点规则不生效
public User message2(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "phone",required = false) String phone) {
User user = new User();
user.setUsername(name);
user.setTelephone(phone);
log.info("调用orderService3热点规则方法:{}", "message2");
return user;
}
}
2 、配置热点规则
浏览器访问接口,发现第一个参数name被限流了。
当然还有个高级作用,就是对某一个参数的具体值进行流控,比如刚才的
public User message2(@RequestParam(value = “name”,required = false) String name, @RequestParam(value = “phone”,required = false) String phone) ;
我们对第一个参数name流控,还可以指定name是chenchen在单位时间内流控,这个功能很强大,而且支持集合的。