Sentinel整合网关服务核心代码
2021-12-03 本文已影响0人
迦叶_金色的人生_荣耀而又辉煌
上一篇 <<<Sentinel整合nacos实现策略持久化
下一篇 >>>Sentinel环境搭建
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。
1.引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!--sentinel整合到gateway中-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
2.配置相关
server:
port: 8089
spring:
application:
name: sentinel-gateway
cloud:
nacos:
discovery:
server-addr: 10.211.55.16:8848
###增加sentinel控制台的链接
sentinel:
transport:
dashboard: localhost:8718
eager: true
gateway:
routes:
#这里的ID必须和保护里的名字一致
- id: jarye
uri: http://www.jarye.cn/
predicates:
- Path=/baidu/**
3.bean配置
@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;
}
/**
* 异常类的处理
* @return
*/
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public JsonSentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new JsonSentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
}
4.事件处理类
public class JsonSentinelGatewayBlockExceptionHandler implements WebExceptionHandler {
public JsonSentinelGatewayBlockExceptionHandler(List<ViewResolver> viewResolvers, ServerCodecConfigurer serverCodecConfigurer) {
}
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
ServerHttpResponse serverHttpResponse = exchange.getResponse();
serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
byte[] datas = "{\"code\":403,\"msg\":\"API接口被限流\"}".getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = serverHttpResponse.bufferFactory().wrap(datas);
return serverHttpResponse.writeWith(Mono.just(buffer));
}
}
5.启动加载
@Slf4j
@Component
public class SentinelApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
initGatewayRules();
}
/**
* 配置限流规则,名字必须和路由规则里的ID一致,也可以整合nacos,从nacos里读取
*/
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("mayikt")
// 限流阈值
.setCount(1)
// 统计时间窗口,单位是秒,默认是 1 秒
.setIntervalSec(1)
);
GatewayRuleManager.loadRules(rules);
}
}
推荐阅读:
<<<Hystrix功能介绍
<<<Sentinel功能介绍
<<<Sentinel与Hytrix区别
<<<Sentinel的熔断降级策略
<<<Sentinel的热点词限流
<<<Sentinel的限流方式实例
<<<Sentinel策略的持久化方式
<<<Sentinel整合nacos实现策略持久化
<<<Sentinel环境搭建