📜  通过Zuul API网关执行请求

📅  最后修改于: 2021-01-07 03:47:26             🧑  作者: Mango

通过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命名服务器上注册的所有服务。

第6步:调用URL http:// localhost:8000 / currency-exchange / from / EUR / to / INR 。我们得到了响应,但是请求没有通过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步:复制上述URL并将其粘贴到浏览器中。我们得到与上述相同的响应,但是此时,请求正在通过Zuul API Gateway进行

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

我们已经通过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:选择项目currency-conversion-service

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

步骤3:通过使用注释@FeignClient与属性名=“Netflix的-zuul-API网关服务器”启用

@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-servicenetflix-zuul-api-gateway-server。

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

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

让我们看一下NetflixZullApiGatewayServerApplication的日志。

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

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

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

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

现在,让我们拦截Currency Converter-serviceCurrency-exchange-service之间的调用。这意味着当我们调用URL时,API网关执行两次。

  • 首次调用API网关时,我们会调用货币转换服务。这意味着在执行货币转换服务之前。通过API网关路由的货币转换服务。
  • 第二次, API网关在货币转换服务调用货币交换服务时执行。这意味着执行currency-conversion-service之后,在执行currency-exchange-service之前。货币兑换服务也通过API网关路由。

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

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

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

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

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

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