欢迎光临散文网 会员登陆 & 注册

软件测试 | Dubbo协议的接口测试

2023-06-05 11:58 作者:爱测软件测试  | 我要投稿

Dubbo

Dubbo 解决了各服务间通信问题,是分布式服务的解决方案之一。从单一应用架构到流动计算架构的演 变,分布式服务是重中之重。随着服务被拆分越来越细,服务间的信息交互是一大痛点。 Dubbo 通过 注册中心解决多服务间通讯的障碍,进行 Dubbo 测试的关键是:创建 Dubbo 的客户端(消费者),使 该客户端与服务端(生产者)正常通信。客户端通过断言函数执行结果,完成 Dubbo 服务测试。

节点角色说明

调用关系说明

  1. 服务容器负责启动,加载,运行服务提供者。

  2. 服务提供者在启动时,向注册中心注册自己提供的服务。

  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。

  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消 费者。

  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败, 再选另一台调用。

  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中 心。

把上述关系简化,其中服务提供者是 QA 需要测试的对象。QA 需要搭符合注册中心的服务消费者,利 用服务消费者对服务提供者进行测试:

问题关键在于如何搭建服务消费者,不同注册中心提供不同的搭建方案,比如:

  • Nacos 注册中心

  • Zookeeper 注册中心

  • Multicast 注册中心

  • Redis 注册中心

  • Simple 注册中心

有些公司内部会开发自己的注册中心,各位自行查找参考手册,下面以 Zookeeper 为例,简述配置过 程,详细内容可参考 Zookeeper 官网, Zookeeper 通过配置文件配置注册中心的信息,比如:

# The number of milliseconds of each tick 

tickTime=2000

# The number of ticks that the initial 

# synchronization phase can take 

initLimit=10 

# The number of ticks that can pass between 

# sending a request and getting an acknowledgement 

syncLimit=5 

# the directory where the snapshot is stored.

# do not use /tmp for storage, /tmp here is just 

# example sakes.

dataDir=/tmp/zookeeper 

# the port at which the clients will connect

clientPort=2181

通过终端启动Zookeeper注册中心服务,下面是在2181端口启动单节点注册中心服务的提示信息:

sudo ./zkServer.sh status

/usr/bin/java 

ZooKeeper JMX enabled by default

 Using config: /usr/local/zookeeper/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. 

Mode: standalone

服务消费者可以通过 Spring Boot 项目进行搭建,编写项目的 application.yaml 文件配置 Zookeeper 注 册中心服务地址即可完成订阅。以下是服务消费者样例, 通过 demoService.sayHello("mercyblitz") 调用服务提供者的函数,函数的返回结果即是调用结果:

@EnableAutoConfiguration 

public class DubboAutoConfigurationConsumerBootstrap {

private final Logger logger = LoggerFactory.getLogger(getClass()); @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:12345") private DemoService demoService; public static void main(String[] args) { SpringApplication.run(DubboAutoConfigurationConsumerBootstrap.class).close(); } @Bean public ApplicationRunner runner() { return args -> { logger.info(demoService.sayHello("mercyblitz")); }; } }

可以看出,上述 demoService.sayHello("mercyblitz") 的调用过程与一般接口测试无区别, QA 利用服务消费者访问服务提供者,配合 Junit 对执行结果进行断言即可。


软件测试 | Dubbo协议的接口测试的评论 (共 条)

分享到微博请遵守国家法律