GRPC Protobuf framework introduction and use tutorial in the Java class library
GRPC Protobuf framework introduction and use tutorial
Introduction to GRPC Protobuf framework
GRPC is a high -performance, open source, universal RPC framework, using Protocol Buffers (Protobuf) as the interface definition language.It supports a variety of programming languages and can work in any environment.GRPC uses IDL (Interface Description Language) to define services, and then automatically generate clients and server code via GRPC plug -in.It provides powerful functions such as two -way flow, streaming processing, head compression, and authentication and authorization.
Protocol Buffers (Protobuf) is a serialized data format that has nothing to do with language, platform irrelevant, and scalable.It is widely used in the fields of communication protocols, data storage, etc., and has the characteristics of high efficiency, simplicity and readability.Protobuf uses the .proto file to define the data structure and service interface, and generate the corresponding class through the compiler.
Second, GRPC Protobuf Framework Tutorial
The following will introduce how to use the GRPC Protobuf framework for development.Suppose we want to achieve a simple service, including clients and server.
1. Install GRPC and PROTOBUF compiler
First, you need to install the GRPC framework and the protobuf compiler.You can install it through the following command:
bash
$ pip install grpcio
$ pip install grpcio-tools
2. Define service interface
Before defining the service interface, you need to create a .proto file to describe the service interface and data structure.For example, we create a file called "Example.proto" and define a simple service interface and message type:
protobuf
syntax = "proto3";
message Request {
string message = 1;
}
message Response {
string message = 1;
}
service ExampleService {
rpc SayHello (Request) returns (Response);
}
3. Generate code
The corresponding code is generated through the Protobuf compiler to implement the implementation of the client and server.Execute the following commands in the command line:
bash
$ python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. example.proto
After the command is executed, the Example_pb2.py and Example_PB2_GRPC.PY files are generated. Among them, the exmple_pb2.py contains the definition of message type. Example_pb2_grpc.py contains the definition of service interface and related categories.
4. Server end implementation
In the server code, the method of service interface definition needs to be realized.First import the generated Example_PB2_GRPC and Example_PB2 modules, and then write the server -side code:
python
import grpc
from concurrent import futures
import example_pb2
import example_pb2_grpc
class ExampleService(example_pb2_grpc.ExampleServiceServicer):
def SayHello(self, request, context):
response = example_pb2.Response()
response.message = "Hello " + request.message
return response
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
example_pb2_grpc.add_ExampleServiceServicer_to_server(ExampleService(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
if __name__ == '__main__':
serve()
5. Client implementation
In the client code, you need to create a connection with the server and call the service method through the connection.First import generated Example_PB2_GRPC and Example_PB2 modules, and then write client code:
python
import grpc
import example_pb2
import example_pb2_grpc
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = example_pb2_grpc.ExampleServiceStub(channel)
request = example_pb2.Request()
request.message = "World"
response = stub.SayHello(request)
print("Response: " + response.message)
if __name__ == '__main__':
run()
6. Run and test
Run the server and client code in the terminal, and then observe the output result.
bash
$ python server.py
$ python client.py
Response: Hello World
The above is a simple tutorial for development using the GRPC Protobuf framework.Through GRPC, we can easily define service interfaces, generate clients and server code, and conduct cross -language communication.
Supplementary instructions: This article provides a basic introduction and use tutorial for the GRPC Protobuf framework, which involved in the code and configuration files involved in the Python environment.If you need to use GRPC in other programming languages, you need to perform corresponding configuration and coding according to the corresponding language and framework.