个性化阅读
专注于IT技术分析

Spring Cloud通过Zuul API网关执行请求

步骤1:运行netflix-eureka-naming-server。

步骤2:在端口8000上运行currency-exchange-service。

步骤3:在端口8100上运行currency-conversion-service。

步骤4:运行netflix-zuul-api-gateway-server。

步骤5:打开浏览器并调用URL http:// localhost:8761。它显示了在Eureka命名服务器上注册的所有服务。

通过Zuul API网关执行请求

第6步:调用URL http:// localhost:8000 / currency-exchange / from / EUR / to / INR。我们得到了响应, 但是请求没有通过Zuul API网关。

通过Zuul API网关执行请求

让我们通过Zuul API网关调用请求。我们使用以下URL:http:// localhost:8765 / {application-name} / {uri}。端口8765是Zuul API网关服务器的默认端口。

在我们的例子中, 应用程序名称为currency-exchange-service, 而URI为/ currency-exchange / from / EUR / to / INR。因此, 完整的URL如下所示:

http:// localhost:8765 / currency-exchange-service / currency-exchange / from / EUR / to / INR

步骤7:复制上述网址, 并将其粘贴到浏览器中。我们得到与上述相同的响应, 但是此时, 请求正在通过Zuul API网关进行。

通过Zuul API网关执行请求

我们还可以看到在Zuul API Gateway服务器上打印的请求的内容。该请求将打印请求URI。

通过Zuul API网关执行请求

我们已经通过Zuul API网关发送了请求, 而不是直接调用微服务。

在微服务调用之间设置Zuul API网关

在上一步中, 我们使用了直接URL通过Zuul API Gateway代理执行currency-exchange-service。当我们使用URL http:// localhost:8765 / currency-exchange-service / currency-exchange / from / EUR / to / INR时, 它使用的端口8765是API网关的代理。

在本节中, 我们将调用称为currency-exchange-service的currency-calculation-service(currency-conversion-service)。到目前为止, 我们正在直接致电该服务。现在, 我们将通过Zuul API网关调用它, 而不是直接调用currency-exchange-service。

步骤1:选择项目货币转换服务。

步骤2:打开CurrencyExchangeServiceProxy.java文件。

步骤3:通过使用属性名称为” =” netflix-zuul-api-gateway-server”的@FeignClient注释启用Feign。

@FeignClient(name="netflix-zuul-api-gateway-server")

切记:删除或注释CurrencyExchangeServiceProxy.java文件中的所有其他注释@FeignClient。

步骤4:为Zuul API Gateway服务器定义映射。

@GetMapping("/currency-exchange-service/currency-exchange/from/{from}/to/{to}")

切记:删除或注释currency-exchange-service的映射。

步骤5:按照我们编写的顺序运行netflix-eureka命名服务器, currency-exchange-service, currency-conversion-service和netflix-zuul-api-gateway-server。

切记:确保所有四个服务均正常运行。

步骤6:打开浏览器并调用URL http:// localhost:8100 / currency-converter-feign / from / USD / to / INR / quantity / 1000。它返回以下响应:

通过Zuul API网关执行请求

让我们看一下NetflixZullApiGatewayServerApplication的日志。

步骤7:单击控制台图标旁边的箭头, 然后选择NetflixZullApiGatewayServerApplication。

通过Zuul API网关执行请求

它显示了几个日志, 如下图所示。

通过Zuul API网关执行请求

步骤8:再次刷新URL。它在控制台上显示单个日志。

通过Zuul API网关执行请求

每当我们通过Feign调用CurrencyClaculationService(currency-converter-service)时, 都会通过API网关服务器进行路由。网关执行一个名为ZuulLoggingFilter的过滤器, 该过滤器将调用currency-exchange-service。

现在, 让我们截取货币转换器服务和货币交换服务之间的调用。这意味着当我们调用URL时, API网关执行两次。

  • 当我们调用货币转换服务时, API网关第一次执行。这意味着在执行货币转换服务之前。通过API网关路由的货币转换服务。
  • 第二次, API网关在货币转换服务调用货币交换服务时执行。这意味着在执行货币转换服务之后和在执行货币交换服务之前。货币兑换服务也通过API网关路由。

让我们在项目中实现拦截。

通过API网关发送请求http:// localhost:8765。 URI为/ {application-name} / {uri}。完整的URL如下所示:

http:// localhost:8765 / currency-conversion-service / currency-converter-feign / from / USD / to / INR / quantity / 1000

调用上述URL。它返回的响应与URL http:// localhost:8100 / currency-converter-feign / from / USD / to / INR / quantity / 1000返回的响应相同。

通过Zuul API网关执行请求

我们可以在日志中看到日志过滤器执行了两次。第一次调用货币转换器服务, 第二次调用货币转换器服务。

通过Zuul API网关执行请求

在本节中, 我们通过Zuul API网关服务器执行了这两项服务。

点击这里下载货币兑换服务

点击这里下载货币兑换服务

点击这里下载netflix-eureka-naming-server

点击这里下载netflix-zuul-api-gateway-server


赞(0)
未经允许不得转载:srcmini » Spring Cloud通过Zuul API网关执行请求

评论 抢沙发

评论前必须登录!