1.声明

当前内容用于本人学习RMI,这个基于java的服务发布调用协议,感觉类似webservice,但是RMI只能在java之间产生通信,其他的不支持

2.创建主要的项目springboot-rmi


在其模块下面添加rmi-api、rmi-client、rmi-server这三个maven模块

pom.xml的配置

<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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.5.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>springboot-rmi</groupId><artifactId>springboot-rmi</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>springboot-rmi</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><modules><module>rmi-server</module><module>rmi-client</module><module>rmi-api</module></modules><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies>
</project>

3.在rmi-api中创建主要的接口


public interface HelloRMIService {void sayHi(String msg);
}

然后将其使用maven build一下,如果出现异常则需要在pom添加

<build><defaultGoal>compile</defaultGoal>
</build>

4.在rmi-server和rmi-client中添加rmi-api的依赖

 <dependency><artifactId>rmi-api</artifactId><groupId>rmi-api</groupId><version>0.0.1-SNAPSHOT</version>
</dependency>

5.在rmi-server中创建实现类和其他的类

创建HelloRMIService 的实现类HelloRMIServiceImpl

@Service
public class HelloRMIServiceImpl implements HelloRMIService {@Overridepublic void sayHi(String msg) {System.out.println("server接收到信息:【" + msg + "】");}}

创建启动入口类

/*** @description 用于创建RMIServer* @author hy* @date 2020-04-16*/
@SpringBootApplication
public class RMIServerApplication {@AutowiredHelloRMIService service;@Beanpublic RmiServiceExporter RmiServiceExporter() {RmiServiceExporter exporter = new RmiServiceExporter();exporter.setService(service);exporter.setServiceName("helloRMI");exporter.setServiceInterface(HelloRMIService.class);exporter.setRegistryPort(9999);//注意这里必须是注册端口try {exporter.afterPropertiesSet();} catch (RemoteException e) {e.printStackTrace();}return exporter;}public static void main(String[] args) {SpringApplication.run(RMIServerApplication.class, args);}
}

配置启动端口:server.port=8002

6.在rmi-client中创建访问服务接口的类

入口类

@SpringBootApplication
public class RMIClientApplication {@Beanpublic RmiProxyFactoryBean rmiProxyFactoryBean() {RmiProxyFactoryBean bean = new RmiProxyFactoryBean();bean.setServiceUrl("rmi://127.0.0.1:9999/helloRMI");bean.setServiceInterface(HelloRMIService.class);bean.setRefreshStubOnConnectFailure(true);return bean;}public static void main(String[] args) {SpringApplication.run(RMIClientApplication.class, args);}}

创建访问controller类

@RestController
public class RMIController {@AutowiredRmiProxyFactoryBean bean;@RequestMapping("/test")public String test(String msg) {HelloRMIService service = (HelloRMIService) bean.getObject();service.sayHi(msg);return "请求成功";}}

配置访问端口:server.port=8001

7.测试

先启动rmi-server,再启动rmi-client,最后访问http://localhost:8001/test?msg=123


操作成功

8.总结

1.使用springboot集成rmi的时候需要注意的是服务端是发布入口,需要指定注册端口和服务的名称

2.客户端访问的服务端的时候注意和服务端保持一致,否则会报错的

3.通过使用发现和dubbo的以及和webservice感觉一致

以上纯属个人见解,如有问题请联系本人!

SpringBoot之RMI的简单使用相关推荐

  1. java+cache使用方法_java相关:springboot使用GuavaCache做简单缓存处理的方法

    java相关:springboot使用GuavaCache做简单缓存处理的方法 发布于 2020-3-29| 复制链接 摘记: 问题背景 实际项目碰到一个上游服务商接口有10秒的查询限制(同个账号). ...

  2. 微信小程序+SpringBoot+mybatis+MySQL实现简单的登录

    微信小程序+SpringBoot+mybatis+MySQL实现简单的登录 当下微信小程序和springboot都是比较火的.今天我们来用springboot和微信小程序来实现简单的登录. 1.首先来 ...

