千家信息网

Nacos性能测试的示例分析

发表于:2024-10-10 作者:千家信息网编辑
千家信息网最后更新 2024年10月10日,小编给大家分享一下Nacos性能测试的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!背景故事基础架构部选择新的注册
千家信息网最后更新 2024年10月10日Nacos性能测试的示例分析

小编给大家分享一下Nacos性能测试的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

背景故事

基础架构部选择新的注册中心,测试组需要配合对业界成熟的注册中心产品做分析和比较。由于掌门教育采用的是比较纯净的 Spring Cloud 技术栈,所以我们需要围绕它的注册中心,从测试角度,进行功能和性能上研究。

Spring Cloud 技术栈官方支持 Netflix EurekaHashiCorp ConsulZookeeper 三个注册中心,它们可以相互间实现无缝迁移,Alibaba Nacos 是新加盟 Spring Cloud 技术栈的新成员。测试组的同学们对上述四个注册中心做了一一研究和分析,鉴于时间紧迫,除了 EurekaNacos 之外,其它两个中间件未做深入的功能测试和性能测试。下面提供来自阿里巴巴 Nacos 官方某次业界宣讲的资料截图以供大家参考:

  • Eureka 介绍

  • Zookeeper 介绍

  • Consul 介绍

  • 上述三个注册中心比较

本文将围绕 Alibaba Nacos 着重针对其功能测试和性能测试两方面进行剖析和介绍。

Nacos 测试篇

Nacos 性能测试

① Nacos Server 性能测试

开发部署了 UATNacos ,测试亲自压测。

  • 核心脚本

def registry(ip):    fo = open("service_name.txt", "r")    str = fo.read()    service_name_list = str.split(";")    service_name = service_name_list[random.randint(0,len(service_name_list) - 1)]    fo.close()    client = nacos.NacosClient(nacos_host, namespace='')    print(client.add_naming_instance(service_name,ip,333,"default",1.0,{'preserved.ip.delete.timeout':86400000},True,True))    while True:      print(client.send_heartbeat(service_name,ip,333,"default",1.0,"{}"))      time.sleep(5)
  • 压测数据

  • 压测结果图

Nacos Server 是3台 1C4G 集群,同时承受1499个服务和12715个实例注册,而且 CPU 和内存长期保持在一个合适的范围内,果真 Nacos 性能是相当 OK 的。

Nacos 功能测试

① Nacos Server 接口测试

更多更详 API 请参见 Nacos 官方文档: Open API 指南

https://nacos.io/zh-cn/docs/open-api.html

② Nacos Eureka Sync 测试
  • 交叉注册

网关,服务 A ,服务 B 各10台实例,网关注册 EurekaA 注册 NacosB 注册 Eureka ,同步正常,可调用。

  • 压力测试

请求大于100万次,查看 Sync Server 会不会受到影响,结果 ErrorRequest = 0,同步服务数和实例数没有变化。

  • 有无损调用

网关 Sync Server 挂掉,网关服务 Eureka 同步 Nacos 失败,不影响网关 -> A -> B 调用。

  • 自动创建同步

发布系统第一次发布应用到 Eureka / Nacos ,会自动创建 Eureka -> Nacos 的同步任务或 Nacos -> Eureka 的同步任务

  • 减少 Sync Server

Sync Server 4C8G ,停止机器,逐台递减,结论:平均1台 4C8G 机器最大可同步100个服务。

  • 增加 Sync Server

2台 Etcd 节点,停机一台,Etcd 读取超时,结论:600个服务至少2台 Etcd 节点,这里重点强调,新增服务时, Hash 算法虚拟节点数,务必和原有的保持一致,不然会出现同步失败,影响跨注册中心调用。

  • 重启 Sync Server

增加 Sync Server 个数,重启 Sync Server ,各节点同步数重新计算且均衡。

③ Nacos Client 功能测试

Nacos Client 界面重点测试集群管理,服务列表和权限控制。

  • Nacos Server 重启后,集群管理界面正常展示3台集群节点 IP

  • 服务注册 Nacos Server 后,服务列表新增注册上去的服务名和实例个数,而且可查看详情。

  • 服务上下线操作,健康状态和元数据等展示正常。

  • 编辑,删除等操作只有具备 Admin 权限的人员才可操作。

④ Nacos Client 自动化测试
  • 自动化测试链路

全链路测试路径

API网关 -> 服务A(两个实例) -> 服务B(两个实例)

全链路服务部署

  • 自动化测试入口

结合 Spring Boot JunitTestApplication.class 为测试框架内置应用启动程序, MyTestConfiguration 用于初始化所有测试用例类。在测试方法上面加入 JUnit@Test注解

