Spring Cloud GateWay service gateway service and filter

Foreword

This article explains the syntax of a single service gateway proxy, in practical work, call each service it is dependent on the entrance to the service center to use the service centers are often registered a lot of service, if you need to configure each service separately then this will be a very boring job. Spring Cloud Gateway provides the ability to forward one of the default, as long as the Spring Cloud Gateway registered to the service center, all services Spring Cloud Gateway will default proxy service center, with the following code demonstrates.

Preparation and registration service center

When using the referral service gateway zuul provided a spring-cloud-eureka, spring-cloud-producer project examples, this presentation we will continue to upgrade versions of the two projects to demonstrate the use of Finchley.SR2 later.

spring-cloud-eureka (Eureka Server) of pom file changes, other dependencies unchanged.

Upgrade ago:

Spring Cloud GateWay service gateway service and filter


After the upgrade:

Spring Cloud GateWay service gateway service and filter


spring-cloud-producer (Eureka Client) of pom file changes. Because the configuration required to register as a service center to the registry, so it is necessary to upgrade Eureka Client, does not depend on other changes.

Upgrade ago:

Spring Cloud GateWay service gateway service and filter


After the upgrade:

Spring Cloud GateWay service gateway service and filter


Two projects completed in order to restart the upgrade dependencies, visit registration centers address http: // localhost: 8000 / to see a service called SPRING-CLOUD-PRODUCER of.

Services Gateway registered to the registry

Copy on a sample cloud-gateway project renamed cloud-gateway-eureka, eureka client add dependencies.

Spring Cloud GateWay service gateway service and filter


Modify the configuration file as follows application.yml

Spring Cloud GateWay service gateway service and filter


Configuration instructions:

  • spring.cloud.gateway.discovery.locator.enabled: whether the service registry to find components combined forwarded to a specific service instances by serviceId. The default is false, set to true will open to create a routing function by automatically according to the service center serviceId.

  • eureka.client.service-url.defaultZone指定注册中心的地址,以便使用服务发现功能

  • logging.level.org.springframework.cloud.gateway 调整相 gateway 包的 log 级别,以便排查问题

修改完成后启动 cloud-gateway-eureka 项目,访问注册中心地址 http://localhost:8000/ 即可看到名为 CLOUD-GATEWAY-EUREKA的服务。

测试

将 Spring Cloud Gateway 注册到服务中心之后,网关会自动代理所有的在注册中心的服务,访问这些服务的语法为:http://网关地址:端口/服务中心注册 serviceId/具体的url

比如我们的 spring-cloud-producer 项目有一个 /hello 的服务,访问此服务的时候会返回:hello world。

比如访问地址:http://localhost:9000/hello,页面返回:hello world!

按照上面的语法我们通过网关来访问,浏览器输入:http://localhost:8888/SPRING-CLOUD-PRODUCER/hello 同样返回:hello world!证明服务网关转发成功。

我们将项目 spring-cloud-producer 复制一份为 spring-cloud-producer-1,将/hello服务的返回值修改为 hello world smile !,修改端口号为 9001 ,修完完成后重启,这时候访问注册中心后台会发现有两个名为 SPRING-CLOUD-PRODUCER的服务。

在浏览器多次访问地址:http://localhost:8888/SPRING-CLOUD-PRODUCER/hello,页面交替返回以下信息:

hello world!
hello world smile!

说明后端服务自动进行了均衡负载。

基于 Filter(过滤器) 实现的高级功能

在服务网关Zuul高级篇中大概介绍过 Filter 的概念。

Spring Cloud Gateway 的 Filter 的生命周期不像 Zuul 的那么丰富,它只有两个:“pre” 和 “post”。

  • PRE: 这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。

  • POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的 HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。

Spring Cloud Gateway 的 Filter 分为两种:GatewayFilter 与 GlobalFilter。GlobalFilter 会应用到所有的路由上,而 GatewayFilter 将应用到单个路由或者一个分组的路由上。

Spring Cloud Gateway 内置了9种 GlobalFilter,比如 Netty Routing Filter、LoadBalancerClient Filter、Websocket Routing Filter 等,根据名字即可猜测出这些 Filter 的作者,具体大家可以参考官网内容:Global Filters

利用 GatewayFilter 可以修改请求的 Http 的请求或者响应,或者根据请求或者响应做一些特殊的限制。 更多时候我们会利用 GatewayFilter 做一些具体的路由配置,下面我们做一些简单的介绍。

快速上手 Filter 使用

我们以 AddRequestParameter GatewayFilter 来演示一下,如何在项目中使用 GatewayFilter,AddRequestParameter GatewayFilter 可以在请求中添加指定参数。

application.yml配置示例

Spring Cloud GateWay service gateway service and filter


这样就会给匹配的每个请求添加上foo=bar的参数和值。

我们将以上配置融入到 cloud-gateway-eureka 项目中,完整的 application.yml 文件配置信息如下:

Spring Cloud GateWay service gateway service and filter


这里的 routes 手动指定了服务的转发地址,设置所有的 GET 方法都会自动添加foo=bar,http://localhost:9000 是 spring-cloud-producer 项目,我们在此项目中添加一个 foo() 方法,用来接收转发中添加的参数 foo。

Spring Cloud GateWay service gateway service and filter


修改完成后重启 cloud-gateway-eureka、spring-cloud-producer 项目。访问地址http://localhost:9000/foo页面返回:hello null!,说明并没有接受到参数 foo;通过网关来调用此服务,浏览器访问地址http://localhost:8888/foo页面返回:hello bar!,说明成功接收到参数 foo 参数的值 bar ,证明网关在转发的过程中已经通过 filter 添加了设置的参数和值。

服务化路由转发

上面我们使用 uri 指定了一个服务转发地址,单个服务这样使用问题不大,但是我们在注册中心往往会使用多个服务来共同支撑整个服务的使用,这个时候我们就期望可以将 Filter 作用到每个应用的实例上,spring cloud gateway 工了这样的功能,只需要简单配置即可。

为了测试两个服务提供者是否都被调用,我们在 spring-cloud-producer-1 项目中也同样添加 foo() 方法。

Spring Cloud GateWay service gateway service and filter


为了和 spring-cloud-producer 中 foo() 方法有所区别,这里使用了两个感叹号。同时将 cloud-gateway-eureka 项目配置文件中的 uri 内容修改如下:

Spring Cloud GateWay service gateway service and filter


修改完之后,重新启动项目 cloud-gateway-eureka、spring-cloud-producer-1,浏览器访问地址:http://localhost:8888/foo页面交替出现:

Spring Cloud GateWay service gateway service and filter


Attestation request forwarded to the back-end service based on uniformity, and back-end services are received to filter the increased value of the parameter foo.

As used herein, the global filter LoadBalancerClient fact default configuration when the routing protocol is used uri LB (to uri: lb: // spring-cloud-producer as an example), using the Gateway LoadBalancerClient the spring-cloud-producer by eureka resolves to the actual host and port, and load balancing.


Guess you like

Origin blog.51cto.com/14528283/2436921