DIZNR INTERNATIONAL

Remote Procedure Call-RPC define procedure java rpc-Remote Procedure Call in Distributed System

Remote Procedure Call-RPC define procedure java rpc-Remote Procedure Call in Distributed System

Remote Procedure Call-RPC define procedure java rpc-Remote Procedure Call in Distributed System

Remote Procedure Call-RPC define procedure java rpc-Remote Procedure Call in Distributed System.

https://www.gyanodhan.com/video/7A2.%20Computer%20Science/Distributed%20Computing/310.%20Day%2004%20Part%2009%20Remote%20Procedure%20Call%20%20RPCdefine%20procedurejava%20rpc.mp4

Remote Procedure Call (RPC) in Java & Distributed Systems

What is Remote Procedure Call (RPC)?

Remote Procedure Call (RPC) is a protocol that allows a program to execute a procedure (function) on a remote server as if it were a local function call. It is widely used in distributed systems for communication between different machines over a network.

1. How Does RPC Work?

RPC follows these steps:
Client makes a request to invoke a function on a remote server.
 The request is converted into a network message and sent.
 The server receives the request and executes the function.
 The result is sent back to the client over the network.
 The client receives the result and continues execution.

Key Components of RPC:
Client & Server: The client calls the remote function, and the server executes it.
Stub: Acts as an interface between the local and remote procedure calls.
Marshalling & Unmarshalling: Converts data into a format suitable for transmission.
Transport Protocol: RPC can use TCP, UDP, or HTTP for communication.

2. RPC in Java – Implementing Java RMI (Remote Method Invocation)

In Java, RPC is implemented using Java RMI (Remote Method Invocation). Java RMI allows an object to call methods of a remote object running on another JVM.

Step-by-Step Java RMI Example

Step 1: Define a Remote Interface

import java.rmi.Remote;
import java.rmi.RemoteException;

// Remote Interface
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}

Step 2: Implement the Remote Interface

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class HelloImpl extends UnicastRemoteObject implements Hello {
protected HelloImpl() throws RemoteException {
super();
}

public String sayHello() throws RemoteException {
return "Hello from Remote Server!";
}
}

Step 3: Create and Start the RMI Server

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
public static void main(String[] args) {
try {
HelloImpl obj = new HelloImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("Hello", obj);
System.out.println("Server is running...");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Step 4: Create an RMI Client

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
Hello stub = (Hello) registry.lookup("Hello");
System.out.println(stub.sayHello());
} catch (Exception e) {
e.printStackTrace();
}
}
}

3. Advantages of RPC in Distributed Systems

Simplicity: Makes remote method calls look like local function calls.
Platform Independence: Works across different operating systems.
Abstraction: Hides the complexity of network communication.
Flexibility: Supports different transport protocols (TCP, HTTP, etc.).

4. Alternatives to RPC

gRPC (Google RPC): Uses Protocol Buffers (protobuf) for efficient communication.
REST APIs: HTTP-based communication for distributed services.
SOAP (Simple Object Access Protocol): XML-based messaging protocol.

 Conclusion

Remote Procedure Call (RPC) allows seamless communication in distributed systems by enabling a client to execute methods on a remote server. Java RMI (Remote Method Invocation) is one of the simplest implementations of RPC. Modern alternatives like gRPC and REST APIs provide more advanced and scalable solutions.

Would you like a detailed guide on gRPC in Java?