SpringCloud Alibaba Sentinel限流熔断降级--------------客户端配置限流
在前一篇文章首先启动了sentinel控制台,在本文中将介绍springboot客户端的相关配置,实现对springboot接口的流量、请求次数进行控制。首先先介绍一个非常重要的注解:
注解介绍
在具体配置之前介绍一下一个非常重要的注解:@SentinelResource
@SentinelResource
用于定义资源,并提供可选的异常处理和 fallback 配置项。 @SentinelResource
注解包含以下属性:
-
value
: 资源名称,必需项(不能为空) -
entryType
: 入口类型,可选项: EntryType.IN和EntryType.OUT(默认为EntryType.OUT
) -
blockHandler
/blockHandlerClass
:blockHandler
对应处理BlockException
的函数名称,可选项。若未配置,则将BlockException
直接抛出。blockHandler 函数访问范围需要是public
,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为BlockException
。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定blockHandlerClass
为对应的类的Class
对象,注意对应的函数必需为 static 函数,否则无法解析。 -
fallback
: fallback 函数名称,可选项,仅针对降级功能生效(DegradeException
)。fallback 函数的访问范围需要是public
,参数类型和返回类型都需要与原方法相匹配,并且需要和原方法在同一个类中。业务异常不会进入 fallback 逻辑。
说明:
-
value:定义资源名,该名称将会显示在控制台中,并且在定义流控以及熔断降级规则时,指定资源名称:
blockHandler是异常处理的方法,默认需要与原方法在同一个类中,如果需要在另外的类中定义,则需要设置blockHandlerClass,并且噶异常处理的方法应为静态方法。
客户端配置步骤
引入依赖:
如果要在您的项目中引入 Sentinel,使用 group ID 为 org.springframework.cloud
和 artifact ID 为 spring-cloud-starter-alibaba-sentinel
的 starter。
<!-- Sentinel -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
修改配置文件
#这里的 spring.cloud.sentinel.transport.port 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了1个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。
spring.cloud.sentinel.transport.port=8731
#设置控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
# 设置客户端端口
server.port=8090
# 设置客户端名称,该名称将作为该客户端在sentinel控制台的名称
spring.application.name=sentinel-client
新建Sentinel异常处理类
新建Java类:
package com.xjl.sentinel.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class SentinelExceptionHandler {
final static Logger logger = LoggerFactory.getLogger(SentinelExceptionHandler.class);
public static String blockExceptionHandle(String name, BlockException exception) {
exception.printStackTrace();
logger.info("sentinel 熔断处理 {}", "SentinelExceptionHandler");
return "Sentinel 熔断处理函数";
}
}
对需要限流的资源添加@SentinelResource注解
package com.xjl.sentinel.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xjl.sentinel.handler.SentinelExceptionHandler;
@RestController
public class SentinelTestController {
@SentinelResource(value = "hello", entryType = EntryType.OUT, blockHandlerClass = SentinelExceptionHandler.class, blockHandler = "blockExceptionHandle")
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
System.out.println(name);
return "hello " + name;
}
}
启动客户端
启动客户端后,请求几次客户端的接口,否则控制台并不会显示出客户端。访问控制台主页,可以看到有两个应用,其中一个是控制台自己,另一个使我们的客户端。
该客户端的http server端口号就是配置文件中配置的 spring.cloud.sentinel.transport.port=8731
如果控制台以及客户端都是在本地电脑上启动的,应注意控制台自身也会启动一个http server,并且默认端口号是8719,如果客户端也采用默认的8719,则客户端启动后会出现冲突,客户端会自动修改该端口号,可能改成8720或者其他未占用的端口。
添加限流规则
测试熔断效果
访问http://localhost:8090/hello/xjl,
快速刷新,可以看到界面中轮流出现如下返回:
同时控制台打印如下:
xjl
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:57.682 INFO 33840 --- [nio-8090-exec-7] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:57.837 INFO 33840 --- [nio-8090-exec-8] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:57.995 INFO 33840 --- [nio-8090-exec-9] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:58.158 INFO 33840 --- [io-8090-exec-10] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
2019-03-30 14:54:58.288 INFO 33840 --- [nio-8090-exec-1] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:58.476 INFO 33840 --- [nio-8090-exec-2] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
xjl
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:58.762 INFO 33840 --- [nio-8090-exec-4] c.x.s.handler.SentinelExceptionHandler : sentinel 熔断处理 SentinelExceptionHandler
客户端代码:
https://github.com/xujingle1995/Learn-SpringCloudAlibaba/tree/master/sentinel-client