天天看點

初學RMI 搭建簡單的rmi

先不說别的,先把項目搭起來然後再說。

首先,服務端項目結構:

初學RMI 搭建簡單的rmi

其中,User類:

package com.test.entity;

import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 42L;
	
	String name;
	int age;
	
	public User(String name, int age){
		this.name = name;
		this.age = age;
	}
	
}
           

MyMain類

package com.test.main;

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

import com.test.rmi.RmiServer;
import com.test.rmi.impl.RmiServerImpl;

public class MyMain {
	
	public static void main(String[] args) {
		RmiServer rmiServer = null;
		Registry registry = null;
		try {
			registry = LocateRegistry.createRegistry(1099);
			rmiServer = new RmiServerImpl();
			registry.rebind("user", rmiServer);
			System.out.println("rmi server is ready ...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
           

RmiServer類

package com.test.rmi;

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

import com.test.entity.User;

public interface RmiServer extends Remote{
	String helloRmi() throws RemoteException;
	String sayHello(String name) throws RemoteException;
	User getUserByName(String name, int age) throws RemoteException;
}
           

RmiServerImpl類

package com.test.rmi.impl;

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

import com.test.entity.User;
import com.test.rmi.RmiServer;

public class RmiServerImpl extends UnicastRemoteObject  implements RmiServer{

	private static final long serialVersionUID = 42L;

	public RmiServerImpl() throws RemoteException {
		super();
	}

	public String helloRmi() throws RemoteException {
		return "歡迎學習rmi";
	}

	public String sayHello(String name) throws RemoteException {
		return "歡迎"+name+",使用rmi";
	}

	public User getUserByName(String name, int age) throws RemoteException {
		return new User(name,age);
	}

}
           

用戶端項目結構:

初學RMI 搭建簡單的rmi

Clientrmi類

package com.test.clientrmi;

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

import com.test.entity.User;
import com.test.rmi.RmiServer;

public class Clientrmi{
	    public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
			try {
				RmiServer rmiServer = (RmiServer) Naming.lookup("user");
				String str = rmiServer.helloRmi();
				String str1 = rmiServer.sayHello("張三");
				User user = rmiServer.getUserByName("lisi", 26);
				System.out.println("==="+str);
				System.out.println("===>>"+str1);
				System.out.println("===<<"+user);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}
           

User類

package com.test.entity;

import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 42L;
	
	String name;
	int id;
	
	@Override
	public String toString() {
		return "User [name=" + name + ", id=" + id + "]";
	}
}
           

RmiServer類

package com.test.rmi;

import java.rmi.RemoteException;

import com.test.entity.User;

public interface RmiServer {
	String helloRmi() throws RemoteException;
	String sayHello(String name) throws RemoteException;
	User getUserByName(String name, int age) throws RemoteException;
}
           

個人的一點心得體會,首先得先運作服務端,服務端正常啟動,才運作用戶端。

其次,在用戶端,必須有rmi需要暴露的接口,且接口所在路徑,包名,等等一切都要與服務端一緻。

不然都會報錯。

具體原理不做多餘的解釋,可以看下面連結的文章,人家說的很詳細我就不多說了。

https://blog.csdn.net/lmy86263/article/details/72594760

在開始搭建的時候心裡一直在想用戶端的類跟服務端的類都一樣,那我還要服務端幹嘛。等搭建完了一對比,就可以發現,在用戶端沒有實作類,是以用戶端這部分相當于直接使用,就像其他的對接接口一樣,但使用的時候我們就像在調用本地的方法。

RMI