sparrow cloud 组件介绍
Django SDK
- RestClient : 封装了request包和服务发现,正确请求返回解析后json数据, 错误请求返回HTTPException
- RequestsClient : 封装了request包和服务发现, 返回原生的request结果
- Message_Client : 将任务发送到rabbitmq, server端未开源
- Rabbitmq_Consumer : rabbitmq消息消费端,server端未开源
- Table_API : 接收查询条件返回 django model 序列化后的数据
- Api Schema Register : django subcommand, 主动注册API 描述到文档服务, server端未开源
- access_control verify : 访问控制验证,服务端未开源
- get_user_token : 获取用户token
- get_app_token : 获取app token
- ly_msg : 发送app 消息
- distributed_lock : 添加分布式锁或移除锁
Django Middleware
- JWTMiddleware : 解析 JWT Token
- MethodConvertMiddleware : 兼容不支持 put/delete 请求
- ExceptionMiddleware : 异常通知
- TracingMiddleware : 追踪链
- CheckLockMiddleware : 前端防重复提交锁中间件
rest_framework 中间件
- UserID Authentication: 验证 user
sparrow cloud组件
RestClient
RequestsClient
Message_Client
Rabbitmq_Consumer
Table_API
Api Schema Register
access_control_verify
access_control_register
get_user_token
get_app_token
app_message
distributed_lock
LogFilter
log_filter
django中间件
JWTMiddleware
MethodConvertMiddleware
ExceptionMiddleware
TracingMiddleware
CheckLockMiddleware
LogMiddleware
rest_framework中间件
UserID Authentication
installation
pip3.11 install sparrowcloud
测试运行
运行所有测试:
source tests/mock_configmap.sh && python3.11 -m unittest tests/test* access_control/test*
运行单个测试:
source tests/mock_configmap.sh && python3.11 -m unittest tests/test_rest_client.py
运行环境
sparrowcloud 5.x.x 基于django4.2.x, python3.11
sparrowcloud 4.x.x 基于django3.2.x, python3.7
JWTMiddleware
描述:Token 解析
注意:配置SC_JWT_PUBLIC_KEY环境变量,rsa公钥文件数据
配置 JWTMiddleware 中间件需要的参数
MIDDLEWARE = (
'sparrow_cloud.middleware.jwt_middleware.JWTMiddleware',
UserIDAuthentication
描述: user_id 解析
配置 UserIDAuthentication 认证需要的参数(仅兼容django2.2以上版本)
SPARROW_AUTHENTICATION = {
"USER_CLASS_PATH": "sparrow_cloud.auth.user.User",
}
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'sparrow_cloud.auth.user_id_authentication.UserIDAuthentication',
),
}
METHOD_MIDDLEWARE
兼容阿里不支持 put/delete 请求
配置METHOD_MIDDLEWARE需要的参数
MIDDLEWARE_CLASSES = (
'sparrow_cloud.middleware.methodconvert.MethodConvertMiddleware',
)
TracingMiddleware
兼容阿里不支持 put/delete 请求
配置METHOD_MIDDLEWARE需要的参数
MIDDLEWARE_CLASSES = (
'sparrow_cloud.middleware.tracing_middleware.TracingMiddleware',
)
CheckLockMiddleware
防前端重复提交中间件,需要前端配合使用
对请求header中的特定键值判断其状态,来决定是否阻止该次请求
MIDDLEWARE_CLASSES = (
'sparrow_cloud.middleware.lock_middleware.CheckLockMiddleware',
)
restclient
微服务间调用,使用示例
from sparrow_cloud.restclient import rest_client
rest_client.post(service_address, api_path, timeout=30, token=None, json=api_list)
参数说明
service_address = "test-svc:8000"
timeout:
非必传,默认超时时间30秒
传参方式:
timeout=30 # 30秒为connect 和 read 的 timeout
timeout=(5, 5) # 分别定制:connect 和 read 的 timeout
timeout=None # Request 永远等待
token:
内部跨服务调用认证token
from sparrow_cloud.authorization.token import get_app_token, get_user_token
token = get_user_token(user_id="21424kvjbcdjslafds")
或者
token = get_app_token()
其它:
剩余参数与 requests.get/post 等方法保持一致
异常返回
当服务间调用异常时,抛出异常 HTTPException
requestsclient
服务调用(返回结果未封装)
from sparrow_cloud.restclient import requests_client
requests_client.post(service_address, api_path, timeout=30, token=None, json=api_list)
参数说明
service_address = "test-svc:8000"
timeout:
非必传,默认超时时间30秒
传参方式:
timeout=30 # 30秒为connect 和 read 的 timeout
timeout=(5, 5) # 分别定制:connect 和 read 的 timeout
timeout=None # Request 永远等待
token:
内部跨服务调用认证token
from sparrow_cloud.authorization.token import get_app_token, get_user_token
token = get_user_token(user_id="21424kvjbcdjslafds")
或者
token = get_app_token()
其它:
剩余参数与 requests.get/post 等方法保持一致
message_client
麻雀任务发送
- 注册消息 2. 发送消息
from sparrow_cloud.message_service.sender import send_task_v3
data = send_task_v3(message_code=message_code,
*args,
**kwargs)
data = send_task_v3(
message_code=message_code,
delay_time=200,
*args,
**kwargs)
=====================以下为旧版调用方式,不建议继续使用,会逐步弃用===========================
from sparrow_cloud.message_service.sender import send_task
data = send_task(
exchange=exchange,
routing_key=routing_key,
message_code=message_code,
retry_times=3,
*args,
**kwargs)
data = send_task(
exchange=exchange,
routing_key=routing_key,
message_code=message_code,
retry_times=3,
delay=True,
delay_time=200,
*args,
**kwargs)
rabbitmq_consumer
麻雀任务消费
- 获取队列 2. 消费任务
INSTALLED_APPS = [
"sparrow_cloud.apps.message_service",
]
QUEUE_CONF_1 = {
"QUEUE": "",
"TARGET_FUNC_MAP": {
"message_code": "path",
},
}
python3.11 manage.py rabbitmq_consumer --queue QUEUE_CONF_1
table_api
接受查询条件返回django model 序列化后的数据
分为server端和client端
INSTALLED_APPS = [
"sparrow_cloud.apps.table_api",
]
urlpatterns = [
path('table/api/', include("sparrow_cloud.apps.table_api.urls")),
]
from sparrow_cloud.restclient import rest_client
service_address = "sparrow-demo:8000"
payload = {
"app_lable_model":"app_lable.model",
"filter_condition":{"product_id":"74101"}
}
response = rest_client.get(service_address, api_path='/table/api/', json=payload)
API SCHEMA REGISTER
描述:主动注册API 描述到文档服务 配置schema_command 需要的参数
INSTALLED_APPS = [
"sparrow_cloud.apps.schema_command",
]
SERVICE_CONF = {
"NAME": "",
"SECRET": ""
}
python3 manage.py register_api_schema
接口描述代码示例
from rest_framework.decorators import api_view
from rest_framework.generics import RetrieveUpdateDestroyAPIView
from rest_framework import generics
from rest_framework.viewsets import ModelViewSet
@api_view(('GET',))
def get_user(request):
"""
### 获取用户信息 ####
请求参数 id, 用户id
返回
{
"user_id":"1", # 用户ID
"user_name":"Tom" # 用户名称
}
"""
class UserApiView(RetrieveUpdateDestroyAPIView, generics.GenericAPIView):
"""
get:
### 查询用户信息 ###
请求参数 id, 用户id
返回
{
"id":"1", # 用户ID
"user_name":"Tom" # 用户名称
}
delete:
### 删除用户 ###
路径参数
id 用户id
返回
404 用户id不存在
204 删除成功
"""
def put(self, request, *args, **kwargs):
"""
### 覆盖修改用户 ###
请求参数
{
"id":"1", # 用户ID
"user_name":"Tom" # 用户名称
}
返回 200 修改成功
"""
return super(UserApiView, self).put(self, request, *args, **kwargs)
class CarViewSet(ModelViewSet):
"""
list: 分页查询车辆
retrieve:获取车辆信息
update: 覆盖修改车辆
partial_update: 部分修改车辆
create: 创建车辆
destroy: 删除车辆
"""
ExceptionMiddleware
中间件
说明: 可自动捕获程序异常,并将详细错误信息发送到飞书
MIDDLEWARE = [
"sparrow_cloud.middleware.exception.ExceptionMiddleware"
]
LogMiddleware
日志中间件
日志中自动打印请求的request_id。日志的request_id与链路追踪中的request_id一致,可结合链路追踪中搜索快速定位异常请求信息。
此中间件需要配合log_filter 使用,才能在每条业务逻辑日志中插入request_id, 如单独使用只能透request_id
MIDDLEWARE = [
"sparrow_cloud.middleware.log_middleware.RequestIDMiddleware"
]
log_filter
log_filter(自定义logfilter,从线程中获取request_id,从每条日志中打印出来)
需要与LogMiddleware 中间件配合使用,否则不生效
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'request_id': {
'()': 'sparrow_cloud.filter.log_filters.RequestIDFilter'
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'standard',
'filters': ['request_id'],
},
'access_control': {
'class': 'logging.handlers.RotatingFileHandler',
'filters': ['request_id'],
'level': 'DEBUG',
'formatter': 'standard',
'filename': os.path.join(os.path.sep, BASE_DIR_LOG, 'access_control.log'),
'mode': 'a',
'maxBytes': 1024 * 1024 * 5,
'backupCount': 1,
},
},
'formatters': {
'standard': {
'format': '%(levelname)s %(asctime)s %(request_id)s %(lineno)d %(name)s %(module)s %(funcName)s %(process)d %(thread)d %(message)s'
},
},
'sparrow_cloud': {
'handlers': ['console', 'sparrow_cloud'],
'level': 'DEBUG',
'propagate': False,
},
'access_control': {
'handlers': ['console', 'access_control'],
'level': 'DEBUG',
'propagate': False,
}
}
}
ACCESS_CONTROL_VERIFY
access_control_verify decorators (访问控制验证)
SERVICE_CONF = {
"NAME": "",
"SECRET": "",
}
from sparrow_cloud.access_control.decorators import access_control_fbv
@api_view(('POST', 'GET', 'PUT', 'DELETE'))
@access_control_fbv("permission_example1")
def test(request, *args, **kwargs):
return Response({"message": "ok"}, status=status.HTTP_200_OK)
from sparrow_cloud.access_control.decorators import access_control_cbv_all
@access_control_cbv_all("permission_example1")
class ProductOperationList(generics.ListCreateAPIView):
"""请求方法:GET/POST"""
pass
from sparrow_cloud.access_control.decorators import access_control_cbv_method
RESOURCE = {
"post": "permission_example1",
"get": "permission_example2"
}
@access_control_cbv_method(RESOURCE)
class ProductOperationList(generics.ListCreateAPIView):
"""请求方法:GET/POST"""
pass
get_user_token
get_user_token (获取用户token)
from sparrow_cloud.authorization.token import get_user_token
user_token = get_user_token(user_id="21424kvjbcdjslafds")
get_app_token
get_app_token (获取服务token)
from sparrow_cloud.authorization.token import get_app_token
app_token = get_app_token()
app_message
app_message (发送消息到揽月app, 服务端未开源)
from sparrow_cloud.app_message.sender import send_message
msg_data = "http://www.test.com/image.png"
res = send_message(msg_data=msg_data, code_type="test", content_type="image", msg_sender="麻雀")
from sparrow_cloud.app_message.sender import send_message
msg_data = "文本消息"
res = send_message(msg_data=msg_data, code_type="test", nickname="文本消息携带字段")
from sparrow_cloud.app_message.sender import send_message
msg_data = "卡片消息内容"
res = send_message(msg_data=msg_data, code_type="test", content_type="card_text", title="通知")
distributed_lock
add_lock (添加锁)
remove_lock (移除锁)
from sparrow_cloud.distributed_lock.lock_op import add_lock, remove_lock
res = add_lock("lock_key", 100)
if res.get("code") != 0:
res = remove_lock("lock_key")
if res.get("code") != 0:
Stargazers over time