Microservice Gateway SpringCloud Gateway

Introduction to Microservice Gateway


Introduce

After we have implemented various microservices, how can our various microservices be provided for external application calls? ?
Of course, because it is a Rest API interface, there is no problem for external clients to directly call microservices. But for various reasons, this is not a very good choice. Let the client directly communicate with each microservice, there will be the following problems.

  • The client will request different microservices multiple times, increasing the complexity of the client
  • There are cross-domain requests, and the processing will become relatively complicated in certain scenarios
  • Implementing authentication is complicated, and each microservice requires independent authentication
  • Difficult to refactor, project iteration, may lead to microservice repartition, if the client directly communicates with the microservice, then refactoring will be difficult to implement
  • If some microservices use firewalls and browser-unfriendly protocols, direct access will be difficult

How should we solve the above problems? It can be solved through the service gateway. In a microservice system, microservice resources are generally not directly exposed to external client access, which has the advantage of hiding internal services to solve the above problems. The gateway has many important meanings, which are embodied in the following aspects:

  1. The gateway can do some identity authentication, authority management, prevent illegal requests for operation authority, etc., and play a certain role in protecting the service
  2. The gateway manages all microservices in a unified manner and exposes them to the outside world. The external system does not need to know the complexity of the microservice architecture calling each other, and it also avoids the leakage of some sensitive information of internal services.
  3. Easy to monitor, monitoring data can be collected at the microservice gateway and pushed to external systems for analysis
  4. The client only deals with the service gateway, reducing the number of interactions between the client and each microservice
  5. Multi-channel support, can provide different API gateways according to different clients (WEB, mobile, desktop,...)
  6. Gateways can be used for traffic monitoring. Under high concurrency, limit and downgrade the service
  7. The gateway separates the service from the internal, which is convenient for testing

The microservice gateway can realize various functions such as routing and load balancing. Functions similar to Nginx and reverse proxy. In the microservice architecture, back-end services are often not directly opened to the caller, but are routed to the corresponding service through an API gateway according to the URL of the request. When the API gateway is added, between the third-party caller and the service provider A wall is created to control permissions in the API gateway, and at the same time, the API gateway sends it to the back-end service in a load-balanced manner. Microservice Gateway Architecture:
insert image description here


1. Introduction to Spring Cloud Gateway

springCloud is a brand new project of spring Cloud, which is a gateway developed based on technologies such as Spring 5.0, spring Boot 2.0 and Project Reactor. It aims to provide a simple and effective unified API management method for microservice architecture

As a gateway in the Spring Cloud ecosystem, SpringCloud Gateway aims to replace Zuul. In versions above Spring Cloud 2.0, it does not integrate the latest high-performance version of the new version of Zuul 2.0, and still uses the old version of Reactor mode before Zuul 2.0. , and in order to improve the performance of the gateway, Spring Cloud Gateway is implemented based on the WebFlux framework, and the bottom layer of the WebFlux framework uses the high-performance Reactor mode communication framework Netty

The goal of Spring Cloud Gateway is not only to provide a unified routing method, but also to provide basic functions of the gateway based on the Filter chain, such as: security, monitoring/indicators, and current limiting


2. Features

SpringCloud officially introduces the features of SpringCloud Gateway as follows:

  • Based on Spring Framework 5, Project Reactor and Spring Boot 2.0
  • Integrate Spring Cloud Discovery Client
  • Predicates and Filters act on specific routes, easy to write Predicates and Filters
  • With some advanced functions of the gateway, dynamic routing, current limiting, path rewriting
  • Integrated fuse CircuitBreaker

From the above characteristics, it is not much different from Zuul. The main difference between Spring Cloud Gateway and Zuul lies in the underlying communication framework.

Briefly explain the three terms above:

  • Filter (filter)
    is conceptually similar to Zuul's filter, which can be used to intercept and modify requests, and perform secondary processing on downstream responses. The filter is an instance of the org.springframework.cloud.gateway.filter.GatewayFilter class

  • The basic component module of Route (routing)
    gateway configuration is similar to Zuul's routing configuration module. A Route module is defined by an ID, a target URI, a set of assertions and a set of filters. If the assertion is true, the route matches and the target URI is accessed.

  • Predicate (assertion)
    This is a Java8 Predicate, which can be used to match any content from the HTTP request, such as headers or parameters, and the input type of the assertion is a ServerWebExchange

3. How to configure routing

Routing is the basic building block of gateway configuration, similar to Zuul's routing configuration module. A Route module is defined by an ID, a target URI, a set of assertions and a set of filters. If the assertion is true, the route matches and the target URI is accessed.

1. Basic routing configuration method:
If the requested address is a single URI resource path, the configuration file is as follows:

spring:
   application:
     name: api-gateway
   cloud:
     gateway:
       routes:
          - id: service1
            uri: https://blog.csdn.net
            predicates:
              - Path=/csdn

The meaning of each field is as follows.
● id: Our custom route ID, keep it unique
● uri: Target service address
● Predicates: Routing conditions, Predicate accepts an input parameter and returns a boolean result. This interface contains various default methods to combine Predicates into other complex logic (such as: AND, OR, NOT).
The above configuration means that a URI proxy rule with an id of url-proxy-1 is configured. The routing rule is that when accessing the address http://localhost:8080/csdn/1.jsp, it will be routed to the upstream Address https://blog.csdn.net/1.jsp.

2. Code-based routing configuration

The forwarding function can also be implemented through code, we can add the method customRouteLocator() in the startup class GateWayApplication to customize the forwarding rules

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    
    
        return builder.routes()
                .route("path_route", r -> r.path("/csdn")
                        .uri("https://blog.csdn.net"))
                .build();
    }
}

3. The routing configuration method combined with the registration center

The schema protocol part of uri is a custom lb: type, which means subscribing to services from a microservice registry (such as Eureka) and routing services through load balancing. code show as below.

server:
  port: 9005
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: service1
          uri: https://blog.csdn.net
          predicates:
            - Path=/csdn
        - id: service2
#          uri: http://127.0.0.1:9001
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/**
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9004/eureka

4. Routing matching rules

The main function of Spring Cloud Gateway is to forward requests. The definition of forwarding rules mainly includes three parts:

  • Route (routing): Route is the basic unit of the gateway. It consists of ID, URI, a set of Predicates, and a set of Filters. It is matched and forwarded according to the Predicates.
  • Predicate (predicate, assertion): Judgment conditions for routing forwarding. Currently, SpringCloud Gateway supports various forms, such as: Path, Query, Method, Header, etc. The writing method must follow the form of key = value
  • Filter (filter): The filter is the filtering logic passed through when the request is forwarded, which can be used to modify the content of the request and response

5. Advanced features

  • Fuse downgrading
    In a distributed system, the gateway is the entrance of traffic, so a large number of requests will enter the gateway and initiate calls to other services. Other services will inevitably fail to call (timeouts, exceptions). When failures occur, requests cannot be accumulated in the On the gateway, it needs to fail quickly and return to the client. To achieve this requirement, it is necessary to perform fuse and downgrade operations on the gateway.
    Why does request failure on the gateway need to be returned quickly to the client? Because when a client request fails, the request will always be piled up on the gateway. Of course, there is only one such request, and the gateway must have no problem (if one request can cause the entire system to paralyze, then the system can be taken off the shelves) , but the accumulation on the gateway will cause huge pressure on the gateway and even the entire server to ensure the normal operation of the core business, and at the same time maintain the correct response of customers and most customers, so the request failure on the gateway needs to be returned quickly to the client.
    The CircuitBreaker filter wraps gateway routes in circuit breakers using the Spring Cloud CircuitBreaker API. Spring Cloud CircuitBreaker supports several circuit breaker libraries that can be used with Spring Cloud Gateway. For example, Spring Cloud supports Resilience4j out of the box.

Guess you like

Origin blog.csdn.net/CXgeng/article/details/123011412