Skip to main content

HTTP microservice package based on FastAPI and HTTPX

Project description

MFHM

基于HTTPXFastAPI的微服务包

注意: 该项目只是本人用于研究微服务原理而创建的探索项目, 请将本项目用于个人/研究为目的/无关紧要的项目中, 切勿在生产项目中使用

开始使用

1. 服务中心

MFHM的服务中心是整个微服务系统的信息交换核心, 在使用MFHM前必须创建一个服务中心

MFHM内置了一个服务中心, 要启动内置的服务中心非常简单:

  1. 创建一个目录作为服务中心源码目录(以 serviceCenter 为例)
  2. 在目录中创建一个python文件(以 serviceCenter.py 为例)
  3. mfhm.center 模块导入服务中心类 ServiceCenter
  4. 配置并创建服务中心实例
  5. 调用实例的 start() 方法启动服务中心
# serviceCenter/serviceCenter.py


from mfhm.center import ServiceCenter


sc = ServiceCenter(
    dataDir='./data'
)

sc.start()

ServiceCenter 接受以下参数用于实例化

class ServiceCenter(object):

    def __init__(
            self, 
            host:str = '0.0.0.0', 
            port:int = 21428,
            pingTestInterval:int = 10,
            pingTestTimeout:int = 5,
            cacheSaveInterval:int = 60,
            dataDir:str = None,
            cacheFilename:str = 'centerCache.json',
            logger = getLogger(os.path.basename(os.path.abspath(__file__))),
            asyncHttpClient = httpx.AsyncClient()
    ) -> None:
        pass
参数 默认值 说明
host 0.0.0.0 服务中心主机地址
port 21428 服务中心监听端口
pingTestInterval 10 ping测试时间间隔,服务中心会定时向各微服务发送一个ping测试用于检测服务是否存活
pingTestTimeout 5 ping测试的超时时间
cacheSaveInterval 60 缓存定时保存时间间隔,服务中心托管的服务信息都存储在内存中,为了防止服务中心在异常退出或者重启后丢失托管的服务数据,会定时将服务信息持久化到磁盘
dataDir None 数据存储目录,
cacheFilename centerCache.json 缓存文件名称
logger mfhm.log.Logger 日志对象,mfhm使用python标准库 logging 作为日志记录工具,该参数值应该是标准的 logging.Logger 类/子类实例
asyncHttpClient httpx.AsyncClient HTTPX的异步HTTP客户端

运行 serviceCenter.py

python serviceCenter.py

你将会看到以下类似的输出, 这代表服务中心启动成功

2022-05-08 22:26:02,494 | INFO     | Started server process [16752]
2022-05-08 22:26:02,494 | INFO     | Waiting for application startup.
2022-05-08 22:26:02,495 | INFO     | Application startup complete.
2022-05-08 22:26:02,496 | INFO     | Uvicorn running on http://0.0.0.0:21428 (Press CTRL+C to quit) 

2. 创建服务

在接下来的例子中将会创建两个微服务 service01service02

2.1 service01

2.1.1 创建目录 service01

2.1.2 在 service01 目录下创建 service01.py

2.1.3 从 mfhm.service 导入 Service

2.1.4 配置 Service 并实例化

2.1.5 使用实例创建一个测试接口

  • request:
    • meghod: GET
    • path: /hello
  • response:
    • json
# service01/service01.py


from mfhm.service import Service


# serviceName: 当前服务的服务名称
# centerHost: 服务中心主机地址
# centerPort: 服务中心监听端口
s = Service(
    serviceName='service01',
    centerHost='127.0.0.1',
    centerPort=21428
)


@s.get('/hello')
async def hello():
    return {'status': True, 'code': '0000', 'message': 'hello, my name is service01'}


s.start()

2.1.6 执行 service01.py 启动服务(服务默认监听30176端口), 你将会看到类似以下的输出, 这代表服务启动成功

2022-05-08 23:00:46,669 | INFO     | Started server process [11372]
2022-05-08 23:00:46,669 | INFO     | Waiting for application startup.
2022-05-08 23:00:46,669 | INFO     | Application startup complete.
2022-05-08 23:00:46,670 | INFO     | Uvicorn running on http://0.0.0.0:30176 (Press CTRL+C to quit)

2.2 service02

重复 service01 服务的创建步骤创建 service02 服务

注意:由于例子中的所有的服务都在同一个系统中创建, 因此创建 service02 服务时需要修改服务的监听端口, 不能与 service01 服务监听的端口冲突, 否则启动 service02 时你将会得到类似以下的报错

2022-05-08 23:04:26,387 | INFO     | Started server process [5220]
2022-05-08 23:04:26,388 | INFO     | Waiting for application startup.
2022-05-08 23:04:26,388 | INFO     | Application startup complete.
2022-05-08 23:04:26,428 | ERROR    | [Errno 10048] error while attempting to bind on address ('0.0.0.0', 30176): 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
2022-05-08 23:04:26,429 | INFO     | Waiting for application shutdown.
2022-05-08 23:04:26,433 | INFO     | Application shutdown complete.
# service02/service02.py


from mfhm.service import Service


s = Service(
    serviceName='service02',
    servicePort=30177,
    centerHost='127.0.0.1',
    centerPort=21428
)


@s.get('/hello')
async def hello():
    return {'status': True, 'code': '0000', 'message': 'hello, my name is service01'}


s.start()

3. 服务调用

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mfhm-0.0.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

mfhm-0.0.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file mfhm-0.0.2.tar.gz.

File metadata

  • Download URL: mfhm-0.0.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for mfhm-0.0.2.tar.gz
Algorithm Hash digest
SHA256 fe106b81eab7ef1c9720e616404d7e1943c2229f8e8024a316eac3fa5b75ca40
MD5 0d8a9954515325b6e689edc3988f3ef9
BLAKE2b-256 49799fbc3b0d9c9b4b0f9b1eaa759f10f341174c720985830e2ad9a37243b97f

See more details on using hashes here.

File details

Details for the file mfhm-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: mfhm-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for mfhm-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e60a2b77bc1465824229e2b4e3446882543efd41cba97450b7a910b48e4d49c9
MD5 431b8c6ad3d8ee783b5e539d407b26ae
BLAKE2b-256 61c322a67ffe5c64439e826707ca80d2a7421c3a3b216902d18431a6d0c0d5b2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page