当前位置:   article > 正文

分布式组件之网关Gateway_分布式gateway的实现

分布式gateway的实现

网关作为流量的入口,常用功能包括路由转发,权限校验,限流控制等。而springcloud Gateway作为SpringCloud官方推出的第二代网关框架,取代了Zuul网关。

Spring Cloud Gateway

主要有三大部分

工作流程如下

 

Example  application.yml

  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: after_route
  6. uri: https://example.org
  7. predicates:
  8. - After=2017-01-20T17:42:47.789-07:00[America/Denver]

上面的例子就是设置了一个网关断言规则

现在自己创建并测试一下Api网关

首先创建一个网关模块

 pom中添加common模块依赖

  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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.8.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example.gulimall</groupId>
  12. <artifactId>gulimall-gateway</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>gulimall-gateway</name>
  15. <description>gulimall-gateway</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>com.example.gulimall</groupId>
  23. <artifactId>gulimall-common</artifactId>
  24. <version>0.0.1-SNAPSHOT</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-gateway</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>
  36. <dependencyManagement>
  37. <dependencies>
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-dependencies</artifactId>
  41. <version>${spring-cloud.version}</version>
  42. <type>pom</type>
  43. <scope>import</scope>
  44. </dependency>
  45. </dependencies>
  46. </dependencyManagement>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-maven-plugin</artifactId>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </project>

在nacos中添加配置文件

 

然后在启动类上加上服务注册发现注解@EnableDiscoveryClient

application.properties
  1. spring.cloud.nacos.discovery.server-addr=192.168.159.135:8848
  2. spring.application.name=gulimall-gateway
  3. server.port=88

bootstrap.properties
  1. spring.application.name=gulimall-gateway
  2. spring.cloud.nacos.config.server-addr=192.168.159.135:8848
  3. spring.cloud.nacos.config.namespace=72bcbd7a-be8c-48cf-8584-2eeee5d14664
  4. spring.cloud.nacos.config.group=dev

最后再创建

application.yml
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: test_route
  6. uri: https://www.baidu.com
  7. predicates:
  8. - Query=url,baidu
  9. - id: qq_route
  10. uri: https://www.qq.com
  11. predicates:
  12. - Query=url,qq

 表示如果地址带有url头并且为baidu时,跳转至https://www.baidu.com,qq也一样

测试http://localhost:88/?url=qq

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/301937
推荐阅读
相关标签
  

闽ICP备14008679号