MyBatis Overall Architecture Analysis: Rpc and RMI Services

Posted by Btown2 on Wed, 29 Dec 2021 23:24:20 +0100

4.gRPC:gRPC is the underlying network library implemented by the Google team based on Netty development, and this framework also has a GoLanguage version based on NetLibrary development

5.RMI:rmi can be said to be one of the earliest RPC frameworks in java, and this RPC framework is developed by sun team and integrated in JDK. It can be said that it can be completely implemented out of the box without any external jar dependency. However, since the early RPC implementation and design are not designed to solve the high concurrency problem of Internet enterprise class, it is not recommended to implement RPC in Internet development now.

Basic use of RMI framework

Since RMI is an rpc framework designed by the java team, it is not suitable for enterprise production now, but its ideas and specifications are worth learning. Let's see how RMI framework is used:

Three Basic Classes of RMI

The API s needed to implement RMI are basically in three categories, as follows:

java.rmi: Provide the classes, interfaces, and exceptions required by the client;

java.rmi.server: Provide the classes, interfaces, and exceptions required by the server;

java.rmi.registry: Provides the creation of a registry and the classes, interfaces, and exceptions for finding and naming remote objects;

Building RMI Server

First, the instances in RMI where the server side is available for client calls are called remote objects, while in RMI, Java is implemented. Rmi. The class of the Remote interface or inherits java. Rmi. The Remote interface is a remote object of the RMI. So let's define an interface that inherits java.rmi.Remote:

/**
 *User Processor
**/
public interface UserHandler extends Remote {
    String getUserName(int id) throws RemoteException;
    String getUserPassWord() throws RemoteException;
    User getUserByName(String name) throws RemoteException;
}

One thing to note here is that all methods defined in an interface that inherits the Remote interface must throw a RemoteException exception, and the implementation class of the interface must directly or indirectly inherit java. Rmi. Server. The UnicastRemoteObject class, which provides many RMI-enabled methods, can export a reference to a remote object through the JRMP protocol, and generate a Stub object built by a dynamic proxy with the following code:

public class UserHandlerImpl extends UnicastRemoteObject implements UserHandler {
    //Here, because the set inherits the UnicastRemoteObject class and its constructor throws RemoteException, the construct is declared
    public UserHandlerImpl() throws RemoteException {
        super();
    }

    @Override
    public String getUserName(int id) throws RemoteException {
        return "pdc";
    }
    @Override
    public String getUserPassWord() throws RemoteException{
        return 654321;
    }
    @Override
    public User getUserByName(String name) throws RemoteException{
        return new User(name, 654321);
    }
}

Here we construct a User entity, so we serialize it for remote transmission:

public class User implements Serializable {
    private static final long serialVersionUID = 42L;
    
    private String name;
    private String passWord;

    public String getName(){
        return this.name;
    }
    
    public String getPassWord(){
        return this.passWord;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public void setPassWord(String passWord){
        this.passWord = passWord;
    }
    
    public User(String name, String passWord) {
        this.name = name;
        this.passWord = passWord;
    }
}

It is important to note that if the jdk version is less than 1.5, you need to manually run the rmic command to generate the Stub object of the implementation class, and 1.5 starts using the dynamic proxy technology, you can already automatically generate the Stub object, and when you are done, you can start the server:

UserHandler userHandler = null;
try {
    userHandler = new UserHandlerImpl();
    Naming.rebind("user", userHandler);//Bind the current instance with a name of user, then the client calls to find the corresponding name
    System.out.println(" RMI Server Start Successfully");
} catch (Exception e) {
    System.err.println(" RMI Server Start Failed");
    e.printStackTrace();
}

Building RMI Registry

In fact, the so-called registry is the process of storing the name of RMI server startup and binding. Since JDK has integrated RMI code into JDK, RMI registry does not need to write any code. There is already a rmiregistry in JDK bin directory. Exe program, but we need to start the registry under the current class class path (so be aware that the JAVA_HOME environment variable must be configured successfully), to the class path, enter the command:

rmiregistry 9999

You can specify that rmi's Registry will run on port 9999. If you don't specify a port, 1099 will be used by default. Of course, you don't want RMI's registry to be displayed in the foreground, or you can enter a command to run in the background:

start rmiregistry

Building RMI Client

The previous server and registry are running. Next we need the client to initiate an access request. Note that the User instance class and the UserHandler interface also have a copy of the client code (the same code is relied on during enterprise development), so the client call code here is as follows:

try {
    UserHandler handler = (UserHandler) Naming.lookup("user");//The user used here is the name of the binding when the server starts
    String passWord = handler.getUserPassWord();
    String name = handler.getUserName(1);
    System.out.println("name: " + name);
    System.out.println("passWord: " + passWord);
    System.out.println("user: " + handler.getUserByName("pdc"));
} catch (Exception e) {
    e.printStackTrace();
}

This allows you to get information about remote objects on the server side. Of course, there are two points to note:

1. The package name of the UserHandler entity class and the UserHandler interface on the server side need to be consistent here, even if the qualified full class name used needs to be consistent, otherwise the following error will be reported:

2. The User instance we get here is of reference type. It is important to note that the User instance object we get must also match the package name on the server side, i.e. qualify the whole class name to be the same, otherwise, handler. The getUserByName ("pdc") method call results in an error, as follows:

Custom Start RMI Registry

At the end of this article, let's have an egg. Do you remember the registry of RMI above? Previously, we started through JDK's exe program, so can we develop or start the RMI registry by ourselves? It's possible, in java. Rmi. There is a Registry interface in the registry package, and it has a default implementation class, LocateRegistry. In fact, the Naming class in the JDK source code is the registration and invocation of the LocateRegistry implementation. Let's take a look at the LocateRegistry method:

createRegistry(int port)
createRegistry(int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf)
getRegistry()
getRegistry(int port)
getRegistry(String host)
getRegistry(String host, int port)
getRegistry(String host, int port, RMIClientSocketFactory csf)

You can see that there are two ways to create a registry here, one is port only, the default open registry is the local registry, the other is a custom registry where you can enter ip, port, and some connection policies, and several ways to get the registry, which clearly provides the way to create and invoke the registry. Similarly, our previous server-side code needs only a slight change, as follows:

epilogue

Small editors are also very touching, if they have been in small and medium-sized companies and have not been exposed to large Internet architecture design, it may be difficult for them to reach the technical and cognitive level of senior architects for a lifetime just by reading their own books. Learning from tough people is the most effective way to reduce time exploration and energy waste.

The industry we have chosen has always been a continuous learning industry and a very young eater.

Although you may often see programmers earning hundreds of thousands a year, these people are not the majority, either have a school halo or are big businesses like Aliwa. Older people are more likely to be cut.

Share a wave of small learning materials!

Give it to everyone who wants to learn Java to improve themselves. For information, click here for free

It is a large enterprise like Alihua. Older people are more likely to be cut.

Share a wave of small learning materials!

Give it to everyone who wants to learn Java to improve themselves. For information, click here for free
[img-tGr63b0A-1628078843388)]

This is the end of the article. Your favorite friends can give you some comments and comments. Thank you for your support!

Topics: Java Back-end Interview Programmer