个人随笔
目录
二十九、springCloudAlibaba-gateway整合sentinel(完)
2023-11-15 22:36:01

在前面几篇,我们学习了gateway的断言、过滤、自定义断言过滤以及跨域请求这些,现在我们来进行跟sentinel的整合。

准备

1、我们首先要搭建sentinel环境

七、springCloudAlibaba-sentinel的控制台搭建
这里只需要搭建控制台即可。

2、搭建gateway环境

二十四、springCloudAlibaba-gateway整合nacos

整合sentinel

1、引入依赖

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba.cloud</groupId>
  7. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  8. </dependency>

2、加上控制台配置

  1. server:
  2. port: 8088
  3. spring:
  4. application:
  5. name: api-gateway
  6. cloud:
  7. nacos:
  8. server-addr: 127.0.0.1:8848
  9. discovery:
  10. username: nacos
  11. password: nacos
  12. namespace: public
  13. #gateway配置
  14. gateway:
  15. #路由规则
  16. routes:
  17. - id: order_route # 路由的唯一标识
  18. uri: lb://order-service #需要转发的地址.lb本地负载均衡策略
  19. #断言规则 用于路由规则的匹配
  20. predicates:
  21. - Path=/order-service/**
  22. #- Query=green,hel.
  23. #- MyCustomize=test
  24. #- After=2023-11-13T21:05:39.811+08:00[Asia/Shanghai]
  25. #匹配请求http://localhost:8088/order-service/order/add
  26. #过滤器 用于过滤请求
  27. filters:
  28. - StripPrefix=1 #转发之前去掉第一层路径
  29. - AddRequestHeader=X-Request-red, blue
  30. #- AddRequestParameter=red, blue
  31. #- MyCustomize=from,suibibk.com
  32. #http://localhost:8010/order/add
  33. #- id: stock_route
  34. #globalcors:
  35. #cors-configurations:
  36. #'[/**]': #允许跨域访问的资源
  37. #allowedOrigins: "*" #跨域来源
  38. #allowedMethods:
  39. # - GET
  40. #- POST
  41. sentinel:
  42. transport:
  43. dashboard: localhost:8084

其实就是这个

  1. sentinel:
  2. transport:
  3. dashboard: localhost:8084

3、启动测试 105

启动后我们需要先请求下http://localhost:8088/order-service/order/add ,才会在控制太看到。

可以看到跟之前的sentinel控制台还是有点区别的,有对gateway进行适配

我们设置个限流规则。访问测试发现,成功限流了。

我们还可以根据API分组来对某些请求统一控制

我们也可以设置对参数进行限流,以及各种规则

更多控制规则可以自行尝试

自定义异常

这里我们进行自定义异常在网关加上如下代码即可

  1. package com.suibibk.springCloud.gateway;
  2. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
  3. import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
  4. import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
  5. import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
  6. import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
  7. import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
  8. import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.web.reactive.function.BodyInserters;
  13. import org.springframework.web.reactive.function.server.ServerResponse;
  14. import org.springframework.web.server.ServerWebExchange;
  15. import reactor.core.publisher.Mono;
  16. import javax.annotation.PostConstruct;
  17. @Configuration
  18. public class GatewayConfig {
  19. @PostConstruct
  20. public void init(){
  21. BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
  22. @Override
  23. public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
  24. Result r = null;
  25. if(throwable instanceof FlowException){
  26. r = Result.error(100,"接口被限流了");
  27. }else if (throwable instanceof DegradeException){
  28. r = Result.error(101,"服务降级了");
  29. }else if (throwable instanceof ParamFlowException){
  30. r = Result.error(102,"热点参数限流了");
  31. }else if (throwable instanceof SystemBlockException){
  32. r = Result.error(103,"系统规则");
  33. }else if (throwable instanceof AuthorityException){
  34. r = Result.error(104,"授权规则不通过");
  35. }
  36. if(r==null){
  37. r = Result.error(105,"当前请求人数过多,请稍后再试");
  38. }
  39. //自定义异常处理
  40. return ServerResponse.status(HttpStatus.OK)
  41. .contentType(MediaType.APPLICATION_JSON)
  42. .body(BodyInserters.fromValue(r));
  43. }
  44. };
  45. GatewayCallbackManager.setBlockHandler(blockRequestHandler);
  46. }
  47. }
  1. package com.suibibk.springCloud.gateway;
  2. public class Result<T> {
  3. private Integer code;
  4. private String msg;
  5. private T data;
  6. public Result(Integer code, String msg, T data) {
  7. this.code = code;
  8. this.msg = msg;
  9. this.data = data;
  10. }
  11. public Result(Integer code, String msg) {
  12. this.code = code;
  13. this.msg = msg;
  14. }
  15. public Integer getCode() {
  16. return code;
  17. }
  18. public void setCode(Integer code) {
  19. this.code = code;
  20. }
  21. public String getMsg() {
  22. return msg;
  23. }
  24. public void setMsg(String msg) {
  25. this.msg = msg;
  26. }
  27. public T getData() {
  28. return data;
  29. }
  30. public void setData(T data) {
  31. this.data = data;
  32. }
  33. public static Result error(Integer code,String msg){
  34. return new Result(code,msg);
  35. }
  36. }

然后就会按我们定义的返回了

gateway篇完结!
ok!

 47

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2