This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

Use gateway

1. Add dependencies

<! -- Spring Cloud Gateway dependencies -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
Copy the code

2, the resources/application. Yml configuration file

server:
  port: 8080

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        # System module
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1
Copy the code

3. Gateway startup class

@SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); System.out.println("-- gateway started successfully --"); }}Copy the code

prompt

The Demo-gateway gateway service is used for route forwarding, exception handling, traffic limiting, degradation, interface, and authentication.

Routing rules

When the Spring Cloud Gateway creates the Route object, the RoutePredicateFactory is used to create the Predicate object, which can be assigned to the Route.

  • Spring Cloud GatewayContains many built-inRoute Predicate Factories.
  • All of these assertions match different attributes of the HTTP request.
  • multipleRoute Predicate FactoriesYou can do this by logic(and)Use it together.

RoutePredicateFactory contains the main implementation classes as shown in the figure, including Datetime, request remote address, route weight, request header, Host address, request method, request path, request parameters and other types of route assertions.

Datetime

Matches requests that occur after date and time

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - After = 2021-02-23 T14:20:00. 000 + 08:00 Asia/Shanghai
Copy the code

Cookie

Matches cookies whose names are specified and whose values match the regular expression

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Cookie=loginname, admin
Copy the code

Test the curl http://localhost:8080/system/config/1 — cookies “loginname = admin”

Header

Matches the request header with the specified name, and the \d+ value matches the regular expression

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Header=X-Request-Id, \d+
Copy the code

Host

A list of matching host names

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Host=**.somehost.org,**.anotherhost.org
Copy the code

Method

Match the parameters of the request methods, which are one or more parameters

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Method=GET,POST
Copy the code

Path

Match request path

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Path=/system/**
Copy the code

Query

Matching query parameters

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - Query=username, abc.
Copy the code

RemoteAddr

Matches the IP address and subnet mask

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system
          uri: http://localhost:9201/
          predicates:
            - RemoteAddr = 192.168.10.1/0
Copy the code

Weight

Match the weight

spring: 
  application:
    name: demo-gateway
  cloud:
    gateway:
      routes:
        - id: demo-system-a
          uri: http://localhost:9201/
          predicates:
            - Weight=group1, 8
        - id: demo-system-b
          uri: http://localhost:9201/
          predicates:
            - Weight=group1, 2
Copy the code