pom
< dependency>
< groupId> com.alibaba.csp</ groupId>
< artifactId> sentinel-annotation-aspectj</ artifactId>
< version> 1.4.1</ version>
</ dependency>
< dependency>
< groupId> com.alibaba.csp</ groupId>
< artifactId> sentinel-core</ artifactId>
< version> 1.8.2</ version>
</ dependency>
< dependency>
< groupId> io.jmnarloch</ groupId>
< artifactId> ribbon-discovery-filter-spring-cloud-starter</ artifactId>
< version> 2.1.0</ version>
</ dependency>
< dependency>
< groupId> io.jmnarloch</ groupId>
< artifactId> ribbon-discovery-filter-spring-cloud-starter</ artifactId>
< version> 2.1.0</ version>
</ dependency>
yml
zuul :
routes :
xxx :
path : /a- forward/**
url : forward: /myController
app
import com. alibaba. csp. sentinel. annotation. aspectj. SentinelResourceAspect ;
import com. alibaba. csp. sentinel. slots. block. RuleConstant ;
import com. alibaba. csp. sentinel. slots. block. flow. FlowRule ;
import com. alibaba. csp. sentinel. slots. block. flow. FlowRuleManager ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
import org. springframework. cloud. netflix. zuul. EnableZuulProxy ;
import org. springframework. context. annotation. Bean ;
import org. springframework. web. client. RestTemplate ;
import java. util. ArrayList ;
import java. util. List ;
@SpringBootApplication
@EnableZuulProxy
public class CloudZuulApplication {
public static void main ( String [ ] args) {
init ( ) ;
SpringApplication . run ( CloudZuulApplication . class , args) ;
}
@Bean
public RestTemplate restTemplate ( ) {
return new RestTemplate ( ) ;
}
private static void init ( ) {
List < FlowRule > rules = new ArrayList < > ( ) ;
FlowRule rule = new FlowRule ( ) ;
rule. setResource ( "filter_limit" ) ;
rule. setGrade ( RuleConstant . FLOW_GRADE_QPS) ;
rule. setCount ( 2 ) ;
rules. add ( rule) ;
FlowRule rule_res = new FlowRule ( ) ;
rule_res. setResource ( "resource_protect" ) ;
rule_res. setGrade ( RuleConstant . FLOW_GRADE_QPS) ;
rule_res. setCount ( 2 ) ;
rules. add ( rule_res) ;
FlowRuleManager . loadRules ( rules) ;
}
@Bean
public SentinelResourceAspect sentinelResourceAspect ( ) {
return new SentinelResourceAspect ( ) ;
}
}
controller
import com. example. cloudzuul. service. SentinelService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RestController ;
import org. springframework. web. client. RestTemplate ;
@RestController
public class controller {
@GetMapping ( "/myController" )
public String testController ( ) {
System . out. println ( "我的调用!" ) ;
return "my controller" ;
}
@Autowired
SentinelService sentinelService;
@GetMapping ( "/sentinel" )
public String sentinel ( ) {
return sentinelService. success ( ) ;
}
@Autowired
private RestTemplate restTemplate;
@GetMapping ( "/call" )
public String testCall ( ) {
return restTemplate. getForObject ( "http://service-sms/test/sms-test" , String . class ) ;
}
}
过滤器限流
import com. alibaba. csp. sentinel. Entry ;
import com. alibaba. csp. sentinel. SphU ;
import com. alibaba. csp. sentinel. slots. block. BlockException ;
import com. netflix. zuul. ZuulFilter ;
import com. netflix. zuul. exception. ZuulException ;
import org. springframework. cloud. netflix. zuul. filters. support. FilterConstants ;
import org. springframework. stereotype. Component ;
@Component
public class SentinelFilter extends ZuulFilter {
@Override
public String filterType ( ) {
return FilterConstants . PRE_TYPE;
}
@Override
public int filterOrder ( ) {
return 0 ;
}
@Override
public boolean shouldFilter ( ) {
return true ;
}
@Override
public Object run ( ) throws ZuulException {
Entry entry = null ;
try {
System . out. println ( this . getClass ( ) . getName ( ) ) ;
entry = SphU . entry ( "filter_limit" ) ;
} catch ( BlockException ex) {
System . out. println ( "==>>: BlockException" ) ;
} finally {
if ( entry != null ) {
entry. exit ( ) ;
}
}
return null ;
}
}
服务限流
blockHandler 参数和限流方法一样,最后要有一个 BlockExcepetion 参数;
import com. alibaba. csp. sentinel. annotation. SentinelResource ;
import com. alibaba. csp. sentinel. slots. block. BlockException ;
import org. springframework. stereotype. Service ;
@Service
public class SentinelService {
@SentinelResource ( value = "resource_protect" , blockHandler = "fail" )
public String success ( ) {
System . out. println ( "success" ) ;
return "ok" ;
}
public String fail ( BlockException block) {
System . out. println ( "fail" ) ;
return "fail" ;
}
}
使用 blockHandlerClass
;注意:fail 方法必须是 static 的,即使是一个类也必须 static