API 网关是微服务架构重要组件之一,是服务唯一入口。API 网关封装内部系统架构,横向抽离通用功能,如:身份验证、监控、限流、熔断、负载均衡等。核心要点是,所有的客户端和消费端都通过统一的网关接入微服务,在网关层处理所有的非业务功能。

反向代理 VS 网关

springcloud微服务api设计(微服务架构之API网关)(1)

在 web 1.0,2.0 时代,主要由web网站形式呈现。大多数都采用前置反向代理,主要作用是反向路由和负载均衡。典型的产品有 Nginx 和 HAproxy。一般采用静态配置方式,由运维团队配置。

如今来到了 web 3.0 微服务的时代,出现了大量提供 API 的服务。这些服务升级频率很高,要求对安全和动态配置的能力很高,架构慢慢衍生出网关。反向代理有的功能网关都具备。以 Netflix Zuul 为代表。

网关主要给微服务/API用,偏向开发人员,反向代理主要面向传统静态 web 应用,偏向运维。未来趋势(云原生时代)是DevOps 网关和反向代理再次融合。

网关应当具备以下功能:

springcloud微服务api设计(微服务架构之API网关)(2)

主流网关对比

网关支持公司实现语言亮点不足Nginx (2004)Nginx IncC/Lua高性能,成熟稳定门槛高,偏运维,可编程弱Kong (2014)Kong IncOpenResty/Lua高性能,可编程API门槛较高Zuul1 (2012)Netflix/PivotalJava成熟,简单门槛低性能一般,可编程一般Spring Cloud Gateway (2016)PivotalJava异步,配置灵活早期产品(现已经比较成熟)Envoy (2016)LyftC 高性能,可编程API/ ServiceMesh 集成门槛较高Traefik (2015)ContainousGolang云原生,可编程API/对接各种服务发现生产案例不多

暂时选择 Spring Cloud Gateway,原因如下:

Spring Cloud Gateway 简介

Spring Cloud Gateway 为 SpringBoot 应用提供了API网关支持,具有强大的智能路由与过滤器功能。Gateway 是在 Spring 生态系统之上构建的 API 网关服务,基于 Spring 5,Spring Boot 2和 Project Reactor等技术。

Spring Cloud Gateway 具有如下特性:

相关概念工作原理

springcloud微服务api设计(微服务架构之API网关)(3)

很像 SpringMVC 的请求处理过程,客户端向 Spring Cloud Gateway 发出请求。如果 Gateway Handler Mapping确定请求与路由匹配,则将其发送给 Gateway Web Handler。这个 Handler 运行通过特定于请求的过滤器链发送请求。过滤器可以在发送代理请求之前或之后执行逻辑。执行所有的“pre”过滤逻辑,然后发出代理请求,最后执行“post”过滤逻辑。

环境集成 Gateway

创建 api-gateway 模块,在 pom.xml 中添加相关依赖:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>

两种不同的配置路由方式

Gateway 提供了两种不同的方式用于配置路由,一种是通过 yml 文件来配置,另一种是通过 Java Bean 来配置。

使用yml配置

在 application.yml 中进行配置:

@Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route2", r -> r.path("/user/getByUsername") .uri("http://localhost:8201/user/getByUsername")) .build(); } }

使用Java Bean配置

添加相关配置类,并配置一个 RouteLocator 对象:

@Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route2", r -> r.path("/user/getByUsername") .uri("http://localhost:8201/user/getByUsername")) .build(); } }

Route Predicate 的使用

Spring Cloud Gateway 将路由匹配作为 Spring WebFlux HandlerMapping 基础架构的一部分。 Spring Cloud Gateway 包括许多内置的 Route Predicate工厂。 所有这些 Predicate 都与 HTTP 请求的不同属性匹配。 多个 Route Predicate 工厂可以进行组合

注意:Predicate 中提到的配置都在 application-predicate.yml 文件中进行修改,并用该配置启动api-gateway 服务。

After Route Predicate

在指定时间之后的请求会匹配该路由。

spring: cloud: gateway: routes: - id: before_route uri: ${service-url.user-service} predicates: - Before=2020-02-14T20:30:00 08:00[Asia/Shanghai]

Before Route Predicate

在指定时间之前的请求会匹配该路由。

spring: cloud: gateway: routes: - id: cookie_route uri: ${service-url.user-service} predicates: - Cookie=username,leo

Between Route Predicate

在指定时间区间内的请求会匹配该路由。

spring: cloud: gateway: routes: - id: cookie_route uri: ${service-url.user-service} predicates: - Cookie=username,leo

Cookie Route Predicate

带有指定Cookie的请求会匹配该路由。