@RunWith(SpringRunner.class)@SpringBootTest(classes = { TestApplication.class, MyTestConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class MyTest {    @Autowired    private MyTestCases myTestCases;    private static long startTime;    @BeforeClass    public static void beforeTest() {        startTime = System.currentTimeMillis();    }    @AfterClass    public static void afterTest() {        LOG.info("* Finished automation test in {} seconds", (System.currentTimeMillis() - startTime) / 1000);    }    @Test    public void testNoGray() throws Exception {        myTestCases.testNoGray(gatewayTestUrl);        myTestCases.testNoGray(zuulTestUrl);    }    @Test    public void testVersionStrategyGray() throws Exception {        myTestCases.testVersionStrategyGray1(gatewayGroup, gatewayServiceId, gatewayTestUrl);        myTestCases.testVersionStrategyGray1(zuulGroup, zuulServiceId, zuulTestUrl);    }}
@Configurationpublic class MyTestConfiguration {    @Bean    public MyTestCases myTestCases() {        return new MyTestCases();    }}
  • 基于 Nacos Client 的普通调用自动化测试

在测试方法上面增加注解 @DTest ,通过断言 Assert 来判断测试结果。注解 @DTest 内容如下:

@Target({ ElementType.METHOD, ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface DTest {}

代码如下:

public class MyTestCases {    @Autowired    private TestRestTemplate testRestTemplate;    @DTest    public void testNoGray(String testUrl) {        int noRepeatCount = 0;        List resultList = new ArrayList();        for (int i = 0; i < 4; i++) {            String result = testRestTemplate.getForEntity(testUrl, String.class).getBody();            LOG.info("Result{} : {}", i + 1, result);            if (!resultList.contains(result)) {                noRepeatCount++;            }            resultList.add(result);        }        Assert.assertEquals(noRepeatCount, 4);    }}
  • 基于 Nacos Client 的灰度蓝绿调用自动化测试

在测试方法上面增加注解 @DTestConfig ,通过断言 Assert 来判断测试结果。注解 DTestConfig 注解内容如下:

@Target({ ElementType.METHOD, ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface DTestConfig {    // 组名    String group();    // 服务名    String serviceId();    // 组名-服务名组合键值的前缀    String prefix() default StringUtils.EMPTY;    // 组名-服务名组合键值的后缀    String suffix() default StringUtils.EMPTY;    // 执行配置的文件路径。测试用例运行前,会把该文件里的内容推送到远程配置中心或者服务    String executePath();    // 重置配置的文件路径。测试用例运行后,会把该文件里的内容推送到远程配置中心或者服务。该文件内容是最初的默认配置    // 如果该注解属性为空,则直接删除从配置中心删除组名-服务名组合键值    String resetPath() default StringUtils.EMPTY;}

代码如下:

public class MyTestCases {    @Autowired    private TestRestTemplate testRestTemplate;    @DTestConfig(group = "#group", serviceId = "#serviceId", executePath = "gray-strategy-version.xml", resetPath = "gray-default.xml")    public void testVersionStrategyGray(String group, String serviceId, String testUrl) {        for (int i = 0; i < 4; i++) {            String result = testRestTemplate.getForEntity(testUrl, String.class).getBody();            LOG.info("Result{} : {}", i + 1, result);            int index = result.indexOf("[V=1.0]");            int lastIndex = result.lastIndexOf("[V=1.0]");            Assert.assertNotEquals(index, -1);            Assert.assertNotEquals(lastIndex, -1);            Assert.assertNotEquals(index, lastIndex);        }    }}

初始默认无灰度蓝绿的配置文件 gray-default.xml

灰度蓝绿生效的配置文件 gray-strategy-version.xml

            1.0    
  • 基于 Nacos Client 的自动化测试报告样例

---------- Run automation testcase :: testStrategyCustomizationGray() ----------Header : [a:"1", b:"2"]Result1 : zuul -> solar-service-a[192.168.0.107:3002][V=1.1][R=qa][G=solar-group] -> solar-service-b[192.168.0.107:4002][V=1.1][R=dev][G=solar-group]Result2 : zuul -> solar-service-a[192.168.0.107:3002][V=1.1][R=qa][G=solar-group] -> solar-service-b[192.168.0.107:4002][V=1.1][R=dev][G=solar-group]Result3 : zuul -> solar-service-a[192.168.0.107:3002][V=1.1][R=qa][G=solar-group] -> solar-service-b[192.168.0.107:4002][V=1.1][R=dev][G=solar-group]Result4 : zuul -> solar-service-a[192.168.0.107:3002][V=1.1][R=qa][G=solar-group] -> solar-service-b[192.168.0.107:4002][V=1.1][R=dev][G=solar-group]* Passed---------- Run automation testcase :: testVersionRuleGray() ----------Result1 : zuul -> solar-service-a[192.168.0.107:3002][V=1.1][R=qa][G=solar-group] -> solar-service-b[192.168.0.107:4002][V=1.1][R=dev][G=solar-group]Result2 : zuul -> solar-service-a[192.168.0.107:3001][V=1.0][R=dev][G=solar-group] -> solar-service-b[192.168.0.107:4001][V=1.0][R=qa][G=solar-group]Result3 : zuul -> solar-service-a[192.168.0.107:3002][V=1.1][R=qa][G=solar-group] -> solar-service-b[192.168.0.107:4002][V=1.1][R=dev][G=solar-group]Result4 : zuul -> solar-service-a[192.168.0.107:3001][V=1.0][R=dev][G=solar-group] -> solar-service-b[192.168.0.107:4001][V=1.0][R=qa][G=solar-group]* Passed

Nacos 测试总结

Nacos 不仅性能好,而且界面简洁,这样的注册中心你值得拥有。

以上是"Nacos性能测试的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0