Tiven Wang
Wang Tiven April 06, 2018
425 favorite favorites
bookmark bookmark
share share

Spring Cloud Gateway 是一个新的基于 Spring Framework 5, Project Reactor and Spring Boot 2.0 的 API Gateway 产品。

Implementation

我们先来看看怎么实现,然后再去讨论它有什么优点。

接上一篇 Microservices - API Gateway : Zuul 我们创建好的 Book 应用程序(如果你对 Spring Boot 创建 Application 很熟悉可以不用关心原来的代码,因为它很简单),本篇使用 Spring Cloud Gateway 重新创建 API Gateway Application 。

Dependency

你可以选择手动添加需要的配置,也可以用 http://start.spring.io/ 从零创建一个项目。因为 Spring Cloud Gateway 需要 Spring Boot 2 的支持,所以应当选择 Spring Boot 2 以上版本。我们使用版本如下

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.1.RELEASE</version>
</parent>

在引入 Spring Cloud Gateway 依赖时尽量使用 pom 的方式

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gateway</artifactId>
            <version>2.0.0.M9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

如果你已经有 Spring Boot Application 类,那么应该可以没有错误地运行起来。

Routes

你可以通过配置文件自定义路由表,如下

spring:
  cloud:
    gateway:
      routes:
      - id: book_route
        uri: http://localhost:8090
        predicates:
        - Path=/available/**

Spring Cloud Gateway 是通过 Predicate (Java 8 Function Predicate) 匹配请求的,有很多种 Predicate 可供使用。上面是使用的 Path Route Predicate Factory 也就是通过区分请求的路径进行匹配。凡是路径匹配 /available/** 的请求都会被转发给 http://localhost:8090 服务。

还可以通过 filters (GatewayFilter) 修改经过路由里的请求。例如上面的例子我们要为路径添加个前缀 /book ,那么可以书写如下

spring:
  cloud:
    gateway:
      routes:
      - id: book_route
        uri: http://localhost:8090
        predicates:
        - Path=/book/available/**
        filters:
        - RewritePath=/book/(?<segment>.*), /$\{segment}

它通过为路径匹配添加前缀以区分不同的服务,然后再在 Filter 里去掉前缀后转发给相应服务。

Java DSL

如果想要更加灵活的编程,还可以使用其提供的 Java Routes API

// static imports from GatewayFilters and RoutePredicates
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
	//@formatter:off
	return builder.routes()
			.route(r -> r.path("/inventory/**")
				.filters(f ->
						f.rewritePath("/inventory/(?<segment>.*)", "/${segment}"))
				.uri("http://localhost:8091")
			)
			.build();
	//@formatter:on
}

本篇完整代码可下载自 Github

Conclusion

Similar Posts

  • Microservices - Inter-Process Communication The kernal of Microservices Architecture is inter-process communication
  • Microservices - API Gateway : Zuul Zuul 是来自 NetFlix 的 Microservice 产品家族的 API Gateway 服务或者说是 edge 服务。 Zuul 为开发者构建微服务架构提供了 Routing,Monitoring,Managing resiliency,Security等功能。简单来说,Zuul 可以被看作是一个反向代理,在服务实例间 Zuul 代理内部使用 Eureka server 作为 service discovery,使用Ribbon 作为 load balancing
  • Microservices - Transactions
  • Try Cloud Foundry - Logs Loggregator is the next generation system for aggregating and streaming logs and metrics from all of the user apps and system components in an Elastic Runtime deployment. Loggregator uses Google's protocol buffers along with gRPC to deliver logs.
  • Try Cloud Foundry - Config Server Config Server for Pivotal Cloud Foundry (PCF) is an externalized application configuration service, which gives you a central place to manage an application’s external properties across all environments.
  • Microservices - API Gateway Implement an API gateway that is the single entry point for all clients. The API gateway handles requests in one of two ways. Some requests are simply proxied/routed to the appropriate service. It handles other requests by fanning out to multiple services.

Comments

Back to Top