What is the RPC framework? Java has its own RPC implementation. Introduction to RMI framework

Keywords: Java github socket firewall

This blog Uncle Cat's Blog For reprinting, please state your origin

Learning Series

Java comes with RPC implementation, introduction to RMI framework

First, RMI (Remote Method Invocation) is a Java-specific RPC implementation, which enables Java objects deployed on different hosts to communicate and invoke methods. It is a Java-based remote method invocation technology.

Let's give priority to implementing a RPC case of RMI.

Project source address: RPC_Demo Remember the comgithubrmi in the project

  • 1. First, we need to create an interface method for the server side, and this interface is best inherited from Remote.
package com.github.rmi.server;

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

/**
 * Create by UncleCatMySelf in 21:03 2019\4\20 0020
 */
public interface MyService extends Remote {

    String say(String someOne)throws RemoteException;

}
  • 2. For the interface implementation class, the RMI interface method definition must explicitly declare that a RemoteException exception is thrown, and the service-side method implementation must inherit the Unicast RemoteObject class, which defines the service invocation and the service provider object implementation, and establishes a one-to-one connection.
package com.github.rmi.server;

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

/**
 * Create by UncleCatMySelf in 21:05 2019\4\20 0020
 */
public class MyServiceImpl extends UnicastRemoteObject implements MyService {

    protected MyServiceImpl() throws RemoteException {
    }

    public String say(String someOne) throws RemoteException {
        return someOne + ",Welcome to Study!";
    }
}
  • 3. We also need a configuration class for the server, because the RMI communication ports are generated randomly, so it may be intercepted by the firewall. In order to prevent interception by firewalls, it is necessary to enforce RMI communication ports, which are usually implemented by customizing a RMISocketFactory class.
package com.github.rmi.config;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.RMISocketFactory;

/**
 * Create by UncleCatMySelf in 21:15 2019\4\20 0020
 */
public class CustomerSocketFactory extends RMISocketFactory {

    public Socket createSocket(String host, int port) throws IOException {
        return new Socket(host, port);
    }

    public ServerSocket createServerSocket(int port) throws IOException {
        if (port == 0){
            port = 8855;
        }
        System.out.println("RMI Communication port : " + port);
        return new ServerSocket(port);
    }
}
  • 4. OK, then you can write the startup code of the server.
package com.github.rmi.server;

import com.github.rmi.config.CustomerSocketFactory;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.RMISocketFactory;

/**
 * Create by UncleCatMySelf in 21:07 2019\4\20 0020
 */
public class ServerMain {

    public static void main(String[] args) throws Exception {
        //Registration service
        LocateRegistry.createRegistry(8866);
        //Specify communication ports to prevent interception by firewalls
        RMISocketFactory.setSocketFactory(new CustomerSocketFactory());
        //Create service
        MyService myService = new MyServiceImpl();
        Naming.bind("rmi://localhost:8866/myService",myService);
        System.out.println("RMI Server Start Normal");
    }

}
  • 5. The startup of the client is relatively simple. We only need to enter the service and call the corresponding remote method.
package com.github.rmi.client;

import com.github.rmi.server.MyService;

import java.rmi.Naming;

/**
 * Create by UncleCatMySelf in 21:10 2019\4\20 0020
 */
public class ClientMain {

    public static void main(String[] args) throws Exception {
        //Service Introduction
        MyService myService = (MyService) Naming.lookup("rmi://localhost:8866/myService");
        //Calling remote methods
        System.out.println("RMI The service-side call returns:" + myService.say("MySelf"));
    }

}

Finally, we can see the effect.


Some Key Points about RMI

  • Supporting true object-oriented polymorphism is the advantage of RMI.
  • Perfectly supports the unique features of the Java language and does not support other languages.
  • With Java native serialization, all serialized objects must implement the java.io.Serializablie interface.
  • The underlying communication is a Socket implemented by BIO (Synchronized Blocking I/O)
  • Due to the performance problems of BIO and native serialization, the performance of RMI is poor. If your project has high performance requirements, it may not be appropriate.

Public Number: Java Cat Says

Learning Communication Group: 728698035

Architecture design (code farmer) and entrepreneurship technical consultant, uninhibited mediocrity, love open source, chat about the process of life and irregular dry goods.

Posted by fatnjazzy on Mon, 29 Apr 2019 15:20:41 -0700