(1)。我新建了三个项目,SpringRmiApi(存放提供者和消费者共有的xx,例如实体类以及服务接口等等)、SpringRmiService(服务提供者)、SpringRmiProvider(服务消费者)

以下是SpringRmiApi一些代码

 package cn.coreqi.entities;

 import java.io.Serializable;

 public class User implements Serializable {
private Integer Id;
private Integer Age;
private String UserName;
private String PassWord;
private Integer Enabled; public User() {
} public User(Integer id, Integer age, String userName, String passWord, Integer enabled) {
Id = id;
Age = age;
UserName = userName;
PassWord = passWord;
Enabled = enabled;
} public Integer getId() {
return Id;
} public void setId(Integer id) {
Id = id;
} public Integer getAge() {
return Age;
} public void setAge(Integer age) {
Age = age;
} public String getUserName() {
return UserName;
} public void setUserName(String userName) {
UserName = userName;
} public String getPassWord() {
return PassWord;
} public void setPassWord(String passWord) {
PassWord = passWord;
} public Integer getEnabled() {
return Enabled;
} public void setEnabled(Integer enabled) {
Enabled = enabled;
} @Override
public String toString() {
return "User{" +
"Id=" + Id +
", Age=" + Age +
", UserName='" + UserName + '\'' +
", PassWord='" + PassWord + '\'' +
", Enabled=" + Enabled +
'}';
}
}
 package cn.coreqi.service;

 import cn.coreqi.entities.User;

 import java.util.List;

 public interface UserService {
public List<User> getAll();
public User getById(int Id);
public void addUser(User user);
public void modifyUser(User user);
public void delById(int Id);
}

(2)服务提供者代码

 package cn.coreqi.service.impl;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.stereotype.Repository; import java.util.ArrayList;
import java.util.List; @Repository
public class UserServiceImpl implements UserService {
private static List<User> users = null;
static {
users = new ArrayList<>();
users.add(new User(1,24,"fanqi","123456",1));
users.add(new User(2,22,"gaoxing","123456",1));
users.add(new User(3,23,"xihuan","123456",1));
}
@Override
public List<User> getAll() {
return users;
} @Override
public User getById(int Id) {
for (User user : users){
if(user.getId() == Id){
return user;
}
}
return null;
} @Override
public void addUser(User user) {
users.add(user);
} @Override
public void modifyUser(User user) {
delById(user.getId());
addUser(user);
} @Override
public void delById(int Id) {
for (User user : users){
if(user.getId() == Id){
users.remove(user);
}
}
}
}

暴露服务

 package cn.coreqi.rmi;

 import cn.coreqi.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiServiceExporter; @Configuration
public class RmiConfig {
@Bean
public RmiServiceExporter rmiServiceExporter(UserService userService){
RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
rmiServiceExporter.setService(userService);
rmiServiceExporter.setServiceName("UserService");
rmiServiceExporter.setServiceInterface(UserService.class);
// rmiServiceExporter.setRegistryHost("coreqi.cn");
rmiServiceExporter.setRegistryPort(1199);
return rmiServiceExporter;
}
}

(3)服务消费者

引用服务

 package cn.coreqi.rmi;

 import cn.coreqi.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiProxyFactoryBean; @Configuration
public class RmiConfig {
@Bean
public RmiProxyFactoryBean userService(){
RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean();
rmiProxy.setServiceUrl("rmi://localhost/UserService");
rmiProxy.setServiceInterface(UserService.class);
return rmiProxy;
}
}

使用服务

 package cn.coreqi.controller;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
public class HomeController {
@Autowired
private UserService userService; @GetMapping("/home/index")
@ResponseBody
public Object index(){
return userService.getAll();
}
}

Spring使用RMI进行远程方法调用的更多相关文章

  1. SpringBoot里使用RMI进行远程方法调用

    一.Java RMI定义 Java RMI:Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程 ...

  2. 简单实现Java的RMI——远程方法调用

    一.RMI简介: 说到RMI就不得不说RPC了. RPC:(Remote Procedure Call),远程过程调用. RMI(Remote Method Invocation),远程方法调用. R ...

  3. RMI远程方法调用

    RMI远程方法调用:适用于 客户端 调用 服务器 内的方法:(Kotlin 语言编写) 如果业务为二个服务器之间的通信,还是得用消息队列的形式,因为RMI 不适合 双向 调用 下面介绍RMI 的使用方 ...

  4. 【Java Web开发学习】远程方法调用RMI

    Java RMI 远程方法调用Remote Method Invocation 转载:http://www.cnblogs.com/yangchongxing/p/9078061.html 1.创建远 ...

  5. Java APi 之 RMI远程方法调用

    一.什么是RPC RPC全称是remote procedure call,即远程过程调用.它是一种协议,用于从远程计算机上请求服务. 例如有两台服务器A和B,A上的应用想要调用B上应用的方法,但是他们 ...

  6. [转]Java远程方法调用

    Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...

  7. XML-RPC远程方法调用

    一.简介 XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用. 它是一套允许运行在不同操作系统.不同环境的程序实现基于Internet过程调用的规范和一系列 ...

  8. Spring集成RMI实现远程调用

    前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...

  9. Java RMI 远程方法调用

    Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...

随机推荐

  1. day11 内置函数

    特殊算数运算 计算整数的和 l = [1,2,3,4,5] print(sum(l)) 除法运算,然后取余 在做页面的时候可以根据数据量分页的时候使用 print(divmod(10,3)) # (3 ...

  2. emwin之窗口关闭按钮用法

    @2018-07-27 [小记] 使用函数 FRAMEWIN_AddCloseButton() 实现关闭当前窗口的功能时,调用其窗口的父窗口必须处于打开状态,否则将导致假死(当前窗口死了,系统还在工作 ...

  3. [HAOI2015]按位或(min-max容斥,FWT,FMT)

    题目链接:洛谷 题目大意:给定正整数 $n$.一开始有一个数字 $0$,然后每一秒,都有 $p_i$ 的概率获得 $i$ 这个数 $(0\le i< 2^n)$.一秒恰好会获得一个数.每获得一个 ...

  4. 面试 -- Http协议相关(转载)

    http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...

  5. Linux 系统缓存机制学习

    前言:本文为参考他人的文章,是一篇学习记录型博客.理解linux的系统缓存机制有助于理解elasticsearch实时更新的原理. 一.缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲 ...

  6. Unity3d-AngryBots实例解读

    最近粗略研究了下Unity3d自带的例子AngryBots,记录一下,部分内容摘自http://oulehui.blog.163.com/blog/static/7961469820125251051 ...

  7. [poj3046][Ant counting数蚂蚁]

    题目链接 http://noi.openjudge.cn/ch0206/9289/ 描述 Bessie was poking around the ant hill one day watching ...

  8. 收藏:c语言的多线程同步

    1.<秒杀多线程第一篇 多线程笔试面试题汇总> 2.<秒杀多线程第二篇 多线程第一次亲密接触 CreateThread与_beginthreadex本质区别> 3.<秒杀 ...

  9. javascript:location.reload()和location.replace()的区别,及对图片缓存的影响。

    有段时间没有清理IE的临时文件(缓存文件),在我清理的时候,我突然发现一个问题. 我打开的一个网站,图片默认缓存一个月的,但我发现,当我上传图片或删除图片之后,图片重新缓存,也就意味着,在我上传新图片 ...

  10. 理解 PHP 依赖注入

    Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据 ...