This is my 17th day of the August Genwen Challenge

Introduction to the

Spring Cloud Gateway is the official Spring Cloud Gateway based on Spring 5.0, Spring Boot 2.0 and Project Reactor technologies.

Spring Cloud Gateway aims to provide a simple and effective unified API route management approach for microservices architectures. As a Gateway in the Spring Cloud ecosystem, Spring Cloud Gateway aims to replace ZUUL. It not only provides a unified routing method, but also provides basic Gateway functions based on Filter chain, such as reverse proxy, security, monitoring/burying point, and flow limiting.

Gateway has three core concepts

  • Route: This is the basic building block of network management. It is defined by an ID, a target URL, a set of assertions, and a set of filters. If the assertion is true, the route matches; otherwise, it does not match
  • Predicate: The input type is a ServerWebExchange. We can use it to match anything from the Http request.
  • Filter: The Gateway filter is divided into two types, namely, Gateway filter and Golabl filter. The filter will modify the request and response

Dynamic routing

Register the Gateway with ERuka

<dependency>
    <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

When modifying YML, note that the URI protocol is LB, which indicates that the load balancing function of the Gateway is enabled. Lb ://serviceName (microservice name) is the load balancing URI that SpringCloud Gateway automatically creates for us in the microservice.

spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: Route: -id: payment_routh #payment_routh # id: payment_routh # http://localhost:8001 ://cloud-provider-service :// cloudprovider-service :// cloudprovider-service :// cloudprovider-service :// cloudprovider-service :// cloudprovider-service -id: payment_routh2 #payment_routh # Route ID: payment_routh2 http://localhost:8001 ://cloud-provider-service :// cloudprovider-service :// cloudprovider-service :// cloudprovider-service :// cloudprovider-service :// cloudprovider-service - Path=/payment/lb/** # Specifies that the route will be routed if the Path matches the paymentCopy the code

The filter filter chain

It is divided into two filters, one is GatewayFilter (single routing filter). The other is GlobalFilter (intercepts all routes)

Rewrite a GatewayFilter:

1, implement GatewayFilterFactory or inherited AbstractGatewayFilterFactory < C > class 2, inheritance AbstractGatewayFilterFactory < C > class, Constructor: call super(c. class); override shortcutFieldOrder to specify the position of the field in the configuration file; Front GatewayFilterFactory characters, such as TokenGatewayFilterFactory, then filter name is Token, function implementation: filter implementation, from the request for the value of a parameter, print it outCopy the code

In Java: @Component @Slf4j public class TokenGatewayFilterFactory extends AbstractGatewayFilterFactory<TokenGatewayFilterFactory.Config> {

public TokenGatewayFilterFactory() { super(Config.class); } @Override public List<String> shortcutFieldOrder() { return Lists.newArrayList("tokenName"); } @override public GatewayFilter apply(Config Config) {return (exchange, chain) -> {log.info); String tokenName = config.getTokenName(); Log.info (" tokenName value obtained from configuration file =[{}].", tokenName); String value = exchange.getRequest().getQueryParams().getFirst(tokenName); Log.info (" Token value obtained from request is :[{}]", value); return chain.filter(exchange); }; }Copy the code

In the configuration file is written, the name of the custom filters for, in front of GatewayFilterFactory characters, such as TokenGatewayFilterFactory, then the filter name is Token

spring:
  cloud:
    gateway:
      routes:
        - id: product-provider-01
          uri: lb://product-provider
          predicates:
            - Path=/product/findOne
          filters:
            - Token=gateway-token
Copy the code