  3. 【快速上手系列】使用Springboot集成Swagger2的简单使用测试

    [快速上手系列]使用Springboot集成Swagger2的简单使用测试 简介 Swagger2是为了解决企业中接口(api)中定义统一标准规范的文档生成工具. 尤其是前后端分离时对一些业务接口也不 ...

  4. springboot 整合minio client 简单使用 视频流展示demo

    springboot 整合minio client 简单使用 视频流展示demo 不好意思 本人没有强迫症 代码随便造 知识来源于百度,感谢百度合作伙伴以及各位网友demo的帮助 源码:https:/ ...

  5. springboot中JJWT的简单使用

    springboot中JJWT的简单使用 一 什么是JWT jwt全称json web token,是基于json协议的用于结局认证授权的方法.token就是令牌,其主要作用是用来进行授权(Autho ...

  6. Springboot+nacos+seata实现简单分布式事务经验分享:二

    Springboot+nacos+seata实现简单的分布式事务 上一篇文章把三个服务都注册进nacos中了,这次就开始写业务代码 首先先创建三个数据库,每个数据库都需要有一张记录表,一张回滚表 or ...

  7. SpringBoot + Elasticsearch7.6实现简单查询及高亮分词查询

    欢迎关注方志朋的博客,回复"666"获面试宝典 前言 该文章需要提前准备好Elasticsearch7.6以及ik分词器的环境,如果还没准备好的可以看看 https://blog. ...

  8. websocket 本地可以服务器断开 springboot linux_SpringBoot+WebSocket实现简单的数据推送...

    问题背景 为什么要要用websocket呢?websocket相对于传统http协议有什么优势呢? http协议有一个缺陷,就是通信只能由客户端发起,服务器返回数据,不能做到服务器主动向客户端推送.这 ...

  9. SpringBoot连接Redis超简单

    建立一个springboot项目,什么也不用设置,建立一个最简单的就可以的,首先还是加入我们的Maven驱动包 <!-- 加入redis连接池--> <dependency>& ...

最新文章

  1. 聚焦点showModalDialog的一些方法
  2. Android安全开发之Provider组件安全
  3. vb.net2019- 串口serial port通信
  4. phoenix-sqlline 连接失败
  5. 在条件类型中使用 infer 关键字
  6. 麦克纳姆轮全向移动机器人纵向直线运动分析
  7. 乐视android系统耗电量大,乐视手机突然充电慢耗电快,怎么解决?
  8. box怎么用 latency_box-sizing使用场景
  9. Round14—最小生成树
  10. 对于拖延症的最好方法
  11. WARN: Establishing SSL connection without server‘s identity verification is not recommended. Acco...
  12. LCD高抗干扰液晶段码屏显示驱动芯片:VK2C21A/B/BA/C/D 大量应用于音箱/音响面板LCD显示驱动
  13. 如何成为高效的学习高手
  14. 苹果系统tft选哪个服务器不卡,tft和ips选哪个 tft和ips区别介绍【图文】
  15. python将英语中的复数名词变成单数名词
  16. Succ(x),Pred(X),inc(x,i),dec(x,i)
  17. css取消a标签自动换行,css中a元素放长英文字母或者数字自动换行的解决
  18. 华为VLAN下应用ACL
  19. 怎么在VM虚拟机上装显卡等驱动
  20. 研究目标系统的可行性

热门文章

  1. 全面、详细、通俗易懂的C语言语法和标准库
  2. MySQL 8.0初体验
  3. maven报错one of its dependencies could not be resolved
  4. tensorflow之四运转方式入门
  5. CVPR 2016 摘要阅读
  6. ThreeJs 学习之旅(十六)—Physics(物理)
  7. 在OpenCV里实现伽马变换
  8. 测试案例中@SpringBootTest与@RunWith**的含义
  9. 2020年Java就业前景和就业方向分析!
  10. 如何加入家庭组计算机打印机,解决方案:Win7系统设置家庭组计算机设置共享打印机...