分布式通信框架-java rmi与自定义rmi(分布式五)

目录

      • 快速入门demo
        • 接口
        • 实现
        • 服务器端
        • 客户端
        • 测试
        • 思路整理
      • rmi层次分析,及简单实现
        • 实现思路
        • 接口
        • 实现
        • 服务端代理
        • 客户端代理
        • 接口
        • 服务端
        • 客户端
        • 测试结果

快速入门demo

文件目录
分布式通信框架-java rmi与自定义rmi(分布式五)_第1张图片

接口

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

public interface IHello extends Remote {
	String sayHello(String name) throws RemoteException;
}

实现

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

public class HelloImpl extends UnicastRemoteObject implements IHello {

	private static final long serialVersionUID = -8306410524095663750L;

	protected HelloImpl() throws RemoteException {
		super();
	}

	@Override
	public String sayHello(String name) throws RemoteException {
		return "hello, " + name;
	}
}

服务器端

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 hello = new HelloImpl();
			LocateRegistry.createRegistry(8888);
			Naming.bind("rmi://localhost:8888/sayHello", hello);
			System.out.println("服务器已经启动...");
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			e.printStackTrace();
		}
	}
}

客户端

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

public class HelloClient {
	public static void main(String[] args) {
		try {
			IHello hello = (IHello) Naming.lookup("rmi://localhost:8888/sayHello");
			String msg = hello.sayHello("肥肥");
			System.out.println(msg);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}

测试

启动服务端
分布式通信框架-java rmi与自定义rmi(分布式五)_第2张图片
启动客户端
分布式通信框架-java rmi与自定义rmi(分布式五)_第3张图片

思路整理

  1. 创建接口,并继承java.rmi.Remote
  2. 实现接口,并继承java.rmi.server.UnicastRemoteObject
  3. 实现接口的方法都需要抛出异常java.rmi.RemoteException
  4. 服务端createRegistry注册,
  5. 客户端请求

rmi层次分析,及简单实现

分布式通信框架-java rmi与自定义rmi(分布式五)_第4张图片

实现思路

分层设计
分布式通信框架-java rmi与自定义rmi(分布式五)_第5张图片
具体请求时序图

分布式通信框架-java rmi与自定义rmi(分布式五)_第6张图片

接口

import java.io.IOException;

public interface IUser {
	int getAge() throws IOException;
}

实现

import java.io.IOException;

public class UserImpl implements IUser {
	private int age;

	@Override
	public int getAge() throws IOException {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

服务端代理

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class User_Skeleton extends Thread {
	
	private IUser user;
	
	public User_Skeleton(IUser userProxy) {
		super();
		this.user = userProxy;
	}

	@Override
	public void run() {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(8888);
			
			while (true) {
				Socket socket = serverSocket.accept();
				ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
				Object method = ois.readObject();
				
				if (method.equals("age")) {
					int age = user.getAge();
					ObjectOutputStream oop = new ObjectOutputStream(socket.getOutputStream());
					oop.writeInt(age);
					oop.flush();
				}
				socket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (serverSocket != null) {
				try {
					serverSocket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}
	

客户端代理

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class User_Stub implements IUser {
	private Socket socket;
	
	public User_Stub() throws Exception {
		socket = new Socket("localhost", 8888);
	}
	
	@Override
	public int getAge() throws IOException {
		ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
		oos.writeObject("age");
		oos.flush();
		
		ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
		return ois.readInt();
	}
}

接口

import java.io.IOException;

public interface IUser {
	int getAge() throws IOException;
}

服务端

public class UserServer {
	public static void main(String[] args) {
		UserImpl user = new UserImpl();
		user.setAge(24);
		
		User_Skeleton skeleton = new User_Skeleton(user);
		skeleton.start();
		System.out.println("代理服务器已经启动...");
	}
}

客户端

public class UserClient {

	public static void main(String[] args) throws Exception {
		IUser user = new User_Stub();
		int age = user.getAge();
		System.out.println(age);
	}

}

测试结果

服务端
在这里插入图片描述

客户端
在这里插入图片描述

测试通过

你可能感兴趣的:(分布式)