spring: cloud: gateway: routes: - id: cookie_route uri: ${service-url.user-service} predicates: - Cookie=username,leo

curl http://localhost:9201/user/1 --cookie "username=leo"

Header Route Predicate

带有指定请求头的请求会匹配该路由。

spring: cloud: gateway: routes: - id: header_route uri: ${service-url.user-service} predicates: - Header=X-Request-Id, \d

curl http://localhost:9201/user/1 -H "X-Request-Id:111"

Host Route Predicate

带有指定Host的请求会匹配该路由。

spring: cloud: gateway: routes: - id: host_route uri: ${service-url.user-service} predicates: - Host=**.lizijian.cn

curl http://localhost:9201/user/1 -H "Host:www.lizijian.cn"

Method Route Predicate

发送指定方法的请求会匹配该路由。

spring: cloud: gateway: routes: - id: method_route uri: ${service-url.user-service} predicates: - Method=GET

Path Route Predicate

发送指定路径的请求会匹配该路由。

spring: cloud: gateway: routes: - id: path_route uri: ${service-url.user-service}/user/{id} predicates: - Path=/user/{id} - id: path_route2 uri: https://example.org predicates: - Path=/foo/{segment},/bar/{segment} # 所有带auth-api都会路由 - id: auth_service_api_route uri: http://localhost:8081/auth-api predicates: - Path=/auth-api/**

Query Route Predicate

带指定查询参数的请求可以匹配该路由。

spring: cloud: gateway: routes: - id: query_route uri: ${service-url.user-service}/user/getByUsername predicates: - Query=username

RemoteAddr Route Predicate

从指定远程地址发起的请求可以匹配该路由。

spring: cloud: gateway: routes: - id: remoteaddr_route uri: ${service-url.user-service} predicates: - RemoteAddr=192.168.1.1/24

Weight Route Predicate

使用权重来路由相应请求,以下表示有80%的请求会被路由到 localhost:8201,20% 会被路由到 localhost:8202。

