Spring Cloud gateway service I

Keywords: Programming Spring WebFlux Java

Before we introduced zuul gateway service, today we talk about spring cloud gateway as the parent-child gateway service of spring cloud. Many ideas are based on zuul, which provides a convenient condition to consider zuul's migration to gateway.

gateway's core functions are similar to zuul's. But his implementation method is different from zuul's. His core is based on Spring Boot 2.x, Spring WebFlux and Project Reactor.

  • Spring WebFlux responsive Web framework.
    • Spring WebFlux is based on reactive flow, so it can be used to build asynchronous, non blocking, event driven services. It uses Reactor as the preferred reactive flow implementation library, but also provides support for RxJava. Due to the characteristics of reactive programming, the underlying layer of Spring WebFlux and Reactor needs to support asynchronous running environment, such as Netty and Undertow; it can also run in the environment that supports asynchronous I/O
    • Servlet 3.1 containers, such as Tomcat (8.0.23 and above) and Jetty (9.0.4 and above).
  • The upper layer of spring webplus supports two development modes:
    • It is similar to the annotation based (@ Controller, @ RequestMapping) development mode of Spring WebMVC;
    • Java 8 lambda style functional development mode.
    • Spring WebFlux also supports responsive web socket server development.
So spring cloud gateway is not based on blocking web development. It conflicts with the traditional Servlet. Exclude traditional Servlet jar package references when creating functions

Working principle

The client makes a request to the Spring Cloud Gateway. If the gateway handler mapping determines that the request matches the route, it is sent to the gateway Web handler. The handler runs to send requests through a request specific filter chain. The reason filters are separated by dashed lines is that they can execute logic before or after sending proxy requests. Perform all "pre" filter logic, and then issue a proxy request. When a proxy request is made, the publish filter logic is executed.

Note: URIs defined in routes without ports will set the default ports for HTTP and HTTPS URI s to 80 and 443 respectively

  • Predicate assertion: This is a predicate for Java 8. The input type is a ServerWebExchange. We can use it to match anything from an HTTP request, such as headers or parameters.
  • Route route forwarding is defined by a serverID, a destination URI, a set of assertions, and a set of filters. If the assertion is true, the route matches.
  • Filter requests filtering to intercept web resources, do some processing, and then give it to the processor for processing

We have a spring boot starter web project reference in the total pom file of the project before modification and delete it. Independent dependence in service. As mentioned above, the jar package conflicts of traditional servlets.

Add to service consumers and service providers respectively

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

We create cloud gateway and modify pom

<dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

Create bootstrap.yml

server:
  port: 9000

spring:
  profiles:
    active: dev
  application:
    name: cloud-gateway-demo
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: non_null
  cloud:
    nacos:
      discovery:
        server-addr: 47.99.209.72:8848
      # ${prefix}-${spring.profile.active}.${file-extension}
      config:
        server-addr: 47.99.209.72:8848
        file-extension: yaml
    gateway:
      discovery:
        locator:
           # Whether to combine with service discovery component and forward to specific service instance through serviceId. Default false,
          # true means to turn on routing rules based on service discovery.
          enabled: true
          # No capitalization required for access after configuration
          lower-case-service-id: true
      routes:
        - id: cloud-discovery-server
          uri: lb://cloud-discovery-server
          predicates:
            # Path matching starts with api. Direct configuration is not effective. See filters configuration
            - Path=/server/**
          filters:
            # Prefix filtering. In the default configuration, our request path is http: / / localhost: 9000 / myshop service consumer item / *, which will be routed to the specified service
            # Remove one path prefix and configure the Path=/api / * * *, you can access it in the way of http://localhost:9000/api / * *
            - StripPrefix=1
        - id: cloud-discovery-client
          uri: lb://cloud-discovery-client
          predicates:
            # Path matching starts with api. Direct configuration is not effective. See filters configuration.
            - Path=/client/**
          filters:
            # Prefix filtering. In the default configuration, our request path is http: / / localhost: 9000 / myshop service consumer item / *, which will be routed to the specified service
            # Remove one path prefix and configure the Path=/api / * * *, you can access it in the way of http://localhost:9000/api / * *
            - StripPrefix=1

Create main startup class

package com.xian.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


/**
 *
 * @Author: xlr
 * @Date: Created in 11:08 a.m. November 4, 2019
 */
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication {

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

Start service command line curl http://localhost:9000/client/client/test

The services have been integrated. Routing function forwarding has been implemented. The description of some fields in the configuration file is also described on the comments. Next, I'll talk about the Spring Cloud Gateway assertion

How to like can pay attention to share this public number.

Copyright notice: This is the original article of the blogger, following CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint. Please upload the public number two-dimensional code.

Posted by paggard on Mon, 04 Nov 2019 17:07:21 -0800