上一章中我们通过Dashboard来为Sentinel客户端设置各种各样的规则,但是这些规则默认是存放在内存中,极不稳定,无法用于生成环境,所以需要将其持久化。
DataSource
扩展常见的实现方式有:
Sentinel 目前支持以下数据源扩展:
生产环境中一般常用的就是 推模式
。这里我们使用Nacos存储规则。推送模式的正确做法应该是 配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 数据源 → Sentinel 。
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring: cloud: sentinel: datasource: # 名称随意 javatrip: nacos: server-addr: 127.0.0.1:8848 dataId: ${spring.application.name}-rules groupId: SENTINEL_GROUP # 规则类型,取值见: # org.springframework.cloud.alibaba.sentinel.datasource.RuleType rule-type: flow
@RestController class test{ @RequestMapping("/test") public String test(){ return "Java旅途"; } }
0
代表根据并发数量来限流, 1
代表根据QPS来进行流量控制
要想实现在sentinel-dashboard中修改规则并同步到nacos,我们就需要修改sentinel服务。首先我们去 官网 下载Sentinel。
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <scope>test</scope> </dependency>
将<scope>test</scope>注释掉,因为这个是作用与test目录下的。
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <!--<scope>test</scope>--> </dependency>
sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
目录,将整个目录拷贝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/
。 com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
,将默认动态规则修改为nacos动态规则。 @Autowired @Qualifier("flowRuleDefaultProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired @Qualifier("flowRuleDefaultPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
修改为:
@Autowired @Qualifier("flowRuleNacosProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired @Qualifier("flowRuleNacosPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
将以下内容注释去掉
<!--<li ui-sref-active="active" ng-if="entry.appType==0">--> <!--<a ui-sref="dashboard.flow({app: entry.app})">--> <!--<i class="glyphicon glyphicon-filter"></i> 流控规则 V1</a>--> <!--</li>-->
注意:以上只是演示了流控规则的持久化,sentinel还支持其他规则,如果想实现哪种规则都可以采用相同的方式实现!
限流:就是请求多了,对请求进行定制的快速响应处理,应用在服务提供者本身。
从 1.6.0 版本开始,Sentinel 提供了 Spring Cloud Gateway 的适配模块,可以提供两种资源维度的限流:
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId> <version>x.y.z</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> </dependency>
SentinelGatewayFilter
实例以及 SentinelGatewayBlockExceptionHandler
实例。 @Configuration public class GatewayConfiguration { private final List<ViewResolver> viewResolvers; private final ServerCodecConfigurer serverCodecConfigurer; public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) { this.viewResolvers=viewResolversProvider.getIfAvailable(Collections::emptyList); this.serverCodecConfigurer = serverCodecConfigurer; } @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() { // Register the block exception handler for Spring Cloud Gateway. return new MySentinelGatewayBlockExceptionHandler(viewResolvers,serverCodecConfigurer); } @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public GlobalFilter sentinelGatewayFilter() { return new SentinelGatewayFilter(); } }
public class MySentinelGatewayBlockExceptionHandler extends SentinelGatewayBlockExceptionHandler { private List<ViewResolver> viewResolvers; private List<HttpMessageWriter<?>> messageWriters; public MySentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer) { super(viewResolvers,serverCodecConfigurer); this.viewResolvers = viewResolvers; this.messageWriters = serverCodecConfigurer.getWriters(); } @Override public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) { if(serverWebExchange.getResponse().isCommitted()){ return Mono.error(throwable); } if(!BlockException.isBlockException(throwable)){ return Mono.error(throwable); } return handleBlockedRequest(serverWebExchange, throwable).flatMap(response -> writeResponse(response, serverWebExchange)); } private Mono<ServerResponse> handleBlockedRequest(ServerWebExchange exchange, Throwable throwable) { return GatewayCallbackManager.getBlockHandler().handleRequest(exchange, throwable); } private final Supplier<ServerResponse.Context> contextSupplier = () -> new ServerResponse.Context() { @Override public List<HttpMessageWriter<?>> messageWriters() { return MySentinelGatewayBlockExceptionHandler.this.messageWriters; } @Override public List<ViewResolver> viewResolvers() { return MySentinelGatewayBlockExceptionHandler.this.viewResolvers; } }; private Mono<Void> writeResponse(ServerResponse response, ServerWebExchange exchange) { ServerHttpResponse resp = exchange.getResponse(); resp.getHeaders().add("Content-Type", "application/json;charset=UTF-8"); String json = "{/"code/": -1, /"data/": null, /"msg/": /"访问量过大,请稍后再试/"}"; DataBuffer buffer = resp.bufferFactory().wrap(json.getBytes(StandardCharsets.UTF_8)); return resp.writeWith(Mono.just(buffer)); } }
server: port: 7003 spring: application: name: alibaba-gateway cloud: nacos: discovery: server-addr: 127.0.0.1:8848 gateway: enabled: true discovery: locator: enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称进行路由 routes: - id: sentinel-nacos # 路由id,建议配合服务名 uri: lb://sentinel-nacos #匹配路由名 predicates: - Path=/sentinel/** # 断言,路径相匹配的进行路由 filters: - StripPrefix=1
-Dcsp.sentinel.app.type=1 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=alibaba-gateway
降级:就是服务崩溃了,所以 降级逻辑应该应用在消费者(调用者)那里 ,加在服务提供者本身是毫无意义的,因为服务已经断开了。
我们根据实际需求在sentinel-dashboard中配置降级规则,然后编写代码。
@RequestMapping("/test") public String test(){ return "Java旅途"; }
@FeignClient(name = "nacos-sentinel",fallback = RmoteTestFallback.class) interface RemoteTest{ @RequestMapping("/test") public String test(); }
为了简写fallback,我们更倾向于用fallbackFactory = RmoteTestFallbackFactory.class
@FeignClient(name = "nacos-sentinel",fallbackFactory = RmoteTestFallbackFactory.class) interface RemoteTest{ @RequestMapping("/test") public String test(); }
@Component class RmoteTestFallback implements RemoteTest{ @Override public String test() { return null; } }
@Component class RmoteTestFallbackFactory implements FallbackFactory<RemoteTest> { @Override public RemoteTest create(Throwable throwable) { return null; } }