Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2和 Project Reactor等技术。
Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等。
SpringCloud Gateway 是 Spring Cloud 的一个全新项目,基于 Spring 5.0+Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。
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 Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。
如下所示,这边便是gateway的工作区域
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由
参考的是Java8的java.util.function.Predicate
开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
mscloud
com.zsy.springcloud
1.0-SNAPSHOT
4.0.0
com.zsy.springcloud
cloud-gateway-gateway9527
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
com.zsy.springcloud
cloud-api-commins
${project.version}
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
如下图所示,我们将服务注册的服务注册中心去,并且配置到达我们所需服务的路由,这样我们就可以通过gateway来完成路由转发功能了
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** # 断言,路径相匹配的进行路由
- id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/** # 断言,路径相匹配的进行路由
eureka:
instance:
hostname: cloud-gateway-service
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://127.0.0.1:7001/eureka
package com.zsy.springclou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GateWayMain9527.class, args);
}
}
注意开启所有payment模块以及eureka服务注册中心,然后再开启gateway
如下图所示,在boot项目中增加这样一个配置类,将gateway地址转发到对应uri上即可。
package com.zsy.springclou.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 customRouterLocator(RouteLocatorBuilder builder) {
RouteLocatorBuilder.Builder router = builder.routes();
router.route("new_router", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
return router.build();
}
}
测试也很简单,输入http://localhost:9527/guonei即可跳转到百度的国内新闻
配置步骤也很简单,只需要将eureka上做负载的的服务名配置到uri上即可
开启所有payment模块以及eureka服务注册中心,然后再开启gateway
这时候通过地址http://localhost:9527/payment/lb
即可访问payment模块,又有是负载所以访问是随机的
如下所示,笔者想让这个请求在指定时间后且cookie为指定内容生效只需按照如下处理即可
具体也可以参照spring的官网示例
https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories
使用开启所有payment模块以及eureka服务注册中心,然后再开启gateway
上述配置完成后可用curl命令进行测试
curl http://localhost:9527/payment/lb --cookie "uname=zsy"
package com.zsy.springclou.config;
import lombok.extern.slf4j.Slf4j;
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;
@Component
@Slf4j
public class MyGateFielter implements GlobalFilter, Ordered {
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("执行了全局过滤器");
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == null) {
log.error("uname为空,请求将会被拦截");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
log.info("uname不为空,放行了,当前uname:" + uname);
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
测试
使用开启所有payment模块以及eureka服务注册中心,然后再开启gateway
测试命令
curl http://localhost:9527/payment/lb?uname=zsy