Viii. Service Gateway of Springcloud Zuul

Source: Internet
Author: User

I. Introduction of Zuul
    • Zuul is an API Gateway server for Netflix open source, essentially a Web servlet application.
    • Zuul is a load balancer based on the JVM Routing and service side of Netflix.
    • Zuul provides a framework for dynamic routing, monitoring, resiliency, security and other edge services on cloud platforms. Zuul corresponds to the front door of all requests from the Web site backend of the device and Netflix streaming app.
    • Zuul's example can be found in Netflix's simple webapp on GitHub, and can be used in accordance with Netflix's document description on the GitHub wiki.
    • Spring Cloud Zuul Routing is an integral part of the microservices architecture, providing dynamic routing, monitoring, resiliency, security, and other edge services.
    • Spring Cloud Zuu is a combination of default and ribbon for load balancing functions.
Second, the working principle of Zuul

The core of Zuul is a series of filtersthat can be used to mimic the filter of the servlet framework, or AOP.

Zuul the request route into the user process logic, these filters participate in some filtering processing, such as authentication,load shedding.

Com.netflix.zuul.http.ZuulServlet is the portal of the Zuulfilter chain execution, through the service method, extracting the request to RequestContext, and then by calling Zuulrunner, sequentially executing each type of filter, completing The entire filter life cycle, the frame composition is shown below.

In Zuul, the process for the entire request is this, first the request is processed to Zuulservlet, andthere is a Zuulrunner object in Zuulservlet that initializes the RequestContext: As some data that stores the entire request and is shared by all zuulfilter. Zuulrunner also has Filterprocessor,filterprocessor as the manager that executes all the Zuulfilter . Filterprocessor gets zuulfilter from Filterloader, and Zuulfilter is loaded by Filterfilemanager and supports groovy hot loading, using Polling the way hot loads . With these filters, Zuulservelet first executes the pre-type filter, executes the route-type filter, and finally executes the P-OST-type filter , if these filters are executed when there is an error, a filter of type error is executed. After these filters are executed, the results of the request are eventually returned to the client .

Zuul most of the functions are implemented through filters. Four standard filter types are defined in Zuul, which correspond to the typical life cycle of the request.

(1) PRE: This filter is called before the request is routed. We can use this filter to authenticate, select the requested microservices in the cluster, log debug information, and so on.

(2) ROUTING: This filter routes the request to the microservices. This filter is used to build requests sent to MicroServices and request microservices using the Apache httpclient or NETFILX ribbon.

(3) POST: This filter is executed after routing to the Micro service. This filter can be used to add standard HTTP headers for responses, collect statistics and metrics, send responses from microservices to clients, and so on.

(4) Error: Executes the filter when an error occurs at another stage.

  The principle of Zuul life cycle: https://www.cnblogs.com/lexiaofei/p/7080257.html

  Source Code Analysis:

76211680 (can be added to log processing)

http://xujin.org/sc/sc-zuul-s2/

Third, the Zuul introduction use

1. Add dependencies

Introduction spring-cloud-starter-zuul Package

< Dependency >    < groupId >org.springframework.cloud</groupId>    <  Artifactid>spring-cloud-starter-zuul</artifactid  ></dependency>

2. Configuration files

Application name, service port, etc.

spring.application.name=gateway-service-zuulserver.port=8888# configuration representation Here, Access/it/** Direct redirect to http://www.baidu.com/** zuul.routes.baidu.path=/it/**zuul.routes.baidu.url=http://www.baidu.com/

3. Startup class

Start class additions @EnableZuulProxy to support gateway routing.

@SpringCloudApplication @enablezuulproxy  Public class gatewayservicezuulapplication {    publicstaticvoid  main (string[] args) {        springapplication.run (gatewayservicezuulapplication. class , args);}    }

Note: here @SpringCloudApplication is annotated, not mentioned before, through the source we see, it integrates @SpringBootApplication , @EnableDiscoveryClient @EnableCircuitBreaker ,, the main purpose or simplified configuration.

  

Viii. Service Gateway of Springcloud Zuul

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.