微服务是什么、为什么、测哪些、怎么测

简介:微服务是一种云原生架构方法,其中单个应用程序由许多松散耦合且可独立部署的较小组件或服务组成。这些服务通常有自己的堆栈,包括数据库和数据模型;通过REST API,事件流和消息代理的组合相互通信;它们是按业务能力组织的,分隔服务的线通常称为有界上下文。

微服务的优势:简言之方便高扩展,方便部署

1、可以更轻松地更新代码。

2、团队可以为不同的组件使用不同的堆栈。

3、组件可以彼此独立地进行缩放,从而减少了因必须缩放整个应用程序而产生的浪费和成本,因为单个功能可能面临过多的负载。

怎么测:细分为测试的内容和测试的方法。

微服务的测试内容:

服务独立测试:功能、数据检查、性能
服务间一致性测试:正常场景、异常场景
系统集成测试:功能、数据检查、性能
服务幂等测试:一个服务、服务之间

测试微服务的方法:

1、间接测试:各服务提供虚拟的http接口,使服务测试转为http接口测试,

一般通过web框架进行转发。



2、直接测试:针对每个服务,专门写一个client进行访问测试,python中可以通过使用grpc的ProtoBuf来辅助生成client进行访问。

微服务搭建范例:

微服务框架nameko + sanic简单使用

间接测试范例:

对应的微服务:

from nameko.rpc import rpc
import datetime


class HelloService:
    name = "hello_service"

    @rpc
    def hello(self, name):
        return "hello.{}.{}".format(name, str(datetime.datetime.now())[:-7])

微服务封装转http接口:

from sanic import Sanic
from nameko.standalone.rpc import ClusterRpcProxy
from sanic.response import json

app = Sanic(__name__)

config_mq = {'AMQP_URI': "amqp://guest:guest@127.0.0.1"}


@app.get('/hello')
async def hello_world(request):
    with ClusterRpcProxy(config_mq) as rpc:
        data = request.args
        result = rpc.hello_service.hello(name=data["name"][0])
        return json({"code": 200, "result": result})


if __name__ == '__main__':
    app.run(host="127.0.0.1", port=3033, auto_reload=True)

微服务转http接口的单元测试用例:

# -*-coding:utf-8 -*-
import unittest
import requests
from logger import log


class TestHello(unittest.TestCase):

    def test_hello_cn(self):
        # 中文
        name = "冰墩墩"
        res = requests.get(url=f"http://127.0.0.1:3033/hello?name={name}")
        log.info(f"cn - res.text:{res.text}")
        self.assertIn(f"hello.{name}.", res.text)

    def test_hello_en(self):
        # 英文
        name = "lily"
        res = requests.get(url=f"http://127.0.0.1:3033/hello?name={name}")
        log.info(f"en - res.text:{res.text}")
        self.assertIn(f"hello.{name}.", res.text)

    def test_hello_punctuation(self):
        # 标点符号
        name = "$%^"
        res = requests.get(url=f"http://127.0.0.1:3033/hello?name={name}")
        log.info(f"punctuation - res.text:{res.text}")
        self.assertIn(f"hello.{name}.", res.text)


if __name__ == "__main__":
    unittest.main()

执行结果:

图片

图形化展示:

图片

如按范例Sanic:JSON - 中文ASCII,请参考:
https://www.jianshu.com/p/0369b4c74bba

这样我们通过测试http接口实现了对 hello_service 这个微服务的间接测试。post,put,delete接口同理。

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值