当前位置: 首页 > news >正文

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的详细基础用法了

 

 

http://www.lryc.cn/news/7699.html

相关文章:

  • ‘excludeSwitches‘ 的 [‘enable-logging‘] 和[‘enable-automation‘]
  • 华为OD机试 - 最短木板长度(Python)| 真题+思路+考点+代码+岗位
  • 第一个Python程序-HelloWorld与Python解释器
  • C++数据类型
  • 华为OD机试 - 考古学家(Python)| 真题+思路+考点+代码+岗位
  • 常用调试golang的bug以及性能问题的实践方法
  • 什么是溶血症?什么是ABO溶血?溶血检查些什么?
  • NLP实践——知识图谱问答模型FiD
  • MyBatis 多表关联查询
  • 《NFL橄榄球》:克利夫兰布朗·橄榄1号位
  • InstructGPT笔记
  • 【uniapp】getOpenerEventChannel().once 接收参数无效的解决方案
  • ELK分布式日志收集快速入门-(二)kafka进阶-快速安装可视化管理界面-(单节点部署)
  • 线程的创建
  • 分布式之Paxos共识算法分析
  • 35岁测试工程师,面临中年危机,我该如何自救...
  • 时间轮算法概念
  • [SCTF2019]babyre 题解
  • 全志H3系统移植 | 移植主线最新uboot 2023.04和kernel 6.1.11到Nanopi NEO开发板
  • vue项目第四天
  • 「C语言进阶」数据内存的存储
  • 面试必问:进程和线程的区别(从操作系统层次理解)
  • ModuleNotFoundError: No module named ‘apex‘与 error: legacy-install-failure
  • Python3 VScode 配置
  • VMware 修复了三个身份认证绕过漏洞
  • 实现一个简单的Database10(译文)
  • CTF-取证题目解析-提供环境
  • 计算机基础 | 网络篇 | TCP/IP 四层模型
  • 实时数据仓库
  • leetcode 1250. 检查「好数组」