java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringCloud Gateway

详解SpringCloud新一代网关Gateway

作者:她眼里也曾有光

SpringCloud Gateway是Spring Cloud的一个全新项目,Spring 5.0+ Spring Boot 2.0和Project Reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式

一、概述简介

1.1、简介

SpringCloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

Spring Cloud Gateway的目标提供统- -的路由方式且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标, 和限流。

一句话:springCloud Geteway使用的Webflux中的reactor-netty响应式变成组建,底层使用了Netty通讯框架。 

1.2、作用

二、三大核心概念

2.1、Route 路由

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

2.2、Predicate 断言

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

2.3、Filter 过滤

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

2.4、总体

三、Getway工作流程

四、入门配置

4.1、pom

<!--新增gateway-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>

4.2、路由配置

yml:

server:

  port: 9527

spring:

  application:

    name: cloud-gateway

  cloud:

    gateway:

      routes:

        - id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名

          uri: http://localhost:8001   #匹配后提供服务的路由地址

          predicates:

            - Path=/payment/get/**   #断言,路径相匹配的进行路由

        - id: payment_routh2

          uri: http://localhost:8001

          predicates:

            - Path=/payment/lb/**   #断言,路径相匹配的进行路由

eureka:

  instance:

    hostname: cloud-gateway-service

  client:

    service-url:

      register-with-eureka: true

      fetch-registry: true

      defaultZone: http://eureka7001.com:7001/eureka

bean:

package com.rw.springcloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GateWayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes=routeLocatorBuilder.routes();
        routes.route("path_rout_rw1",
                r->r.path("/guonei")
                        .uri("http://news.baidu.com/guonei"))
                .build();
        return routes.build();
    }
}

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

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

yml:

server:

  port: 9527

spring:

  application:

    name: cloud-gateway

  cloud:

    gateway:

      discovery:

        locator:

          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由

      routes:

        - id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名

#          uri: http://localhost:8001   #匹配后提供服务的路由地址

          uri: lb://cloud-payment-service

          predicates:

            - Path=/payment/get/**   #断言,路径相匹配的进行路由

        - id: payment_routh2

          #uri: http://localhost:8001

          uri: lb://cloud-payment-service

          predicates:

            - Path=/payment/lb/**   #断言,路径相匹配的进行路由

eureka:

  instance:

    hostname: cloud-gateway-service

  client:

    service-url:

      register-with-eureka: true

      fetch-registry: true

      defaultZone: http://eureka7001.com:7001/eureka

六、Predicate的使用

七、Filter的使用

7.1、作用

7.2、Spring Cloud Gateway的Filter

生命周期,Only Two

种类,Only Two

7.3、自定义过滤器

两个接口介绍:GlobalFilter,Ordered

功能:

案例代码:

package com.rw.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

@Component
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("*********com in MyLogGateWayFilter"+new Date());
        String name=exchange.getRequest().getQueryParams().getFirst("uname");
        if(name==null){
            System.out.println("******用户名为null,非法用户");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

效果:请求地址中带由uname才让访问http://localhost:9527/payment/lb?uname=z3

以上就是详解SpringCloud新一代网关Gateway的详细内容,更多关于SpringCloud Gateway的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文