文章目录版权声明:本文为 小异常 原创文章,非商用自由转载-保持署名-注明出处,谢谢!
本文网址:https://sunkuan.blog.csdn.net/article/details/120501308
- 一、搭建 Maven 聚合项目
- 1、创建 rmi-demo 项目
- 2、整理 rmi-demo 项目的 pom
- 3、创建 rmi-api、rmi-server、rmi-client 子模块
- 4、目录结构
- 5、Maven依赖
- 二、rmi-api
- 1、pom.xml
- 2、User 类
- 3、UserService 接口
- 三、rmi-server
- 1、pom.xml
- 2、application.yml
- 3、RMIConfig
- 4、UserServiceImpl
- 5、RmiServerApplication
- 四、rmi-client
- 1、pom.xml
- 2、application.yml
- 3、RMIConfig
- 4、UserInit
- 5、RmiClientApplication
- 五、测试程序
- 六、程序代码
RMI ( Remote Method Invocation,远程方法调用),它能够让 在某个 Java 虚拟机上的对象 像调用本地对象一样调用另一个 Java 虚拟机中的对象上的方法,它是 Java 原生的应用层分布式技术。
RMI 的性能是优于 HTTP 的。那为啥很少用到这个技术?那是因为用这个有很多局限性,首先它要保证传输的两端都要用 Java 语言实现,且两边需要有相同的对象类型和代理接口,不需要容器,但是加大了编程的难度。但在应用内部的各个子系统之间有的时候偶尔还是会看到他的身影,比如 EJB 就是基于 RMI 技术 的,这就与目前的 B/S 架构的软件大相径庭了。
本篇讲解 RPC 基于 RMI 技术的简单实现,只供大家了解,来帮助大家快速熟悉 Dubbo,项目中基本不会用到 RMI 技术。
一、搭建 Maven 聚合项目 1、创建 rmi-demo 项目
2、整理 rmi-demo 项目的 pom
4.0.0 pom rmi-server rmi-client rmi-api org.springframework.boot spring-boot-starter-parent 2.5.4 com.demo rmi-demo 0.0.1-SNAPSHOT rmi-demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
3、创建 rmi-api、rmi-server、rmi-client 子模块
以创建 rmi-api 子模块为例,rmi-server 与 rmi-client 创建方式相同。
4、目录结构
5、Maven依赖
二、rmi-api 1、pom.xml
4.0.0 com.demo rmi-demo 0.0.1-SNAPSHOT com.demo rmi-api 0.0.1-SNAPSHOT rmi-api Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-maven-plugin
2、User 类
public class User implements Serializable {
private Integer id;
private String name;
// set..() & get..()
// toString()
}
3、UserService 接口
public interface UserService {
User selectUserById(Integer id);
}
三、rmi-server 1、pom.xml
4.0.0 com.demo rmi-demo 0.0.1-SNAPSHOT com.demo rmi-server 0.0.1-SNAPSHOT rmi-server Demo project for Spring Boot 1.8 com.demo rmi-api 0.0.1-SNAPSHOT org.springframework.boot spring-boot-maven-plugin
2、application.yml
server: port: 8081
3、RMIConfig
@Configuration
public class RMIConfig {
@Autowired
private UserService userService;
@Bean
public RmiServiceExporter rmiServiceExporter(){
RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
rmiServiceExporter.setService(userService);
rmiServiceExporter.setServiceInterface(UserService.class);
rmiServiceExporter.setRegistryPort(2002);
rmiServiceExporter.setServiceName("userService");
return rmiServiceExporter;
}
}
4、UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Override
public User selectUserById(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三");
return user;
}
}
5、RmiServerApplication
@SpringBootApplication
public class RmiServerApplication {
public static void main(String[] args) {
SpringApplication.run(RmiServerApplication.class, args);
}
}
四、rmi-client 1、pom.xml
4.0.0 com.demo rmi-demo 0.0.1-SNAPSHOT com.demo rmi-client 0.0.1-SNAPSHOT rmi-client Demo project for Spring Boot 1.8 com.demo rmi-api 0.0.1-SNAPSHOT org.springframework.boot spring-boot-maven-plugin
2、application.yml
server: port: 8082
3、RMIConfig
@Configuration
public class RMIConfig {
@Bean(name = "userService")
public RmiProxyFactoryBean rmiProxyFactoryBean() {
RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
rmiProxyFactoryBean.setServiceUrl("rmi://127.0.0.1:2002/userService");
rmiProxyFactoryBean.setServiceInterface(UserService.class);
return rmiProxyFactoryBean;
}
}
4、UserInit
@Component
public class UserInit implements CommandLineRunner {
@Autowired
private UserService userService;
@Override
public void run(String... args) throws Exception {
System.out.println(userService.selectUserById(2));
}
}
5、RmiClientApplication
@SpringBootApplication
public class RmiClientApplication {
public static void main(String[] args) {
SpringApplication.run(RmiClientApplication.class, args);
}
}
五、测试程序
先执行 RmiServerApplication ( 服务端程序),再执行 RmiClientApplication ( 客户端程序),执行效果如下:
六、程序代码
本文代码的下载链接:https://download.csdn.net/download/sun8112133/24280947



