Introduction to the common RPC framework in Java

Source: Internet
Author: User
Tags object serialization zookeeper

Name: Deng Yong class: Software 151

RPC is the short name of remote procedure call, widely used in large-scale distributed applications, the role is to help the vertical splitting of the system, making the system more easily expanded. There are many RPC frameworks in Java, each has its own features and is widely used in RMI, Hessian, Dubbo and so on. RPC also has a feature that can be cross-language, this article only in the Java language RPC as an example. Other framework structures are similar, with the distinction between object serialization methods, communication protocols for transmitting objects, and registry management and failover design (using zookeeper).
Clients and servers can run in different JVMs, the client only need to introduce the interface, the implementation of the interface and the data required by the runtime are on the server side, the main technology of RPC depends on the serialization, deserialization and transport protocol, the corresponding object in Java is the serialization, Deserialization and the transfer of data after serialization. RMI serialization and deserialization is the Java, Hessian serialization and deserialization is private, the transport protocol is HTTP,DUBBO serialization can be a variety of options, generally use Hessian serialization protocol, transmission is the TCP protocol, A high-performance NiO frame Netty is used. For serialization, I also know some, like Google's Probuffer, JBoss marshalling and Apache thrift, etc., before writing a post about Probuffer
1. RMI (remote method invocation)
Java comes with the remote method call tool, but there are some limitations, after all, is the Java language at the beginning of the design, and then many of the principles of the framework are based on the use of Rmi,rmi as follows:
External interface
<span style= "FONT-SIZE:12PX;" >public interface IService extends Remote {

public string QueryName (string no) throws remoteexception;

}</span>
Service implementation
Import java.rmi.RemoteException;
Import Java.rmi.server.UnicastRemoteObject;

Service implementation
public class Serviceimpl extends UnicastRemoteObject implements IService {

/**
*/
Private static final long serialversionuid = 682805210518738166L;

/**
* @throws RemoteException
*/
Protected Serviceimpl () throws RemoteException {
Super ();
}

/* (Non-javadoc)
* @see Com.suning.ebuy.wd.web.iservice#queryname (java.lang.String)
*/
@Override
public string QueryName (string no) throws RemoteException {
The concrete implementation of the method
System.out.println ("Hello" + No);
Return string.valueof (System.currenttimemillis ());
}

} RMI Client
Import java.rmi.AccessException;
Import java.rmi.NotBoundException;
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
Import Java.rmi.registry.Registry;

RMI Client
public class Client {

public static void Main (string[] args) {
Registration Manager
Registry Registry = null;
try {
Get Service Registry Manager
Registry = Locateregistry.getregistry ("127.0.0.1", 8088);
List all registered Services
string[] list = Registry.list ();
for (String s:list) {
System.out.println (s);
}
} catch (RemoteException e) {

}
try {
Get Service by name
IService Server = (iservice) registry.lookup ("Vince");
Calling a remote method
String result = Server.queryname ("ha ha Ha");
Output call Result
SYSTEM.OUT.PRINTLN ("Result from remote:" + result);
} catch (Accessexception e) {

} catch (RemoteException e) {

} catch (Notboundexception e) {

}
}
}
RMI Service Side
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
Import Java.rmi.registry.Registry;

RMI Service Side
public class Server {

public static void Main (string[] args) {
Registration Manager
Registry Registry = null;
try {
Create a service registry manager
Registry = Locateregistry.createregistry (8088);

} catch (RemoteException e) {

}
try {
Create a service
Serviceimpl Server = new Serviceimpl ();
Name the service binding
Registry.rebind ("Vince", server);

SYSTEM.OUT.PRINTLN ("Bind server");
} catch (RemoteException e) {

}
}
}
The Service registration Manager is written in the server and can of course be pulled out as a service, and in some other frameworks, it often uses zookeeper as a registration management role.

Introduction to the common RPC framework in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.