远程服务调用(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 ...
随机推荐
- SQL 语句优化方法
尽量避免非操作符的使用,在索引上使用 NOT,<> 等操作符,数据库管理系统是不会使用索引的,可以将查询语句转化为可以使用索引的查询. 避免对查询的列的操作,任何对列的操作都可能导致全表扫 ...
- 五分钟学习React(一): 什么是React
在前端的世界里,我们要处理的文件不是太多,而是太少.每天开发项目将html.css.js.图片.字体文件都像大杂烩一般加载都网页上.当应用变得越来越臃肿的时候,会发现js用了那么多全局变量,css的继 ...
- python科学计算之numpy
1.np.logspace(start,stop,num): 函数表示的意思是;在(start,stop)间生成等比数列num个 eg: import numpy as np print np.log ...
- JavaScript闭包的深入理解
闭包算是javascript中一个比较难理解的概念,想要深入理解闭包的原理,首先需要搞清楚其他几个概念: 一.栈内存和堆内存 学过C/C++的同学可能知道,计算机系统将内存分为栈和堆两部分(大学的基础 ...
- 网络编程(sock)搞定!
前些日子写了一个网络编程的,纯新手,能优化的地方很多!但是,也算自己独立完成了这么一个东西,晚上发上来!!
- php写一个简洁的登录页面
在学php中,刚刚看完实战演练就写了个登录页面 1.表单解析图 这是我们要写的 先用html写个表单先 <html> <head> <title>login< ...
- JSZX_HC_2016_R5
#1 ccz 200 #2 CTL 130 #3 KPM 130 本来以为准备挺充分的,开始后还是出现一些状况 >_< 好在还算顺利…… A AC人数:4 平均分:70 题目描述 给定 ...
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- ElasticSearch + xpack 使用.md
ElasticSearch 是一个高可用开源全文检索和分析组件.提供存储服务,搜索服务,大数据准实时分析等.一般用于提供一些提供复杂搜索的应.我们为什么要选择 ElasticSearch ?因为它是一 ...
- [国嵌笔记][011][Linux密码破解]
破解步骤 1.在系统启动时进入grub选项菜单 2.在grub选项菜单中按e进入编辑模式 3.编辑kernel行,添加 /init 1 (表示进入单用户启动模式,在单用户启动模式中不会要求输入密码) ...