【英文】Gateway统一网关

Preface

This project provides a library for building an API Gateway on top of Spring WebFlux. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency. (Official Website)

Import Dependencies

pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

Add the Gateway to the Eureka Registry

Add Dependencies

pom.xml
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Add Annotations

  • Add annotations to the main application class to start the Eureka client.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com;

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

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {

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

}

Update Configuration

src/main/resources/application.yml
1
2
3
4
5
6
7
8
9
server:
port: 8100
spring:
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8000/eureka

Implement Request Routing

Add Configuration

  • When the request path matches the route predicate rule, forward the request to the target path of the route.

spring.cloud.gateway.default-filters: Default filters that apply to all routes (DefaultFilter)
spring.cloud.gateway.routes.id: Id of the route
spring.cloud.gateway.routes.uri: Target path of the route

lb://service-name: Send the request based on the service name
http://ip-address: Send the request based on the IP address
spring.cloud.gateway.routes.predicates: Predicates for the specified route
spring.cloud.gateway.routes.filters: Filters for the specified route

src/main/resources/application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
cloud:
gateway:
routes:
- id: eureka-service-user
uri: lb://eureka-service-user
predicates:
- Path=/users/**
- id: eureka-service-user
uri: http://127.0.0.1:8082
predicates:
- Path=/orders/**
filters:
- AddRequestHeader=key, value
default-filters:
- AddRequestHeader=key, value

Route Predicates

spring.cloud.gateway.routes.predicates.After: After a specified time
spring.cloud.gateway.routes.predicates.Before: Before a specified time
spring.cloud.gateway.routes.predicates.Between: Between two specified times
spring.cloud.gateway.routes.predicates.Cookie: Match specified cookies
spring.cloud.gateway.routes.predicates.Header: Match specified headers
spring.cloud.gateway.routes.predicates.Host: Match specified request domains
spring.cloud.gateway.routes.predicates.Method: Match specified request methods
spring.cloud.gateway.routes.predicates.Path: Match specified request paths
spring.cloud.gateway.routes.predicates.Query: Match specified request parameters
spring.cloud.gateway.routes.predicates.RemoteAddr: Match specified request IP ranges
spring.cloud.gateway.routes.predicates.Weight: Match specified groups and weights

spring:
  cloud:
    gateway:
      routes:
        - predicates:
          - After=2017-01-20T17:42:47.789-07:00[America/Denver]
          - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
          - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
          - Cookie=chocolate, ch