springCloud(Gateway网关)

£神魔★判官ぃ 2024-03-30 16:21 255阅读 0赞

Gateway是springCloud中的重要组件。官网上的定义概述是:

This project provides a library for building an API Gateway on top of Spring WebFlux. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

翻译成中文:

这个项目提供了一个在Spring WebFlux之上构建API网关的库。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到api,并为它们提供交叉关注点,例如:安全性、监视/度量和弹性。

从定义上来看;网关主要解决服务API的路由,而且可以在网关这一层做一些安全校验,请求监控,统计的动作。

关键特性和作用:

基于Spring Framework 5、Project Reactor和Spring Boot 2.0构建

能够匹配任何请求属性的路由。

谓词和过滤器是特定于路由的。

断路器集成。

Spring Cloud DiscoveryClient集成

易于编写谓词和过滤器

请求速率限制

路径重写

源码示例:

  1. @SpringBootApplication
  2. public class DemogatewayApplication {
  3. @Bean
  4. public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
  5. return builder.routes()
  6. .route("path_route", r -> r.path("/get")
  7. .uri("http://httpbin.org"))
  8. .route("host_route", r -> r.host("*.myhost.org")
  9. .uri("http://httpbin.org"))
  10. .route("rewrite_route", r -> r.host("*.rewrite.org")
  11. .filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
  12. .uri("http://httpbin.org"))
  13. .route("hystrix_route", r -> r.host("*.hystrix.org")
  14. .filters(f -> f.hystrix(c -> c.setName("slowcmd")))
  15. .uri("http://httpbin.org"))
  16. .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
  17. .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
  18. .uri("http://httpbin.org"))
  19. .route("limit_route", r -> r
  20. .host("*.limited.org").and().path("/anything/**")
  21. .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
  22. .uri("http://httpbin.org"))
  23. .build();
  24. }
  25. }

在springCloud中集成

spring-cloud-starter-gateway

鉴权,授权示例:

  1. import org.springframework.cloud.gateway.filter.GatewayFilter;
  2. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
  3. import org.springframework.core.Ordered;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.server.ServerWebExchange;
  7. import lombok.extern.slf4j.Slf4j;
  8. import reactor.core.publisher.Mono;
  9. @Slf4j
  10. @Component
  11. public class PermissionGatewayFilter implements GatewayFilter, Ordered {
  12. @Override
  13. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  14. log.info("执行了自定义的全局过滤器");
  15. //1.获取请求参数access-token
  16. String token = exchange.getRequest().getQueryParams().getFirst("access-token");
  17. //2.判断是否存在
  18. if(token == null) {
  19. //3.如果不存在 : 认证失败
  20. log.info("没有获取到token");
  21. exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
  22. return exchange.getResponse().setComplete(); //请求结束
  23. }
  24. //4.如果存在,继续执行
  25. return chain.filter(exchange); //继续向下执行
  26. }
  27. @Override
  28. public int getOrder() {
  29. return 0;
  30. }
  31. }

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关

    一、什么是网关 网关英文名称为Gateway,又称网间连接器、协议转换器,可以在不同的通信协议、数据格式或语言,甚至体系结构完全不同的两种系统之间,充当翻译器。 网关在

    相关 作用

    什么是网关 网关(Gateway)又称网间连接器、协议转换器。网关在传输层上以实现网络互连,是最复杂的网络互连设备,仅用于两个高层协议不同的网络互连。网关的结构也和路由器类似