远程服务调用(RMI)
模块概念的引入,是本框架的一大优势,而跨JVM的远程服务调用则是另一个最有价值的功能。
《本地服务调用》一文中我们讲解了跨模块间的服务调用可以是这样的:
ServiceHelper.invoke("pas","AuthService:auth",new Data("principal",principalInstance,"url","http://localhost:8080/pas/index.shtml"));
如果这个pas不是本地模块,而是远程模块,又该如何调用呢?
正如你期望的那样,还是上面这样调用,这意味着,无论这个pas模块部署在什么地方,你得代码复杂度没有变化。可怜了那些靠代码量体现工作量的程序猿们,处理了一个如此复杂的工作,还是这么简单的两行代码。
远程服务的实现方法
flying中是通过hessian来实现远程调用的,主要是因为hessian简单、功能够用。
服务提供端代码:
1、定义Hessian服务提供接口
public interface RemoteService {
RemoteValue invoke(Principal principal, String moduleId, String serviceId, RemoteValue request) throws Exception;
}
2、定义Hessian的Servlet
@WebServlet(value="/remoting")
public class HessianRemoteService extends HessianServlet implements RemoteService{
private final static Logger logger = Logger.getLogger(HessianRemoteService.class);
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)servletRequest;
long start = System.currentTimeMillis();
final String requestURI = req.getRequestURI() + (req.getQueryString() == null?"":"?" + req.getQueryString());
try {
super.service(servletRequest, servletResponse);
} finally {
logger.info("Access(" + (System.currentTimeMillis() - start) + ")\tURI:" + requestURI);
}
}
public RemoteValue invoke(Principal principal, String moduleId, String serviceId, RemoteValue remoteData) throws Exception {
long start = System.currentTimeMillis();
try {
Data req = remoteData.getValue();
LocalModule module = Application.getInstance().getModules().getLocalModule(moduleId);
ThreadContext.getContext().reset(module, serviceId, req, principal);
ThreadContext.getContext().setInvokeType(ThreadContext.InvokeType.Remote);
Data result = module.invoke(serviceId, req);
remoteData.setValue(result);
} catch (Exception e){
remoteData.setException(e);
} finally {
logger.info("RemoteInvoker(" + (System.currentTimeMillis() - start) + ")\tModuleId:" + moduleId+";ServiceId:" + serviceId);
}
return remoteData;
}
}
3、实现Hessian调用的客户端
public class HessianRemoteServiceInvoker implements RemoteServiceInvoker {
@Override
public Data invoke(Principal principal, LocalModule localModule, String remoteModuleId, String serviceId, Data request)
throws Exception {
if (localModule == null) {
localModule = ThreadContext.getContext().getModule();
}
if (localModule != null) {
Thread.currentThread().setContextClassLoader(localModule.getClassLoader());
}
String url = Application.getInstance().getModules().getRemoteModule(remoteModuleId).getPath();
RemoteService remoteService = getRemoteService(url);
Map<String, Object> nonSerializable = request.filterValues(new DataFilter(){
@Override
public boolean isValid(String key, Object value) {
return key != null && value != null && (value instanceof Serializable);
}
});
final RemoteValue res = remoteService.invoke(principal, remoteModuleId, serviceId, new RemoteValue(request));
if (res.getException() != null) {
throw (Exception) res.getException();
} else {
return res.getValue().putAll(nonSerializable);
}
}
private static Map<String,RemoteService> remoteServiceMap = new ConcurrentHashMap<String, RemoteService>();
private static Object lock = new Object();
private RemoteService getRemoteService(String url) throws Exception {
RemoteService remoteService = remoteServiceMap.get(url);
if(remoteService==null){
synchronized (lock) {
remoteService = remoteServiceMap.get(url);
if(remoteService==null){
HessianProxyFactory factory = new HessianProxyFactory(Thread.currentThread().getContextClassLoader());
HttpClientHessianConnectionFactory hessianConnectionFactory = HttpClientHessianConnectionFactory.getInstance();
//hessianConnectionFactory.addHeader("authorization", "admin");
factory.setConnectionFactory(hessianConnectionFactory);
final Data config = Application.getInstance().getConfigs("hessian");
if (config != null) {
factory.setConnectTimeout(config.getLong("connectTimeout", 10000l));
factory.setReadTimeout(config.getLong("readTimeout", 10000l));
factory.setHessian2Reply(config.getBoolean("hessian2Reply", true));
factory.setHessian2Request(config.getBoolean("hesian2Request", false));
factory.setChunkedPost(config.getBoolean("chunkedPost", true));
factory.setDebug(config.getBoolean("debug", false));
}
remoteService = (RemoteService) factory.create(RemoteService.class, url);
remoteServiceMap.put(url, remoteService);
}
}
}
return remoteService;
}
}
4、实现RemoteModule中的invoke方法:
public Data invoke(String serviceId, Data request) throws Exception {
return RemoteServiceInvokerHelper.invoke(remoteServiceInvoker, id, serviceId, request);
}
框架源码:https://github.com/hifong/flying
博客空间:http://www.cnblogs.com/hifong/
Demo应用:https://github.com/hifong/pas
技术QQ群:455852142
远程服务调用(RMI)的更多相关文章
- 远程服务调用RMI框架 演示,和底层原理解析
远程服务调用RMI框架: 是纯java写的, 只支持java服务之间的远程调用,很简单, // 接口要继承 Remote接口 public interface IHelloService extend ...
- spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法
spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...
- dubbo专题」dubbo其实很简单,就是一个远程服务调用的框架(1)
一.dubbo是什么? 1)本质:一个Jar包,一个分布式框架,,一个远程服务调用的分布式框架. 既然是新手教学,肯定很多同学不明白什么是分布式和远程服务调用,为什么要分布式,为什么要远程调用.我简单 ...
- 远程服务调用RPC框架介绍,微服务架构介绍和RPC框架对比,dubbo、SpringClound对比
远程服务调用RPC框架介绍,微服务架构介绍和RPC框架对比,dubbo.SpringClound对比 远程服务调用RPC框架介绍,RPC简单的来说就是像调用本地服务一样调用远程服务. 分布式RPC需要 ...
- 【Dubbo源码阅读系列】之远程服务调用(上)
今天打算来讲一讲 Dubbo 服务远程调用.笔者在开始看 Dubbo 远程服务相关源码的时候,看的有点迷糊.后来慢慢明白 Dubbo 远程服务的调用的本质就是动态代理模式的一种实现.本地消费者无须知道 ...
- 分布式远程服务调用(RPC)框架
分布式远程服务调用(RPC)框架 finagle:一个支持容错,协议无关的RPC系统 热门度(没变化) 10.0 活跃度(没变化) 10.0 Watchers:581 Star:6174 Fork: ...
- maven聚合项目以及使用dubbo远程服务调用debug操作。
1.maven聚合项目以及使用dubbo远程服务调用debug操作. 然后操作如下所示: 然后如下所示: 启动断点所在的包的服务.以debug的形式启动. 断点进来的效果如下所示: 接下来请继续你的表 ...
- RPC远程服务调用
RPC远程服务调用: RPC 的全称是 Remote Procedure Call 是一种进程间通信方式. 它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编 ...
- 分布式应用开发 | SpringBoot+dubbo+zookeeper实现服务注册发现 | 远程服务调用
前言 通过新建两个独立服务--提供者.消费者,模拟两个独立分布的应用,通过使用dubbo+zookeeper来实现远程服务调用. 目录 项目搭建 provider-server consumer-se ...
随机推荐
- Ubuntu 安装Appium
1.安装node apt-get install node.js 2.安装npm apt-get install npm 3.安装cnpm npm install -g cnpm 创建链接:ln -s ...
- iOS中 学会如何对sqlite3 进行封装 (纯手工)
#waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...
- linux系统的磁盘空间限制的两种方法
最近在搞VPS,要用到磁盘的限额,在网上找了一些相关的资料,总结起来,有两个方法能实现,一是用quota,另外一种是限制目录大小,下面我就将这两种方法写出来,与大家一起分享! 首先我们来看第一种方法, ...
- Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像
1.定位到Vritualbox的安装目录 2.将安装好的.vdi文件复制一份到指定目录下 3.执行 VBoxManage internalcommands sethduuid F:\VirtualBo ...
- ABP架构学习系列
ABP实践学习系列 ABP Zero 本地化语言的初始化和扩展 ABP Zero 导航菜单之角色权限 ABP Zero示例项目问题总结 ABP后台服务之作业调度Quartz.NET ABP架构学 ...
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(2)
接上文内容,本节介绍Maven的聚合和继承. 从头阅读传送门 互联网时代,软件正在变得越来越复杂,开发人员通常会对软件划分模块,以获得清晰的设计.良好的分工及更高的可重用性.Maven的聚合特性能把多 ...
- 洛谷 P1471 方差
洛谷 P1471 方差 题目背景 滚粗了的HansBug在收拾旧数学书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本数学书里面发现了一个神奇的数列,包含N个实数.他想算算这个数列的 ...
- 在实战中使用Sass和Compass
第三章 无需计算玩转CSS网格布局 3.1 网格布局介绍 3.2 使用网格布局 3.2.1 术语 1 术语名 定义 是否涉及HTML标签 2 列 内容度量的垂直单位 否 3 容器 构成一个网格布局的H ...
- DOCKER 开发学习记录
DOCKER常用命令及参数 DOCKER镜像管理命令: 检索:docker search image_name 下载:docker pull image_namge 查看本地镜像:docker ima ...
- hihoCoder #1053 : 居民迁移(贪心,二分搜索,google在线技术笔试模拟)
#1053 : 居民迁移 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描述 公元2411年,人类开始在地球以外的行星建立居住点.在第1326号殖民星上,N个居住点分布在一条直 ...