SpringCloud微服务

SpringCloud 执行请求

步骤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-server URI /currency-exchange/from/EUR/to/INR 。因此,完整的URL如下所示:
http://localhost:8765/currency-exchange-service/currency-exchange/from/EUR/to/INR
步骤7: 复制上面的URL并将其粘贴到浏览器中。我们得到与上述相同的响应,但此时,请求正在通过 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-conversion-service)。到目前为止,我们正在直接致电该服务。现在,我们将通过Zuul API网关调用它,而不是直接调用currency-exchange-service。
步骤1: 选择项目 currency-conversion-service
步骤2: 打开 CurrencyExchangeServiceProxy.java 文件。
步骤3: 使用带有属性 name="netflix-zuul-api-gateway-server"的注释 @FeignClient 启用 假冒
@FeignClient(name="netflix-zuul-api-gateway-server")
记住: 删除或注释 CurrencyExchangeServiceProxy.java 文件中的所有其他注释@FeignClient。
步骤4: 为Zuul API网关服务器定义 映射
@GetMapping("/currency-exchange-service/currency-exchange/from/{from}/to/{to}")
步骤5: 运行 netflix-eureka-naming-server,currency-exchange-server 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 Gateway
它显示了几个日志,如下图所示。
通过Zuul API网关执行请求
步骤8: 再次刷新URL。它在控制台上显示单个日志。
通过Zuul API Gateway
每当我们通过Feign调用CurrencyClaculationService(currency-converter-service)时,都会通过API Gateway服务器进行路由。网关执行一个名为 ZuulLoggingFilter 的过滤器,该过滤器调用currency-exchange-server。
现在,让我们拦截 currency converter-service currency-exchange-service之间的调用 。这意味着当我们调用URL时,API网关会执行 两次
第一次,API网关在我们调用currency-conversion-service时执行。这意味着在执行currency-conversion-service之前。通过API网关路由的currency-conversion-service。 第二次 API网关在currency-conversion-service调用货币兑换服务时执行。这意味着在执行 currency-conversion-service 之后之后,在 currency-exchange-service 执行之前之前。货币兑换服务也通过API网关路由。
让我们在我们的项目中实现拦截。
发送请求 http://localhost:8765 API网关访问 。 URI将为 /{application-name}/{uri}。完整的URL如下所示:
http://localhost:8765/currency-conversion-service/currency-converter-feign/from/USD/to/INR/quantity/100 0
调用上述网址。它返回与URLhttp://localhost:8100/currency-converter-feign/from/USD/to/INR/quantity/1000 返回相同的响应。
通过Zuul API网关执行请求
我们在日志中看到日志记录过滤器执行了 两次首次调用 货币转换器服务第二次调用 货币转换器服务称为 currency-exchange-service
通过Zuul API网关执行请求
在本节中,我们已经通过Zuul API网关服务器执行了这两项服务。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4