Spring Cloud Alibaba-Spring Cloud Gateway-API 网关-使用路由网关统一访问接口

什么是 Spring Cloud Gateway

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,Spring Cloud Gateway 旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式。Spring Cloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

Spring Cloud Gateway 功能特征

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
  • 动态路由
  • Predicates 和 Filters 作用于特定路由
  • 集成 Hystrix 断路器
  • 集成 Spring Cloud DiscoveryClient
  • 易于编写的 Predicates 和 Filters
  • 限流
  • 路径重写

Spring Cloud Gateway 工程流程

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。

过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(pre)或之后(post)执行业务逻辑。

POM

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>cn.dandelioncloud</groupId>
  7. <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
  8. <version>1.0.0-SNAPSHOT</version>
  9. <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
  10. </parent>
  11. <artifactId>hello-spring-cloud-gateway</artifactId>
  12. <packaging>jar</packaging>
  13. <name>hello-spring-cloud-gateway</name>
  14. <url>http://www.dandelioncloud.cn</url>
  15. <inceptionYear>2018-Now</inceptionYear>
  16. <dependencies>
  17. <!-- Spring Boot Begin -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-actuator</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. <scope>test</scope>
  26. </dependency>
  27. <!-- Spring Boot End -->
  28. <!-- Spring Cloud Begin -->
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-openfeign</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-starter-gateway</artifactId>
  44. </dependency>
  45. <!-- Spring Cloud End -->
  46. <!-- Commons Begin -->
  47. <dependency>
  48. <groupId>javax.servlet</groupId>
  49. <artifactId>javax.servlet-api</artifactId>
  50. </dependency>
  51. <!-- Commons Begin -->
  52. </dependencies>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. <configuration>
  59. <mainClass>cn.dandelioncloud.hello.spring.cloud.gateway.GatewayApplication</mainClass>
  60. </configuration>
  61. </plugin>
  62. </plugins>
  63. </build>
  64. </project>

主要增加了 org.springframework.cloud:spring-cloud-starter-gateway 依赖

特别注意

  • Spring Cloud Gateway 不使用 Web 作为服务器,而是 使用 WebFlux 作为服务器,Gateway 项目已经依赖了 starter-webflux,所以这里 千万不要依赖 starter-web
  • 由于过滤器等功能依然需要 Servlet 支持,故这里还需要依赖 javax.servlet:javax.servlet-api

Application

  1. package cn.dandelioncloud.hello.spring.cloud.gateway;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication
  7. @EnableDiscoveryClient
  8. @EnableFeignClients
  9. public class GatewayApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(GatewayApplication.class, args);
  12. }
  13. }

application.yml

  1. spring:
  2. application:
  3. # 应用名称
  4. name: spring-gateway
  5. cloud:
  6. # 使用 Naoos 作为服务注册发现
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848
  10. # 使用 Sentinel 作为熔断器
  11. sentinel:
  12. transport:
  13. port: 8721
  14. dashboard: localhost:8080
  15. # 路由网关配置
  16. gateway:
  17. # 设置与服务注册发现组件结合,这样可以采用服务名的路由策略
  18. discovery:
  19. locator:
  20. enabled: true
  21. # 配置路由规则
  22. routes:
  23. # 采用自定义路由 ID(有固定用法,不同的 id 有不同的功能,详见:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
  24. - id: NACOS-CONSUMER
  25. # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名
  26. uri: lb://nacos-consumer
  27. # Predicate 翻译过来是“谓词”的意思,必须,主要作用是匹配用户的请求,有很多种用法
  28. predicates:
  29. # Method 方法谓词,这里是匹配 GET 和 POST 请求
  30. - Method=GET,POST
  31. - id: NACOS-CONSUMER-FEIGN
  32. uri: lb://nacos-consumer-feign
  33. predicates:
  34. - Method=GET,POST
  35. server:
  36. port: 9000
  37. # 目前无效
  38. feign:
  39. sentinel:
  40. enabled: true
  41. # 目前无效
  42. management:
  43. endpoints:
  44. web:
  45. exposure:
  46. include: "*"
  47. # 配置日志级别,方别调试
  48. logging:
  49. level:
  50. org.springframework.cloud.gateway: debug

注意:请仔细阅读注释

测试访问

依次运行 Nacos 服务、NacosProviderApplicationNacosConsumerApplicationNacosConsumerFeignApplicationGatewayApplication

打开浏览器访问:http://localhost:9000/nacos-consumer/echo/app/name 浏览器显示

  1. Hello Nacos Discovery nacos-consumer i am from port 8082

打开浏览器访问:http://localhost:9000/nacos-consumer-feign/echo/hi 浏览器显示

  1. Hello Nacos Discovery Hi Feign i am from port 8082

注意:请求方式是 http://路由网关IP:路由网关Port/服务名/**

至此说明 Spring Cloud Gateway 的路由功能配置成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值