RMI简易实现(基于maven)
参考其它rmi(remote method invocation)的代码后,加入了自己思考。整个工程基于maven构建,我觉得maven的模块化可比较直观地演示rmi
目录
项目结构图
模块解读
pom文件
rmi-impl
rmi-common-interface
rmi-server
rmi-client
各模块源码
rmi-common-interface
RemoteInterface.java
rmi-server
RMIServer.java
RMIServerProperties.java
rmi-client
RMIClient.java
启动说明(重要)
运行截图
启动服务是的控制台信息
编辑客户端连接服务端并发送消息
服务端控制台再次打印信息
项目结构图
模块解读
整个工程rmi-impl分为三个子模块
1、rmi-commom-interface:rmi-server与rmi-client都依赖的模块,用来定义供服务端实现、供客户端调用的公共远程调用接口
2、rmi-server:实现远程调用接口,并注册服务
3、rmi-client:拉去服务,并调用远程接口
pom文件
rmi-impl
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>psn.kiko</groupId><artifactId>rmi-impl</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>rmi-common-interface</module><module>rmi-server</module><module>rmi-client</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
rmi-common-interface
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>rmi-impl</artifactId><groupId>psn.kiko</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>rmi-common-interface</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
rmi-server
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>rmi-impl</artifactId><groupId>psn.kiko</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>rmi-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><--依赖common模块--><dependencies><dependency><groupId>psn.kiko</groupId><artifactId>rmi-common-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>
rmi-client
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>rmi-impl</artifactId><groupId>psn.kiko</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>rmi-client</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><--依赖common模块--><dependencies><dependency><groupId>psn.kiko</groupId><artifactId>rmi-common-interface</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies> </project>
各模块源码
rmi-common-interface
RemoteInterface.java
package psn.kiko.rmi.common;import java.rmi.Remote; import java.rmi.RemoteException;/*** 声明远程服务接口,此接口里声明的方法,由服务端实现,供客户端调用。* 鉴于此,将所有远程接口都放到common模块,并在需要此接口的模块引入<br>* on 2023/2/15 18:03*/ public interface RemoteInterface extends Remote {/*** 此方法将会在服务端打印message:由rmi-server模块实现,由rmi-client远程调用* @param message* @throws RemoteException*/void printMsg(String message) throws RemoteException; }
rmi-server
RMIServer.java
package psn.kiko.rmi.server;import psn.kiko.rmi.common.RemoteInterface;import java.net.MalformedURLException; import java.rmi.AlreadyBoundException; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.server.UnicastRemoteObject;/*** rmi服务端<br>* on 2023/2/15 18:08*/ public class RMIServer extends UnicastRemoteObject implements RemoteInterface {// 运行main注册服务public static void main(String[] args) {try {rmiServerRegistration();} catch (RemoteException | MalformedURLException | AlreadyBoundException e) {e.printStackTrace();}}private RMIServerProperties serverProperties;public void setServerProperties(RMIServerProperties serverProperties) {this.serverProperties = serverProperties;}public RMIServerProperties getServerProperties() {return serverProperties;}protected RMIServer() throws RemoteException {}@Overridepublic void printMsg(String message) throws RemoteException {System.out.println(this.serverProperties.getSeverName()+"获得客户端消息: "+message);}/*** rmi服务注册:添加到虚拟机注册表* @throws RemoteException* @throws AlreadyBoundException* @throws MalformedURLException*/private static void rmiServerRegistration() throws RemoteException, AlreadyBoundException, MalformedURLException {RMIServer rmiServer = new RMIServer();rmiServer.setServerProperties(new RMIServerProperties(8888, "rmi-server"));LocateRegistry.createRegistry(rmiServer.getServerProperties().getServerPort());Naming.bind(rmiServer.getServerProperties().getServerUrl(), rmiServer);System.out.println(rmiServer.getServerProperties().getSeverName()+"已启动================>");} }
RMIServerProperties.java
package psn.kiko.rmi.server;/*** 服务端参数<br>* on 2023/2/15 18:41*/ public class RMIServerProperties {private String serverUrl; //服务端通信地址private int serverPort;//服务端端口private String severName;//服务名称public RMIServerProperties(int serverPort,String serverName) {this.severName=serverName;this.serverPort = serverPort;this.serverUrl ="rmi://localhost:"+serverPort+"/"+severName;}public String getServerUrl() {return serverUrl;}public int getServerPort() {return serverPort;}public String getSeverName() {return severName;} }
rmi-client
RMIClient.java
package psn.kiko.client;import psn.kiko.rmi.common.RemoteInterface;import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.util.Scanner;/*** rmi客户端<br>* on 2023/2/15 18:19*/ public class RMIClient {// 运行main获取远程服务,并向其传输messagepublic static void main(String[] args) {try {RemoteInterface rmiServer = (RemoteInterface)Naming.lookup("rmi://localhost:8888/rmi-server");Scanner scanner = new Scanner(System.in);System.out.println("请输入需要发送给服务端的信息=============>");String message = scanner.next();rmiServer.printMsg(message);System.out.println("客户端发送消息完毕<=====================");} catch (NotBoundException | MalformedURLException | RemoteException e) {e.printStackTrace();}} }
启动说明(重要)
先运行RMIServer.main(启动后将等待客户端连接),再运行RMIClient.main(可多次单独运行,进行多次连接通信)
运行截图
启动服务是的控制台信息
截图中的红色正方形也表示服务程序启动后就一直运行着
客户端连接服务端并发送消息
灰色正方形表示客户端与服务端通信完毕就断开连接
服务端控制台再次打印信息
红色正方形表示服务端与客户端通信完毕后仍在运行
以上就是全部关于RMI的详细基础用法了