• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

gateway的基本使用

武飞扬头像
一个风轻云淡
帮助1

gateway三大核心概念

Route(路由)

路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由

Predicate(断言)

参考的是Java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由

Filter(过滤)

指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。

 学新通

 web请求,通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。
predicate就是我们的匹配条件;
而filter,就可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了

Gateway工作流程

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。
 
Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。

 
Filter在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,
在“post”类型的过滤器中可以做响应内容、响应头的修改,日志的输出,流量监控等有着非常重要的作用。

学新通

 学新通

核心逻辑 :路由转发 执行过滤器链

 代码讲解1

需求:

现在有一个请求http://localhost:8001/payment/get/31,我们目前不想暴露8001端口,希望在8001外面套一层9527

pom文件依赖:

(版本由父工程控制)

  1.  
    <!--gateway-->
  2.  
    <dependency>
  3.  
    <groupId>org.springframework.cloud</groupId>
  4.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  5.  
    </dependency>
  6.  
    <!--eureka-client-->
  7.  
    <dependency>
  8.  
    <groupId>org.springframework.cloud</groupId>
  9.  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  10.  
    </dependency>
  11.  
    <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
  12.  
    <dependency>
  13.  
    <groupId>com.atguigu.springcloud</groupId>
  14.  
    <artifactId>cloud-api-commons</artifactId>
  15.  
    <version>${project.version}</version>
  16.  
    </dependency>
  17.  
    <!--一般基础配置类-->
  18.  
    <dependency>
  19.  
    <groupId>org.springframework.boot</groupId>
  20.  
    <artifactId>spring-boot-devtools</artifactId>
  21.  
    <scope>runtime</scope>
  22.  
    <optional>true</optional>
  23.  
    </dependency>
  24.  
    <dependency>
  25.  
    <groupId>org.projectlombok</groupId>
  26.  
    <artifactId>lombok</artifactId>
  27.  
    <optional>true</optional>
  28.  
    </dependency>
  29.  
    <dependency>
  30.  
    <groupId>org.springframework.boot</groupId>
  31.  
    <artifactId>spring-boot-starter-test</artifactId>
  32.  
    <scope>test</scope>
  33.  
    </dependency>
  34.  
    </dependencies>
  35.  
     
  36.  
     
学新通

 yml文件设置:

  1.  
    server:
  2.  
    port: 9527
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: cloud-gateway
  7.  
    cloud:
  8.  
    gateway:
  9.  
    routes:
  10.  
    - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
  11.  
    uri: http://localhost:8001 #匹配后提供服务的路由地址
  12.  
    predicates:
  13.  
    - Path=/payment/get/** # 断言,路径相匹配的进行路由
  14.  
     
  15.  
    - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
  16.  
    uri: http://localhost:8001 #匹配后提供服务的路由地址
  17.  
    predicates:
  18.  
    - Path=/payment/lb/** # 断言,路径相匹配的进行路由
  19.  
     
  20.  
    eureka:
  21.  
    instance:
  22.  
    hostname: cloud-gateway-service
  23.  
    client: #服务提供者provider注册进eureka服务列表内
  24.  
    service-url:
  25.  
    register-with-eureka: true
  26.  
    fetch-registry: true
  27.  
    defaultZone: http://eureka7001.com:7001/eureka
学新通

 主启动类:

  1.  
    @SpringBootApplication
  2.  
    @EnableEurekaClient
  3.  
    public class GateWayMain9527
  4.  
    {
  5.  
    public static void main(String[] args)
  6.  
    {
  7.  
    SpringApplication.run(GateWayMain9527.class,args);
  8.  
    }
  9.  
    }

代码中注入RouteLocator的Bean 

上面的是通过配置文件去设置的网关,而除了这种方法以外还有另一种方法去设置通过代码中注入RouteLocator的Bean 

代码讲解2

需求:

通过9527网关访问到外网的百度新闻网址,当访问地址 http://localhost:9527/guonei时会自动转发到地址:http://news.百度.com/guonei

设置代码:

  1.  
    @Configuration
  2.  
    public class GateWayConfig
  3.  
    {
  4.  
    /**
  5.  
    * 配置了一个id为route-name的路由规则,
  6.  
    * 当访问地址 http://localhost:9527/guonei时会自动转发到地址:http://news.百度.com/guonei
  7.  
    * @param builder
  8.  
    * @return
  9.  
    */
  10.  
    @Bean
  11.  
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder)
  12.  
    {
  13.  
    RouteLocatorBuilder.Builder routes = builder.routes();
  14.  
     
  15.  
    routes.route("path_route_atguigu", r -> r.path("/guonei").uri("http://news.百度.com/guonei")).build();
  16.  
     
  17.  
    return routes.build();
  18.  
     
  19.  
    }
  20.  
     
  21.  
    }
  22.  
     
学新通

通过微服务名实现动态路由 

默认情况下Gateway会根据注册中心注册的服务列表,
以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能

 代码讲解3

需求:

 从服务注册中心上面去调用服务,即假设现在有俩个服务8001 8002(已经注册进服务中心             7001, 服务名称:cloud-payment-service ),需要通过http://localhost:9527/payment/lb去     调用 cloud-payment-service上面的服务,即8001 8002上的服务


 pom依赖引入:

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.cloud</groupId>
  3.  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4.  
    </dependency>

yml文件 :

  1.  
    server:
  2.  
    port: 9527
  3.  
     
  4.  
    spring:
  5.  
    application:
  6.  
    name: cloud-gateway
  7.  
    cloud:
  8.  
    gateway:
  9.  
    discovery:
  10.  
    locator:
  11.  
    enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
  12.  
    routes:
  13.  
    - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
  14.  
    # uri: http://localhost:8001 #匹配后提供服务的路由地址
  15.  
    uri: lb://cloud-payment-service #匹配后提供服务的路由地址
  16.  
    predicates:
  17.  
    - Path=/payment/get/** # 断言,路径相匹配的进行路由
  18.  
     
  19.  
    - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
  20.  
    # uri: http://localhost:8001 #匹配后提供服务的路由地址
  21.  
    uri: lb://cloud-payment-service #匹配后提供服务的路由地址
  22.  
    predicates:
  23.  
    - Path=/payment/lb/** # 断言,路径相匹配的进行路由
  24.  
     
  25.  
    eureka:
  26.  
    instance:
  27.  
    hostname: cloud-gateway-service
  28.  
    client: #服务提供者provider注册进eureka服务列表内
  29.  
    service-url:
  30.  
    register-with-eureka: true
  31.  
    fetch-registry: true
  32.  
    defaultZone: http://eureka7001.com:7001/eureka
学新通

需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。

lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhicbc
系列文章
更多 icon
同类精品
更多 icon
继续加载