Java uses RMI

Java uses RMI

Java uses serialization to implement remote method calls, which is very convenient for division of labor and cooperation. This article records the implementation of rmi using the java standard library

1. Server

structure

.
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── hello
│   │           ├── HelloImpl.java
│   │           ├── HelloServer.java
│   │           └── IHello.java
│   └── test
│       └── java
│           └── mycom
│               └── AppTest.java
└── target

IHello.java

package hello;

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

public interface IHello extends Remote {
    public String sayHelloToSomeBody(String someBodyName) throws RemoteException;

HelloImpl.java

package hello;

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

public class HelloImpl extends UnicastRemoteObject implements IHello {
    public HelloImpl() throws RemoteException {
        super();
    }
    public String sayHelloToSomeBody(String someBodyName) throws RemoteException {
        System.out.println("Connected sucessfully!");
        return "你好," + someBodyName + "!";
    }
}

HelloServer.java

package hello;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class HelloServer {
    public static void main(String args[]) {
        try {
            IHello rhello = new HelloImpl();
            LocateRegistry.createRegistry(8888);
            // 如果配置在远程服务器,把地址换成你的ip
            System.setProperty("java.rmi.server.hostname","127.0.0.1");
            Naming.bind("rmi://localhost:8888/RHello", rhello);
            System.out.println(">>>>>INFO:远程IHello对象绑定成功!");
        } catch (RemoteException e) {
            System.out.println("创建远程对象发生异常!");
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            System.out.println("发生重复绑定对象异常!");
            e.printStackTrace();
        } catch (MalformedURLException e) {
            System.out.println("发生URL畸形异常!");
            e.printStackTrace();
        }
    }
}

Starting HelloServer.java on the server side will listen for requests on port 8888

2. Client

The client only needs to share the interface file

startup code

package hello;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Client {
    public static void main(String args[]) {
        try {
            // 填写服务器ip
            IHello rhello = (IHello) Naming.lookup("rmi://127.0.0.1:8888/RHello");
            System.out.println(rhello.sayHelloToSomeBody("Erich"));
        } catch (NotBoundException e) {
        } catch (MalformedURLException e) {
            e.printStackTrace();
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

3. Summary

It can be seen that the client can perform operations as long as it obtains the interface file, and the specific implementation is left to the server to complete. This architecture can decouple calls and implementations

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324789311&siteId=291194637