SpringCloud-Gateway二代网关

x33g5p2x  于2021-12-18 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(227)

简介:
SpringCloud Gateway的是SpringCloud的一个全新的项目,基于Spring 5.0 加SpringBoot2.0和Project Reactor等技术开发的网关,他只在为微服务架构提供一种简单有效的API路由管理方式
SpringCloud Gateway作为SpringCloud 生态系统中的网关,目标是替代了Zull,在SpringCloud 2.0以上的版本中,没有对新版本的Zull 2.0以上最新高性能版本进行集成,任然还是使用Zuul 1.X非Reaceor模式的老版本。而为了提升网关的的性能SpringCloud Gateway是基于WebFiux框架实现党的,而WebFiux框架底层则使用了性能高的Reactor模式通信框架Netty
SpringCloud Gateway的目标提供统一的路由方式基于Filter链的方式提供了网关基本的功能。安全,监控,指标 和限流

zuul和Gateway的区别
一方面是zuul已经进入维护阶段,而Gateway是SpringCloud团队研发解决 网关的产品,有很多功能,用起来比Zuul简单。
Gateway是基于异步非阻塞模型上开发的,性能方面优越,虽然Netrflix早就发布了Zuul二代但是一直在孵化期

Gateway的三大核心:
Route(路由):路由是构建网关的基本模块,他由ID目标URI一系列的断言和过滤器组
Predicate(断言):可与匹配HTTP请求中的所有的内容比如请求头或参数,如果请求与 断言相匹配路由
Filetr(过滤):指的是Spring框架GatewayFiletr的实例 ,使用过滤器,可以在请求被路由前或者之后对请求修改

工作流程

客户端 SpringCloud Gateway发出请求 ,然后再Gateway Handler Mapping中找到请求相匹配的 路由,将其转发Gateway Web Handler
Handler 在通过指定的 过滤器链来将请求 发送到我们实际的服务执行逻辑,然后返回,过滤器之间用虚线分开是因为过滤器可能会在发送代理请求(“pre”)或之后(“post”)执行业务逻辑
Filetr在“pre”类型的过滤器可以做参数校验,权限校验,流量监控,日志输出,协议转换,在post 类型的过滤器中可以做相应内容,相应头修改 ,日志输出,流量监控有着非常重要的作用

使用:
pom

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

创建项目路由
yml

server:
  port: 8085
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes:
        - id: cloud-hystrix-provider #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
          #lb://CLOUD-HYSTRIX-PROVIDER  等于localhost:8084的服务
          uri: lb://CLOUD-HYSTRIX-PROVIDER #匹配后提供服务的路由地址
          predicates:
            - Path=/select/** # 断言,路径相匹配的进行路由 - id: cloud-hystrix-providers #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: lb://CLOUD-HYSTRIX-PROVIDER #匹配后提供服务的路由地址 predicates: - Path=/getALL/** # 断言,路径相匹配的进行路由 #- After=2020-02-21T15:51:37.485+08:00[Asia/Shanghai] #- Cookie=username,zzyy #- Header=X-Request-Id, \d+ # 请求头要有X-Request-Id属性并且值为整数的正则表达式 eureka: instance: hostname: cloud-gateway-service client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://localhost:9001/eureka,http://127.0.0.1:9002/eureka

自定义网关(看需求)

@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter,Ordered
{

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
    {

        String uname = exchange.getRequest().getQueryParams().getFirst("name");

        if(name == null)
        {
         
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }

        return chain.filter(exchange);
    }

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

主启动类

package com.tnag.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class Gateway8085 {

    public static void main(String[] args) {
        SpringApplication.run(Gateway8085.class,args);
    }
}

配置好直接访问测试就可以(测试用的 yml测试)

相关文章