Java之SpringCloud Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】

编程入门 行业动态 更新时间:2024-10-28 14:31:29

Java之SpringCloud <a href=https://www.elefans.com/category/jswz/34/1769600.html style=Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】"/>

Java之SpringCloud Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】

一、Gateway整合sentinel限流

网关作为内部系统外的一层屏障,对内起到-定的保护作用,限流便是其中之- - .网关层的限流可以简单地针对不同路由进行限流,也可针对业务的接口进行限流,或者根据接口的特征分组限流。

1、添加依赖

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2、添加配置

server:port: 8088
spring:application:name: api-gateway
# gateway的配置cloud:gateway:routes:- id: order_route #路由的唯一标识,路由到orderuri: lb://order-service # 需要转发的地址  lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略 order-service服务名#断言规则 用于路由规则的匹配predicates:- Path=/order/**# http://localhost:8088/order-serve/order/add  路由到↓# http://localhost:8020/order-serve/order/add#- After=2020-10-19T09:07:00.660+08:00[Asia/Shanghai]#- Header=X-Request-Id, \d+#- Method=GET#- Query=name,xushu|zhuge#- CheckAuth = xushu#filters:#- AddRequestHeader=X-Request-color,red#- AddRequestParameter=color,blue#- PrefixPath=/mall-order #添加前缀对应微服务需要配置context-path#- StripPrefix=1 # 转发之前,去掉第一次的路径# http://localhost:8020/order/add#- RedirectTo=302, #- SetStatus= 404#- CheckAuth=xushu#- id: stock_route# 配置Nacos# 跨域的配置#     globalcors:#       cors-configurations:#         '[/**]': # 允许跨域访问的资源#           allowedOrigins: "*" #跨域允许的来源 例如:www.smsm#           allowedMethods:#             - GET#             - POST#             - PUT#配置Nacosnacos:discovery:server-addr: 127.0.0.1:8848username: nacospassword: nacos# 配置sentinelsentinel:transport:dashboard: 127.0.0.1:8858

3、完善测试接口

下载:sentinel-dashboard-1.8.0.jar



运行jar包

java -Dserver.port=8858 -Dsentinel.dashboard.auth.username=xushu -Dsentinel.dashboard.auth.password=123456 -jar C:\Users\ZHENG\Desktop\sentinel-dashboard-1.8.0.jar

访问:http://localhost:8858/

账号:xushu
密码:123456


http://127.0.0.1:8088/order/add




访问:http://127.0.0.1:8088/order/add


不断点击连续访问

二、通过代码实现限流

1、编写配置类


@Configuration
public class GatewayConfig {@PostConstruct //设置初始化的时候public void init(){BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {@Overridepublic Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable t) {System.out.println(t);HashMap<String,String> map = new HashMap<String,String>();map.put("code",HttpStatus.TOO_MANY_REQUESTS.toString());map.put("message","限流了");//自定义的异常处理return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(map));}};GatewayCallbackManager.setBlockHandler(blockRequestHandler);}
}


访问:http://127.0.0.1:8088/order/get



访问:http://127.0.0.1:8088/order/get

连续不断访问

三、通过配置文件实现

      scg:fallback:mode: responseresponse-body: "{code:'',messageL:''}"

四、网管高可用

为了保证Gateway的高可用性,可以同时启动多个Gateway实例进行负载,在Gateway的.上游使用Nginx或者F5进行负载转发以达到高可用。

更多推荐

Java之SpringCloud Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】

本文发布于:2023-11-16 00:04:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Alibaba   Spring   Java   SpringCloud   sentinel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!