GateWay服务网关
书接上文,继续跟着周阳老师学习SpringCloud的Gateway,特此整理如下
一、Gateway是什么
Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。
二、为什么用Gateway
Spring Cloud Gateway 可以看做是一个 Zuul 1.x 的升级版和代替品,比 Zuul 2 更早的使用 Netty 实现异步 IO,从而实现了一个简单、比 Zuul 1.x 更高效的、与 Spring Cloud 紧密配合的 API 网关。
Spring Cloud Gateway 里明确的区分了 Router 和 Filter,并且一个很大的特点是内置了非常多的开箱即用功能,并且都可以通过 SpringBoot 配置或者手工编码链式调用来使用。
比如内置了 10 种 Router,使得我们可以直接配置一下就可以随心所欲的根据 Header、或者 Path、或者 Host、或者 Query 来做路由。
比如区分了一般的 Filter 和全局 Filter,内置了 20 种 Filter 和 9 种全局 Filter,也都可以直接用。当然自定义 Filter 也非常方便。
三、Zuul和GateWay区别
Zuul:
使用的是阻塞式的 API,不支持长连接,比如 websockets。
底层是servlet,Zuul处理的是http请求
没有提供异步支持,流控等均由hystrix支持。
依赖包spring-cloud-starter-netflix-zuul。
Gateway:
Spring Boot和Spring Webflux提供的Netty底层环境,不能和传统的Servlet容器一起使用,也不能打包成一个WAR包。
依赖spring-boot-starter-webflux和/ spring-cloud-starter-gateway
提供了异步支持,提供了抽象负载均衡,提供了抽象流控,并默认实现了RedisRateLimiter。
3.1 相同点:
1)底层都是servlet
2)两者均是web网关,处理的是http请求
3.2 不同点:
1)内部实现:
gateway对比zuul多依赖了spring-webflux,在spring的支持下,功能更强大,内部实现了限流、负载均衡等,扩展性也更强,但同时也限制了仅适合于Spring Cloud套件
zuul则可以扩展至其他微服务框架中,其内部没有实现限流、负载均衡等。
2)是否支持异步
zuul仅支持同步
gateway支持异步。理论上gateway则更适合于提高系统吞吐量(但不一定能有更好的性能),最终性能还需要通过严密的压测来决定
3)框架设计的角度
gateway具有更好的扩展性,并且其已经发布了2.0.0的RELESE版本,稳定性也是非常好的
4)性能
WebFlux 模块的名称是 spring-webflux,名称中的 Flux 来源于 Reactor 中的类 Flux。Spring webflux 有一个全新的非堵塞的函数式 Reactive Web 框架,可以用来构建异步的、非堵塞的、事件驱动的服务,在伸缩性方面表现非常好。使用非阻塞API。 Websockets得到支持,并且由于它与Spring紧密集成,所以将会是一个更好的 开发 体验。
Zuul 1.x,是一个基于阻塞io的API Gateway。Zuul已经发布了Zuul 2.x,基于Netty,也是非阻塞的,支持长连接,但Spring Cloud暂时还没有整合计划。
3.3 Spring WebFlux是随Spring 5推出的响应式Web框架
1)spring-webflux支持两种开发模式:
(1)类似于Spring WebMVC的基于注解(@Controller、@RequestMapping)的开发模式;
(2)Java 8 lambda风格的函数式开发模式。
2)WebFlux是基于响应式流的,可以用来建立异步、非阻塞、事件驱动的服务。默认采用Reactor作为响应式流的实现库,也提供对RxJava的支持
四、搭建cloud-gateway-gateway9527
application.yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启注册中心路由功能,利用微服务名进行路由
# lower-case-service-id: true
routes:
- id: payment_routh
# uri: http://localhost:8001
uri: lb://cloud-payment-service #此处如果有问题,请注意依赖spring-cloud-starter-netflix-eureka-client依赖不能错
predicates: #断言,路径匹配的才会路由
- Path=/payment/get/**
- id: payment_routh2
# uri: http://localhost:8001
uri: lb://cloud-payment-service #lb即loadblance代表负载均衡
predicates:
- Path=/payment/lb/**
- Cookie=username,zhangsanfeng #在cmd窗口,执行curl http://localhost:9527/payment/lb --cookie "username=zhangsanfeng",则会轮询访问8001、8002
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka #,http://eureka7002.com:7002/eureka
#logging:
# level:
# root: debug
GateWayMain9527
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
//启动EurekaMain7001、PaymentMain8001、PaymentMain8002、GateWayMain9527
//添加网关前:访问http://localhost:8001/payment/get/1 、http://localhost:8001/payment/lb/
//添加网关后:访问http://localhost:9527/payment/get/1 、http://localhost:9527/payment/lb/均可得到正确结果,屏蔽了8001端口
SpringApplication.run(GateWayMain9527.class,args);
}
}
GateWayConfig
@Configuration
//采用编码方式代替yml配置文件,实现gateway功能
public class GateWayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route",r->r.path("/guonei").uri("http://news.baidu.com/guonei")).build();
return routes.build();
}
}
MyLogGateWayFilter
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter,Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("*****************come in MyLogGateWayFilter:"+new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if (uname == null) {
log.info("**********用户名为空,非法用户,┭┮﹏┭┮");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 0;
}
}
五、测试
启动EurekaMain7001、PaymentMain8001、PaymentMain8002、GateWayMain9527
5.1 添加网关前
访问http://localhost:8001/payment/get/1 http://localhost:8001/payment/lb/
5.2 添加网关后
访问http://localhost:9527/payment/get/1
http://localhost:9527/payment/lb/均可得到正确结果,屏蔽了8001端口
5.3 自定义过滤
http://localhost:9527/payment/get/1
http://localhost:9527/payment/get/1?uname=zhangsanfeng
在cmd窗口,执行curl http://localhost:9527/payment/lb --cookie “username=zhangsanfeng”,则会轮询访问8001、8002