RPC:
gRPC:
RPC:
gRPC:
RPC:
gRPC:
RPC:
gRPC:
RPC:
gRPC:
在 gRPC 中,支持四种不同的 RPC 模式:单向 RPC、服务器流式 RPC、客户端流式 RPC 和双向流式 RPC。每种模式适用于不同的应用场景。以下是每种模式的详细说明:
客户端发送一个单独的请求到服务器并等待响应。这是最常见的 RPC 模式,类似于普通的函数调用。
客户端发送一个请求来获取用户信息,服务器返回该用户的信息。
- ervice UserService
- { rpc GetUser (UserRequest) returns (UserResponse) {} }
- message UserRequest { int32 user_id = 1; }
- message UserResponse { string name = 1; int32 age = 2; }
客户端发送一个请求到服务器,服务器返回一个流式响应。客户端可以读取从服务器返回的多个消息,直到流结束。
客户端请求一个用户的所有订单,服务器以流的形式返回订单信息。
- service OrderService
- {
- rpc ListOrders (OrderRequest) returns (stream OrderResponse) {}
- }
- message OrderRequest { int32 user_id = 1; }
- message OrderResponse
- { int32 order_id = 1;
- string product_name = 2;
- double price = 3; }
客户端发送一个流式请求到服务器,服务器在接收到所有请求消息后返回一个响应。
客户端以流的形式发送多个传感器数据点,服务器处理后返回处理结果
service SensorService { rpc RecordMetrics (stream MetricRequest) returns (MetricResponse) {} } message MetricRequest { double value = 1; string timestamp = 2; } message MetricResponse { string status = 1; }
客户端和服务器都可以发送和接收多个消息。消息在流中按顺序发送,客户端和服务器可以随时读写消息。
客户端和服务器之间的实时聊天。
- proto 文件定义
- service ChatService { rpc Chat (stream ChatMessage)
- returns (stream ChatMessage) {} }
- message ChatMessage { string user = 1; string message = 2; string timestamp = 3; }
以 Python 为例,这里是实现上述模式的一些基本代码:
服务端代码:
- rviceServicer(user_pb2_grpc.UserServiceServicer):
- def GetUser(self, request, context):
- response = user_pb2.UserResponse(name="John Doe", age=30)
- return response
客户端代码:
with grpc.insecure_channel('localhost:50051') as channel: stub = user_pb2_grpc.UserServiceStub(channel) response = stub.GetUser(user_pb2.UserRequest(user_id=1)) print(response)
服务端代码:
class OrderServiceServicer(order_pb2_grpc.OrderServiceServicer): def ListOrders(self, request, context): orders = [order_pb2.OrderResponse(order_id=1, product_name="Product1", price=100.0), order_pb2.OrderResponse(order_id=2, product_name="Product2", price=150.0)] for order in orders: yield order
客户端代码:
with grpc.insecure_channel('localhost:50051') as channel: stub = order_pb2_grpc.OrderServiceStub(channel) responses = stub.ListOrders(order_pb2.OrderRequest(user_id=1)) for response in responses: print(response)
服务端代码:
- class SensorServiceServicer(sensor_pb2_grpc.SensorServiceServicer):
- def RecordMetrics(self, request_iterator, context):
- for request in request_iterator: print(f"Received metric:
- {request.value} at {request.timestamp}")
- return sensor_pb2.MetricResponse(status="Metrics Recorded")
客户端代码:
with grpc.insecure_channel('localhost:50051') as channel: stub = sensor_pb2_grpc.SensorServiceStub(channel) requests = (sensor_pb2.MetricRequest(value=i, timestamp=str(i)) for i in range(5)) response = stub.RecordMetrics(requests) print(response)
服务端代码:
class ChatServiceServicer(chat_pb2_grpc.ChatServiceServicer): def Chat(self, request_iterator, context): for chat_message in request_iterator: print(f"Received message from {chat_message.user}: {chat_message.message}") yield chat_pb2.ChatMessage(user="Server", message="Hello " + chat_message.user, timestamp="now")
客户端代码:
- with grpc.insecure_channel('localhost:50051')
- as channel: stub = chat_pb2_grpc.ChatServiceStub(channel)
- def generate_messages(): for i in range(5):
- yield chat_pb2.ChatMessage(user="Client", message=f"Message {i}",
- timestamp=str(i)) responses = stub.Chat(generate_messages())
- for response in responses: print(response)
头部压缩(Header Compression)是 HTTP/2 引入的一项技术,用于减少 HTTP 请求和响应头部的大小,从而提高网络传输的效率和性能。在 HTTP/1.x 中,每个请求和相应地都会承载完整的头部信息,这些头部信息往往包含大量的波形数据。头部压缩通过减少波形和优化传输效率,显着提升了性能。
HTTP/2 使用了一种名为 HPACK 的头部压缩算法,主要包含以下两个部分:
静态表(Static Table):
动态表(Dynamic Table):