0% found this document useful (0 votes)
4 views6 pages

Project Ds

The document explains the process of setting up a Remote Procedure Call (RPC) application using gRPC, detailing the client-server architecture, the use of .proto files for service definition, and the generation of client and server code. It provides example code for both the server and client implementations in Python, demonstrating how to handle RPC calls and responses. The document concludes with instructions for running the server and client to test the application.

Uploaded by

robaa3874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Project Ds

The document explains the process of setting up a Remote Procedure Call (RPC) application using gRPC, detailing the client-server architecture, the use of .proto files for service definition, and the generation of client and server code. It provides example code for both the server and client implementations in Python, demonstrating how to handle RPC calls and responses. The document concludes with instructions for running the server and client to test the application.

Uploaded by

robaa3874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Step 1: Understand the Concept

What is RPC?

Remote Procedure Call (RPC) is a communication protocol that allows a program to call procedures or
methods on another program or computer over a network. It enables distributed computing by allowing
different systems to communicate with each other seamlessly.

How Does RPC Work?

 Client-Server Architecture: The client initiates a request to the server.

 Stubs: Client and server stubs handle the communication. The client stub converts the request
into a network message, and the server stub receives this message and invokes the actual
procedure.

 Response: The server stub sends the result back to the client stub, which then returns it to the
client.

Step 2: Define the Service

Using .proto Files

Create a .proto file to define the RPC service and message structure using Protocol Buffers.

Example .proto File:

text

syntax = "proto3";

service Calculator {

rpc Add (AddRequest) returns (AddResponse);

}
message AddRequest {

int32 num1 = 1;

int32 num2 = 2;

message AddResponse {

int32 result = 1;

 syntax = "proto3";: Specifies the version of Protocol Buffers syntax.

 service Calculator { ... }: Defines a service named Calculator.

 rpc Add (AddRequest) returns (AddResponse);: Declares a method named Add that takes an
AddRequest message and returns an AddResponse message.

 message AddRequest { ... }: Defines a message type for the request.

 message AddResponse { ... }: Defines a message type for the response.

Step 3: Generate Code

Using the protoc Compiler

After defining your service in a .proto file, use the protoc compiler to generate client and server code in
your preferred language.

Example Command:
bash

protoc --proto_path=. --python_out=. --grpc_python_out=. calculator.proto

 --proto_path=.: Specifies the directory where the .proto file is located.

 --python_out=.: Generates Python code for the messages.

 --grpc_python_out=.: Generates Python code for the gRPC service.

 calculator.proto: The name of your .proto file.

Generated Files:

 calculator_pb2.py: Contains the generated message classes.

 calculator_pb2_grpc.py: Contains the generated service classes.

Step 4: Implement the Server

Server Implementation:

Create a server class that implements the service methods defined in your .proto file.

Example Server Code (Python):

python

import grpc

from concurrent import futures

import calculator_pb2

import calculator_pb2_grpc
class CalculatorService(calculator_pb2_grpc.CalculatorServicer):

def Add(self, request, context):

result = request.num1 + request.num2

return calculator_pb2.AddResponse(result=result)

def serve():

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

calculator_pb2_grpc.add_CalculatorServicer_to_server(CalculatorService(), server)

server.add_insecure_port("[::]:50051")

server.start()

print("Server running on port 50051")

server.wait_for_termination()

if __name__ == "__main__":

serve()

 CalculatorService Class: Implements the CalculatorServicer interface.

 Add Method: Handles the Add RPC call by adding two numbers.

 serve Function: Sets up and starts the gRPC server.

Step 5: Implement the Client

Client Implementation:

Write client code to call the server's RPC method.


Example Client Code (Python):

python

import grpc

import calculator_pb2

import calculator_pb2_grpc

def run():

with grpc.insecure_channel("localhost:50051") as channel:

stub = calculator_pb2_grpc.CalculatorStub(channel)

response = stub.Add(calculator_pb2.AddRequest(num1=5, num2=3))

print(f"Result: {response.result}")

if __name__ == "__main__":

run()

 grpc.insecure_channel: Establishes a connection to the server.

 CalculatorStub: Provides methods to call the server's RPCs.

 Add Call: Sends a request to the server and prints the result.

Step 6: Run the Application

Start the Server:

Run the server script to start listening for incoming requests.


Run the Client:

Execute the client script to send a request to the server and display the response.

This detailed explanation covers the process of setting up a basic RPC application using gRPC, from
defining the service to running the client and server.

You might also like