Software Architecture-springcloud-zuul Microservice Gateway (below)

Any mature architecture has a life cycle, and so does zuul. Let's find out together.

(1) The life cycle of zuul requests

  • process
  1. The client HTTP sends a request.
  2. The spring framework often uses interceptors, filters, and zuul as well, customizing microservices.
  3. Access back-end microservices through pre-filters "pre fiters" and "routing fiters", and "post fiters" are returned to the client

(2) Use and detailed explanation of zuul filter

Filter (filter) is the core component of zuul. Most of the functions of zuul are implemented through filters. There are 4 standard filter types defined in zuul that correspond to the typical lifecycle of a request.

  • PRE

This filter is called before the request is routed. Such filters can be used to implement authentication, select requested microservices in the cluster, log debug information, and more.

  • ROUTING

This filter routes requests to microservices. This filter is used to structure requests to microservices and request microservices using Apache HttpCIient or Netfilx Ribbon.

  • POST

This filter is executed after routing to the microservice. Such filters can be used to add standard HTTP headers to responses, collect statistics and metrics, send responses from microservices to clients, and more.

  • ERROR

This filter is executed when errors occur in other stages.

  • source code

08-ms-gateway-zuul-filter As can be seen from the code, the custom zuul Filter needs to implement the following methods.

  • filterType

Returns the type of filter. There are several values, such as pre, route, post, and error, which correspond to the above filters. For details, please refer to the comments in com.netflix.zuul.ZuulFilter.filterType().

  • filter0rder

Returns an int value to specify the order in which the filters are executed, different filters are allowed to return the same number.

  • shouldFilter

Returns a boolean value to determine whether the filter is to be executed, true for execution, false for not execution.

  • run

过滤器的具体逻辑。本例中让它打印了请求的 HTTP方法以及请求的地址。

  • 代码示例

1.08-ms-provider-user

2.08-ms-gateway-zuul-filter 3.08-ms-eureka-server 4.08-ms-consumer-order-ribbon

直接源码运行就可以了,上几次文章都演示了,这次不演示了。直接main方法运行起来就可以了。

运行项目访问地址:http://localhost:8040/microservice-provider-user/getIpAndPort 可以看到zuul服务的后台正常打印了run方法里的日志。

  • 禁用zuul过滤器

Spring Cloud默认为Zuul编写并启用了一些过滤器,例如DebugFilter、

FormBodyWrapperFilter等,这些过滤器都存放在spring-cloud-netflix-core这个jar包里,一些场景下,想要禁用掉部分过滤器,该怎么办呢?

只需在application.yml里设置

zuul...disable=true

例如,要禁用上面我们写的过滤器,这样配置就行了:
zuul.PreRequestLogFilter.pre.disable=true
复制代码

  • zuul的容错与回退

老铁!如果zuul代理的后端微服务挂了会出现什么情况?zuul默认已经整合了hystrix,也就是zuul也是可以利用hystrix做降级容错处理的,但是zuul监控的粒度是微服务级别,而不是某个API。

源码:08-ms-gateway-zuul-fallback 编写zuul的降级回退类如下

启动项目 1.08-ms-gateway-zuul-fallback

2.08-ms-eureka-server 3.08-ms-consumer-order-ribbon 注意:这里不启动记住不启动 08-ms-provider-user

关闭zuul代理的用户微服务,再运行本项目,访问地址:http://localhost:8040/microservice-provider-user/getIpAndPort,将会返回如下内容

(三)zuul的高可用

分两种场景讨论zuul的高可用

  • zuul客户端也注册到了Eureka Server上

这种情况下,Zuul的高可用非常简单,只需将多个Zuul节点注册到Eureka Server上,就可实现Zuul的高可用。此时,Zuul的高可用与其他微服务的高可用没什么区别。

当zuul客户端也注册到Eureka Server上时,只需部署多个Zuul节点即可实现其高可用。zuul客户端会自动从Eureka Server中查询zuul Server的列表,并使用Ribbon负载均衡地请求Zuul集群。

  • zuul客户端未注册到Eureka Server上

现实中,这种场景往往更常见,例如,zuul客户端是一个手机APP——我们不可能让所有的手机终端都注册到Eureka Server上。这种情况下,我们可借助一个额外的负载均衡器来实现Zuul的高可用,例如Nginx、HAProxy、F5等。

zuul客户端将请求发送到负载均衡器,负载均衡器将请求转发到其代理的其中一个zuul节点。这样,就可以实现Zuul的高可用。

PS:zuul 作为网关这么重要的角色,高可用是非常有必要的。但是通常来说网关所面对的请求应该的是来于外部,所以虽然说网关可以注册到Eureka Server上,但是外部的客户端数量众多,是不可能向Eureka Server注册的。那么要实现高可用的,要么在网关前面再架一个前置代理(如Nginx)。

Guess you like

Origin juejin.im/post/7087757104789848071