添加header信息
service -> client
service:
context.set_trailing_metadata((("x-envoy-decorator-operation", "test"), ("name", "donghao"), ("age", "23")))
client:
resp, call = client.HelloDonghao.with_call(pb2.HelloDonghaoReq(
name='donghao', age=23
))
print(resp.result)
headers = call.trailing_metadata()
print(dir(headers[0]))
print(headers[0].key, headers[0].value)
print(headers[1])
client -> service
client:
resp, call = client.HelloDonghao.with_call(pb2.HelloDonghaoReq(
name='donghao', age=23
), metadata=(("client_id", "andriod-alsdjfal"), ("platform", 'IOS')))
service:
headers = context.invocation_metadata()
print(headers)
print(headers[0].key, headers[0].value)
数据压缩与解压缩
class Compression(enum.IntEnum):
"""Indicates the compression method to be used for an RPC.
This enumeration is part of an EXPERIMENTAL API.
Attributes:
NoCompression: Do not use compression algorithm.
Deflate: Use "Deflate" compression algorithm.
Gzip: Use "Gzip" compression algorithm.
"""
NoCompression = _compression.NoCompression
Deflate = _compression.Deflate
Gzip = _compression.Gzip
1. 单个service压缩
service:
context.set_compression(grpc.Compression.Gzip) # or Deflate
2. 全局压缩
grpc_server = grpc.server(
futures.ThreadPoolExecutor(max_workers=4),
compression=grpc.Compression.Gzip
)
with_call 有哪些参数?
with_call(request, timeout=None, metadata=None, credentials=None, wait_for_ready=None, compression=None)
request,
timeout=None, # 超时时间
metadata=None, # 请求携带meta信息
credentials=None, # 证书相关
wait_for_ready=None, # 同步等到服务器有响应后再调用
compression=None # 数据压缩
发送数据大小限制
grpc_server = grpc.server(
futures.ThreadPoolExecutor(max_workers=1),
compression=grpc.Compression.Gzip,
options=[
("grpc.max_send_message_length", 50 * 1024 * 1024),
("grpc.max_receive_message_length", 50 * 1024 * 1024),
]
)