public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory<HeaderRoutePredicateFactory.Config> { public MyRoutePredicateFactory() { super(Config.class); } @Override public Predicate<ServerWebExchange> apply(Config config) { // grab configuration from Config object return exchange -> { //grab the request ServerHttpRequest request = exchange.getRequest(); //take information from the request to see if it //matches configuration. return matches(config, request); }; } public static class Config { //Put the configuration properties for your filter here } }

RewritePath Route Predicate

重写路径,对于的请求路径/red/blue,在发出下游请求之前路径将会设置为/blue。由于YAML规范,$应将替换$\为。

curl http://localhost:9201/user/getByUsername # 相当于发起该请求: curl http://localhost:8201/user/getByUsername?username=leo

自定义 Route Predicate 工厂

curl http://localhost:9201/user/getByUsername # 相当于发起该请求: curl http://localhost:8201/user/getByUsername?username=leo

Route Filter 的使用

路由过滤器可用于修改进入的 HTTP 请求和返回的 HTTP 响应,路由过滤器只能指定路由进行使用。Spring Cloud Gateway 内置了多种路由过滤器,他们都由 GatewayFilter 的工厂类来产生。

AddRequestParameter GatewayFilter

给请求添加参数的过滤器。

curl http://localhost:9201/user/getByUsername # 相当于发起该请求: curl http://localhost:8201/user/getByUsername?username=leo

curl http://localhost:9201/user/getByUsername # 相当于发起该请求: curl http://localhost:8201/user/getByUsername?username=leo

AddRequestHeader GatewayFilter

给请求添加一个请求头 X-Request-Foo:Bar

spring: cloud: gateway: routes: - id: add_response_head_route uri: http://localhost:8201 filters: - AddResponseHeader=X-Response-Foo, Bar

AddResponseHeader GatewayFilter

给请求添加一个响应头 X-Response-Foo:Bar

curl http://localhost:9201/user-service/a/user/1 # 相当于发起该请求: curl http://localhost:8201/user/1

StripPrefix GatewayFilter

对指定数量的路径前缀进行去除的过滤器。

spring: cloud: gateway: routes: - id: hystrix_route uri: http://localhost:8201 predicates: - Method=GET filters: - Hystrix=myCommandName

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency>

PrefixPath GatewayFilter

与 StripPrefix 过滤器恰好相反,会对原有路径进行增加操作的过滤器。

spring: cloud: gateway: routes: - id: prefix_path_route uri: http://localhost:8201 predicates: - Method=GET filters: - PrefixPath=/user

Hystrix GatewayFilter

Hystrix 过滤器允许你将断路器功能添加到网关路由中,使你的服务免受级联故障的影响,并提供服务降级处理。

要开启断路器功能,我们需要在 pom.xml 中添加 Hystrix 的相关依赖:

public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory<PostGatewayFilterFactory.Config> { public PostGatewayFilterFactory() { super(Config.class); } @Override public GatewayFilter apply(Config config) { // grab configuration from Config object return (exchange, chain) -> { return chain.filter(exchange).then(Mono.fromRunnable(() -> { ServerHttpResponse response = exchange.getResponse(); //Manipulate the response in some way })); }; } public static class Config { //Put the configuration properties for your filter here } }

然后添加相关服务降级的处理类:

spring: cloud: gateway: routes: - id: custom_gateway_filter_factories uri: https://example.org filters: - Pre

在 application-filter.yml 中添加相关配置,当路由出错时会转发到服务降级处理的控制器上:

@Configuration public class FilterFactory { @Bean public PreGatewayFilterFactory preGatewayFilterFactory() { return new PreGatewayFilterFactory(); } @Bean public PostGatewayFilterFactory postGatewayFilterFactory() { return new PostGatewayFilterFactory(); } }

或者指定一个 HystrixCommand 的名字:

@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) { return builder.routes() .route(r -> r.host("**.abc.org").and().path("/image/png") .filters(f -> f.addResponseHeader("X-TestHeader", "foobar")) .uri("http://httpbin.org:80") ) .route(r -> r.path("/image/webp") .filters(f -> f.addResponseHeader("X-AnotherHeader", "baz")) .uri("http://httpbin.org:80") ) .route(r -> r.order(-1) .host("**.throttle.org").and().path("/get") .filters(f -> f.filter(throttle.apply(1, 1, 10, TimeUnit.SECONDS))) .uri("http://httpbin.org:80") ) .build(); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/get") .uri("http://httpbin.org")) .route("host_route", r -> r.host("*.myhost.org") .uri("http://httpbin.org")) .route("hystrix_route", r -> r.host("*.hystrix.org") .filters(f -> f.hystrix(c -> c.setName("slowcmd"))) .uri("http://httpbin.org")) .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org") .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback"))) .uri("http://httpbin.org")) .route("limit_route", r -> r .host("*.limited.org").and().path("/anything/**") .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter()))) .uri("http://httpbin.org")) .build(); }

RequestRateLimiter GatewayFilter

RequestRateLimiter 过滤器可以用于限流,使用 RateLimiter 实现来确定是否允许当前请求继续进行,如果请求太大默认会返回 HTTP 429-太多请求状态。

在pom.xml中添加相关依赖:

@Configuration public class FilterFactory { @Bean public PreGatewayFilterFactory preGatewayFilterFactory() { return new PreGatewayFilterFactory(); } @Bean public PostGatewayFilterFactory postGatewayFilterFactory() { return new PostGatewayFilterFactory(); } }

添加限流策略的配置类,这里有两种策略一种是根据请求参数中的 username 进行限流,另一种是根据访问 IP 进行限流:

@Configuration public class RedisRateLimiterConfig { @Bean(value = "userKeyResolver") KeyResolver userKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId")); } @Bean("hostNameKeyResolver") public KeyResolver hostNameKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName()); } @Bean("ipKeyResolver") public KeyResolver ipKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } }

使用 Redis 来进行限流,所以需要添加 Redis 和 RequestRateLimiter 的配置,这里对所有的 GET 请求都进行了按 IP 来限流的操作:

server: port: 9201 spring: redis: host: localhost password: 123456 port: 6379 cloud: gateway: routes: - id: requestratelimiter_route uri: http://localhost:8201 filters: - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 10 # 允许用户每秒处理多少个请求 (令牌桶每秒填充平均速率) redis-rate-limiter.burstCapacity: 20 # 允许在一秒钟内完成的最大请求数 (令牌桶总容量) key-resolver: "#{@ipKeyResolver}" # 限流策略,对应策略的Bean predicates: - Method=GET logging: level: org.springframework.cloud.gateway: debug Retry GatewayFilter

对路由请求进行重试的过滤器,可以根据路由请求返回的 HTTP 状态码来确定是否进行重试。

spring: cloud: gateway: routes: - id: retry_route uri: http://localhost:8201 predicates: - Method=GET filters: - name: Retry args: retries: 1 #需要进行重试的次数 statuses: BAD_GATEWAY #返回哪个状态码需要进行重试,返回状态码为5XX进行重试 backoff: firstBackoff: 10ms maxBackoff: 50ms factor: 2 basedOnPreviousValue: false

Default Filters

如果你想要添加一个过滤器并且把它应用于所有路由的话,你可以用 spring.cloud.gateway.default-filters。这个属性接受一个过滤器列表。

spring: cloud: gateway: default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar - PrefixPath=/httpbin

,