6

要黑盒测试微服务内部服务间调用,我该如何实现?

 3 years ago
source link: http://blog.720ui.com/2020/hoverfly/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

要黑盒测试微服务内部服务间调用,我该如何实现?

发表于 2020-02-10 | 微服务 | 架构

本文总阅读量 286 次

单体系统和微服务的区别在于,一个单体系统是一个大而全的功能集合,每个服务器运行的是这个应用的完整服务。而微服务是独立自治的功能模块,它是生态系统中的一部分,和其他微服务是共生关系。随着微服务架构的普及,我们遇到了许多服务之间相互依赖。那么,我们需要对其进行模块测试就有点力不从心了。例如,我们的模块中存在服务 A 内部依赖于服务 B,而整个模块又依赖于服务 C,如图所示。

image.png
那么,针对微服务的内部服务依赖,我们如何实现测试呢?
resize,w_540#align=left&display=inline&height=413&originHeight=540&originWidth=540&status=done&style=none&width=413

事实上,我们由两种方案。第一种方案,我们将所有依赖的服务都进行构建与打包,然后统一部署进行测试。但是呢,这种方案过于复杂,且可能会可能演变成集成测试。因此,第二种方案通过虚拟化服务进行 API 仿真就显得更加合适了。对此,Hoverfly 作为一种新的服务虚拟化工具,可以模拟 HTTP 和 HTTPS 服务。Hoverfly 会启动一个代理,并使用存储的内容对请求进行响应,而这个响应和真实服务针对特定的请求产生的响应完全一致。如果整个流程能被正确地执行,并且存储地响应和真实服务一致,则 Hoverfly 可以完美地模拟真实服务。

读者可以通过官方教程进行安装,链接:https://hoverfly.readthedocs.io/en/latest/pages/introduction/downloadinstallation.html

本文的示例教程以开放在 github,可以下载运行,链接:https://github.com/lianggzone/hoverfly-samples

其中最为核心的是 hoverfly-provider 服务提供一个 API 接口,通过 http://localhost:8080/health 提供服务。

  1. @RestController
  2. public class HealthController {
  3. @RequestMapping(value = "/health")
  4. public HealthVO getHealthInfo() {
  5. HealthVO healthVO = HealthVO.builder()
  6. .msg("OK")
  7. .timestrap(System.currentTimeMillis())
  8. .build();
  9. return healthVO;

而另外一个 hoverfly-service 服务通过 RestTemplate 进行内部服务调用,并通过 http://localhost:8089/health 提供服务。

  1. @Component
  2. public class HoverflyProxy {
  3. @Autowired
  4. private RestTemplate restTemplate;
  5. public HealthVO getHealthInfo(){
  6. String url = "http://localhost:8080/health";
  7. HealthVO healthVO = restTemplate.getForEntity(url, HealthVO.class).getBody();
  8. return healthVO;
  9. @RestController
  10. public class HealthController {
  11. @Autowired
  12. private HoverflyProxy hoverflyProxy;
  13. @RequestMapping(value = "/health")
  14. public HealthVO getHealthInfo() {
  15. return hoverflyProxy.getHealthInfo();

现在,准备好必要的工作后,我们来聊一聊如何使用 Hoverfly 进行虚拟化服务。首先,我们需要启动 Hoverfly。

  1. hoverctl start

Hoverfly 启动后,提示其代理端口和管理台端口,如图所示。

image.png

然后,我们将 Hoverfly 切换成捕获模式。

  1. hoverctl mode capture

我们启动 hoverfly-provider 服务和 hoverfly-service 服务,进行 http://localhost:8089/health 接口调用。注意的是,这里,我们需要指定 Hoverfly 作为代理。

  1. curl localhost:8089/health -H "Content-Type: Content-Type" --proxy localhost:8500

此时,我们通过指定 proxy 参数,请求会首先转发到 Hoverfly 代理服务,然后再被转发到真正的业务服务,而响应接受过程也是如此。

image.png

最后,我们采集足够多的样本后,就可以切换到仿真模式。

  1. hoverctl mode simulate

在仿真模式下,我们再次发起相同的请求。

  1. curl localhost:8089/health -H "Content-Type: Content-Type" --proxy localhost:8500

Hoverfly 会将之前记录袭来的请求来响应业务结果。

image.png

那么,我也可以通过其控制台进行查看。

image.png

事实上,一旦 Hoverfly 存储了请求和响应的数据,我们就不需要访问真正的业务访问,做到真正的仿真测试。次数,我们可以通过如下命名导出结果文件 simulations.json。

  1. hoverctl export simulate.json

然后,根据持续集成和持续测试的规格标准,进行配置文件依赖的初始化即可。(这里,可以通过 hoverctl import simulations.json 执行)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK