SpringCloud microservice project combat-zuul gateway detailed and build

The previous article introduced the service invocation. This article follows the previous topic and talks about the use of zuul in Spring Cloud and the establishment of a gateway.

First, we need to know what is a gateway ?
The gateway is a unified front-end entrance in a system. It is an intermediate layer between the client and the server. It is mainly used to handle non-business functions and provide functions such as routing requests, authentication, monitoring, caching, and current limiting. The request initiated by the client first passes through the gateway for non-business function processing, and then locates to a specific service node according to the routing of the path.

At the same time, the system uses the function of service routing, which can only expose the call address configured in the gateway when providing services to the outside world, and the caller does not need to know the specific micro service host of the back end. Can effectively protect the real service address information. Zuul is a microservice gateway, first of all a microservice. It will also register and discover services in the Eureka registration center. It is also a gateway, and requests should be routed through Zuul. Zuul gateway is not necessary. It is recommended. Introduction of SpringCloud zuul

1. Why use microservice gateway?

Different microservices generally have different network addresses, and the client may need to call multiple service interfaces to complete a business requirement. If the client directly communicates with each microservice, there will be the following problems:

    1) The client will request different microservices many times, increasing the complexity of the client;

    2) There are cross-domain requests and the processing is relatively complex;

    3) Certification is complex, and each service requires independent certification;

    4) Difficult to reconstruct, multiple services may be merged into one or split into multiple;

2. The advantages and disadvantages of using a gateway?

advantage:

  1. All external requests interact through the microservice gateway first, without calling specific microservice interfaces, simplifying development;

  2. Service gateway = routing forwarding + filter

  • Routing and forwarding: receiving requests and forwarding them to the back-end microservices;

  • Filter: A series of cross-cutting functions can be completed in the service gateway (authority verification)

Disadvantages: increase the gateway, an additional layer of forwarding, service performance will decline;

3. What is the role of using zuul? Zuul can implement the following functions by loading a dynamic filtering mechanism:

  • Verification and security: Identify verification requirements for various resources and reject those requests that do not match the requirements.
  • Review and monitoring: Track meaningful data and statistical results at the edge to bring us accurate production status conclusions.
  • Dynamic routing: Dynamically route requests to different back-end clusters as needed.
  • Stress test: Gradually increase the load traffic directed to the cluster to calculate the performance level.
  • Load distribution: Allocate the corresponding capacity for each type of load, and discard requests that exceed the limit.
  • Static response processing: Partial response is established directly at the edge to prevent it from flowing into the internal cluster.
  • Multi-regional flexibility: Request routing across AWS regions aims to diversify ELB usage and ensure that edge locations are as close as possible to users.

4. How does zuul work?

The core of zuul is a series of filters, its role can be compared to the Servlet framework's Filter, or AOP. In the process of zuul putting Request route into user processing logic, these filters participate in some filtering processes, such as Authentication, Load Shedding, etc. 

Zuul provides a framework to dynamically load, compile, and run filters.

There is no direct communication between Zuul's filters, and they pass data through a static class of RequestContext. There is a ThreadLocal variable in the RequestContext class to record the data that each Request needs to pass.

Zuul's filters are written by Groovy. These filter files are placed under specific directories on Zuul Server. Zuul will periodically poll these directories. The modified filters will be dynamically loaded into Zuul Server for filtering requests.

There are several standard filter types:

Most of Zuul's functions are implemented through filters. Four standard filter types are defined in Zuul. These filter types correspond to the typical life cycle of a request.

    1) PRE: This filter is called before the request is routed. We can use this filter to implement authentication, select the requested microservices in the cluster, and record debugging information.

    2) ROUTING business. This filter is used to build requests sent to microservices and use Apache HttpClient or Netfilx Ribbon to request microservices.

    3) POST: This filter is executed after routing to the microservice. This filter can be used to add standard HTTP headers to the response, collect statistics and metrics, and send the response from the microservice to the client.

    4) ERROR: The filter is executed when an error occurs in other stages.

Built-in special filter

zuul also provides a special type of filters, namely: StaticResponseFilter and SurgicalDebugFilter;

  • StaticResponseFilter: StaticResponseFilter allows generating a response from Zuul itself, rather than forwarding the request to the source.

  • SurgicalDebugFilter: SurgicalDebugFilter allows specific requests to be routed to separate debug clusters or hosts.

Custom filters

In addition to the default filter type, Zuul also allows us to create custom filter types.

5. The difference between Zuul and Nginx is the same: Zuul and Nginx can achieve load balancing, reverse proxy (hide the real IP address), filter requests, and achieve the effect of the gateway:

  • Nginx–c language development

  • Zuul--java language development

  • Zuul load balancing implementation: use ribbon + eureka to achieve local load balancing

  • Nginx load balancing implementation: using server to achieve load balancing

  • Nginx is more powerful than zuul because Nginx integrates some scripting languages ​​(Nginx + lua)

  • Nginx is suitable for server-side load balancing

  • Zuul is suitable for implementing gateways in microservices

Zuul service building

Let's first introduce zuul's dependency service in the parent project

Then create a Zuul microservice module and add pom dependencies as follows:

Add yml configuration:

Then add the startup class:

Start all services:

test:

Recommended reading: SpringCloud microservice project combat & nbsp;-& nbsp; Detailed explanation of microservice call (with interview questions)

SpringCloud microservice project combat, service registration and discovery (with interview questions)

Spring Cloud microservice project combat-Eureka service building

 

Scan the code to pay attention to the public number, send keywords to obtain relevant information:

  1. Send "Springboot" to receive the actual source code of the e-commerce project;

  2. Send "SpringCloud" to receive practical learning materials;

  3. Send "SpringCloud combat" to receive SpringCloud combat source code;

 

Guess you like

Origin www.cnblogs.com/lyn20141231/p/12761503.html