Spring使用RMI进行远程方法调用
(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进行远程方法调用的更多相关文章
- SpringBoot里使用RMI进行远程方法调用
一.Java RMI定义 Java RMI:Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程 ...
- 简单实现Java的RMI——远程方法调用
一.RMI简介: 说到RMI就不得不说RPC了. RPC:(Remote Procedure Call),远程过程调用. RMI(Remote Method Invocation),远程方法调用. R ...
- RMI远程方法调用
RMI远程方法调用:适用于 客户端 调用 服务器 内的方法:(Kotlin 语言编写) 如果业务为二个服务器之间的通信,还是得用消息队列的形式,因为RMI 不适合 双向 调用 下面介绍RMI 的使用方 ...
- 【Java Web开发学习】远程方法调用RMI
Java RMI 远程方法调用Remote Method Invocation 转载:http://www.cnblogs.com/yangchongxing/p/9078061.html 1.创建远 ...
- Java APi 之 RMI远程方法调用
一.什么是RPC RPC全称是remote procedure call,即远程过程调用.它是一种协议,用于从远程计算机上请求服务. 例如有两台服务器A和B,A上的应用想要调用B上应用的方法,但是他们 ...
- [转]Java远程方法调用
Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口.它使客户机上运行的程序可以调用远 ...
- XML-RPC远程方法调用
一.简介 XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用. 它是一套允许运行在不同操作系统.不同环境的程序实现基于Internet过程调用的规范和一系列 ...
- Spring集成RMI实现远程调用
前提: 1.开发工具: jdk tomcat ecplise,开发工具的使用本篇不做介绍. 2.需具备以下知识:javase servelt web rmi spring maven 一.关于RMI ...
- Java RMI 远程方法调用
Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...
随机推荐
- [UVALive 3661] Animal Run
图片加载可能有点慢,请跳过题面先看题解,谢谢 附:中文题面,[BZOJ1001]狼抓兔子 就要考联赛了,博客里题目的\(style\)都变了,几乎都是些套路啥的,这道题也比较套路 第一眼看这道题的感觉 ...
- 神奇的操作——线段树合并(例题: BZOJ2212)
什么是线段树合并? 首先你需要动态开点的线段树.(对每个节点维护左儿子.右儿子.存储的数据,然后要修改某儿子所在的区间中的数据的时候再创建该节点.) 考虑这样一个问题: 你现在有两棵权值线段树(大概是 ...
- OI生涯回忆录 2017.9.10~2018.11.11
然而并没有退役 为了这两天,也准备了一年. 高一零基础的蒟蒻,NOIP2017仅有80pts 之后看着luogu的倒计时, 300天,200天,100天,30天, 直到10天.1天. 10号,11号的 ...
- A1005. Spell It Right
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- 如何删除launchpad里的空文件夹
方法1: 重启后将任意一个应用拖入再移出 方法2: 在终端(应用工具>实用工具>终端)执行:defaults write com.apple.dock ResetLaunchPad -bo ...
- 通过url传递参数如果汉字乱码采用的方法
urlCodeDeal 方法把汉字编码, 在Jsp界面通过Escape.unescape方法,将编码反编译成汉字. 下面是urlCodeDeal方法: //UrlCode 处理代码 function ...
- 浏览器中输入URL发生了什么
浏览器中输入URL会发生什么呢?这是我们经常会问到的一个问题. 我们知道的都是会发送http请求,服务端会处理请求给我们响应的结果,浏览器会渲染html 页面 但其实会遗漏掉一些比较重要的东西.下面的 ...
- csp20141203 集合竞价 解题报告
Solution:对股票出价进行排序,然后按照价格递增的次序依次设定p的价格并求成交量.1. //prove that the result of price(maximum--maxprice) i ...
- linux 一些命令(2)
1.查看centos版本 # lsb_release -a 2.查找大于1000M的文件 find / -type f -size +1000M -print0 | xargs -0 du –h
- 采集化工内容写入TXT文本
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...