Introduction to spring cloud gateway

Introduction to spring cloud gateway

The official version of spring boot 2 was just released some time ago. Spring cloud gateway is a new project of spring cloud based on spring boot 2. The project provides an API gateway built on spring ecology, including spring 5, spring boot 2 and project reactor. Spring cloud gateway aims to provide a simple and effective way to send APIs and provide them with crosscutting concerns, such as security, monitoring / metrics and elasticity. The latest version is v2 0.0. M8, the official version will come soon.

Features of spring cloud gateway:

vs Netflix Zuul

Zuul is based on servlet 2.5 (using 3. X) and uses blocking API. It does not support any long connections, such as WebSockets. Gateway is built on spring framework 5, project reactor and spring boot 2 and uses non blocking API. WebSockets are supported and will be a better development experience because of its close integration with spring.

Getting started with spring cloud gateway

The author recently studied the source code of spring cloud gateway, and wrote articles on source code analysis for the implementation of most functions, but after all, the official version has not been released. This article is an introductory practice to show several commonly used functions, and looks forward to the recent official version release.

The example starts two services: gateway server and user server. The simulated scenario is that the client requests back-end services, and the gateway provides a unified entry for back-end services. All back-end services are registered with service discovery consult (both build ZK and Eureka are OK, and the author is used to using consult). The gateway forwards to specific back-end services through load balancing.

User services

The user service registers with consul and provides an interface / test.

rely on

The required dependencies are as follows:

configuration file

Exposed interface

Expose the / test interface and return OK.

Gateway service

The gateway service provides a variety of functions such as routing configuration, routing assertion factory and filter factory.

rely on

Dependencies to be introduced:

As mentioned above, kotlin related dependencies are introduced. Here, you need to support kotlin routing configuration. The use of spring cloud gateway needs to exclude web related configurations and introduce a reference to weblux. It will be checked when the application starts and must be introduced.

Route assertion factory

There are many types of routing assertion factories, depending on the time of the request, host, path, method, and so on. The following definition is a path based route assertion match.

When the requested path is / testfun, the status code of OK is returned directly, and the response body is hello string.

Filter factory

The gateway often needs to filter routing requests and perform some operations, such as constructing headers after authentication. There are many types of filtering, such as adding request headers, request parameters, response headers and circuit breakers.

As described above, when the request path is / image / webp, the request is forwarded to http://httpbin.org:80 , and filter the response, adding the header x-anotherheader: Baz.

Custom routing

The above two sections are API custom routes, which can also be defined through configuration:

The above configuration defines routing and filters. The global filter adds all responses to the header x-response-default-foo: default bar. The ID is defined as default_ path_ to_ HTTP routing has a low priority. When the requests cannot match, they will be forwarded to blueskykong com。

Kotlin custom routing

Spring cloud gateway can use kotlin to customize routes:

When the requested path is / image / PNG, it will be forwarded to http://httpbin.org:80 , and set the filter, and add the x-testheader: foobar header to its response header.

Service discovery component

It is combined with the service registered in the discovery component and forwarded to the specific service instance through serviceid. The corresponding dependencies have been introduced in the previous configuration.

Inject discoveryclient into the constructor of discoveryclientroutedrefinitionlocator. The route definition locator will be explained in the following source code analysis, which will not be expanded here.

The above configuration enables the implementation of the discoveryclient locator. The route defines that all requests whose request path starts with / user will be forwarded to the user service, and the path filter will be applied to intercept the first part of the path prefix. That is, the actual request to access / user / test is converted into LB: / / user / test.

websocket

You can also configure the gateway route of websocket:

Start a WS server wscat -- listen 9000, start the gateway (the gateway port is 9090), and connect the client. Wscat -- connect WS: / / localhost: 9090 / echo.

Client access

For the above functions, readers can download the source code to try. The author only shows the results of accessing user services:

The gateway successfully load balances to the user server and returns OK. The header of the response contains the header x-response-default-foo: default bar of the global filter settings

summary

In this article, we discussed some functions and components belonging to spring cloud gateway. This new API provides out of the box tools for gateway and proxy support. We look forward to the official version of spring cloud gateway 2.0.

Source address

https://github.com/keets2012/Spring-Cloud_Samples

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>