远程调用方式总结

简介:

1、Hessian

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据

Hessian实现的样例见:http://www.alisdn.com/wordpress/?p=478

 

2、Web Service

 

3、RMI

spring对rmi进行了封装,操作起来更加的方便

 

 
  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.io.Serializable; 
  4. /** 
  5.  * POJO 类 
  6.  * @author keju.wangkj 
  7.  * 
  8.  */ 
  9. public class Account implements Serializable { 
  10.     private static final long serialVersionUID = 2093515007620385610L; 
  11.     private String name; 
  12.  
  13.     public String getName() { 
  14.         return name; 
  15.     } 
  16.  
  17.     public void setName(String name) { 
  18.         this.name = name; 
  19.     } 
  20.  
  21. } 

 

 
  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.util.List; 
  4.  
  5. public interface AccountService { 
  6.  
  7.       public void insertAccount(Account acc); 
  8.  
  9.       public List<String> getAccounts(String name); 
  10.     } 

 

 
  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5.  
  6. public class AccountServiceImpl implements AccountService { 
  7.  
  8.       public void insertAccount(Account acc) { 
  9.         // do something 
  10.           System.out.println("rmi invoke: account name -> " + acc.getName()); 
  11.       } 
  12.  
  13.       public List<String> getAccounts(String name) { 
  14.         // do something 
  15.          List<String> list = new ArrayList<String>(); 
  16.          list.add("rmi invoke: " +  name); 
  17.          return list; 
  18.       } 
  19.     } 

 

 
  1. package com.alibaba.ural.rmi; 
  2.  
  3. import java.rmi.Remote; 
  4. import java.rmi.RemoteException; 
  5. import java.util.List; 
  6.  
  7. public interface RemoteAccountService extends Remote { 
  8.  
  9.     public void insertAccount(Account acc) throws RemoteException; 
  10.  
  11.     public List<String> getAccounts(String name) throws RemoteException; 
  12. } 

 

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
  7.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
  8.  
  9.  
  10.     <bean id="accountService" class="com.alibaba.ural.rmi.AccountServiceImpl"> 
  11.         <!-- any additional properties, maybe a DAO? --> 
  12.     </bean> 
  13.     <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> 
  14.         <property name="serviceName" value="AccountService" /> 
  15.         <property name="service" ref="accountService" /> 
  16.         <property name="serviceInterface" value="com.alibaba.ural.rmi.AccountService" /> 
  17.          
  18.         <!-- defaults to 1099 --> 
  19.         <property name="registryPort" value="1199" /> 
  20.     </bean> 
  21.  
  22. </beans> 

启动服务:

 
  1. package com.alibaba.ural.rmi; 
  2.  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  4.  
  5. public class ServerStart { 
  6.     public static void main(String[] args) { 
  7.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 
  8.                 new String[] { "rmiServer.xml" }); // 加载提供者配置 
  9.         context.start(); 
  10.         synchronized (ServerStart.class) { 
  11.             while (true) { 
  12.                 try { 
  13.                     ServerStart.class.wait(); // 阻止JVM退出 
  14.                 } catch (InterruptedException e) { 
  15.                     // ignore 
  16.                 } 
  17.             } 
  18.         } 
  19.     } 
  20. } 

客户端代码:

 
  1. package com.alibaba.ural.rmi.client; 
  2.  
  3. import com.alibaba.ural.rmi.AccountService; 
  4.  
  5. public class SimpleObject { 
  6.     private AccountService accountService; 
  7.  
  8.     public void setAccountService(AccountService accountService) { 
  9.         this.accountService = accountService; 
  10.     } 
  11.  
  12.     public AccountService getAccountService() { 
  13.         return accountService; 
  14.     } 
  15. } 

 

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:tx="http://www.springframework.org/schema/tx" 
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
  6.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
  7.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
  8.  
  9.  
  10.     <bean id="simpleObject" class="com.alibaba.ural.rmi.client.SimpleObject"> 
  11.         <property name="accountService" ref="accountService" /> 
  12.     </bean> 
  13.  
  14.     <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
  15.         <property name="serviceUrl" value="rmi://localhost:1199/AccountService" /> 
  16.         <property name="serviceInterface" value="com.alibaba.ural.rmi.AccountService" /> 
  17.     </bean> 
  18.  
  19. </beans> 

开启调用:

 
  1. package com.alibaba.ural.rmi.client; 
  2.  
  3. import java.util.List; 
  4.  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  6.  
  7. import com.alibaba.ural.rmi.AccountService; 
  8.  
  9. public class Client { 
  10.     public static void main(String[] args) { 
  11.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( 
  12.                 new String[] { "rmiClient.xml" }); // 加载消费者配置 
  13.         context.start(); 
  14.         AccountService helloService = ((SimpleObject) context 
  15.                 .getBean("simpleObject")).getAccountService(); // 获取远程服务代理 
  16.         for (int i = 0; i < Integer.MAX_VALUE; i++) { 
  17.             try { 
  18.                 Thread.sleep(1000); 
  19.                 List<String>  hello = helloService.getAccounts("world"); // 执行远程服务 
  20.                 System.out.println("(" + i + ") " + hello.toString()); 
  21.             } catch (Exception e) { 
  22.                 e.printStackTrace(); 
  23.             } 
  24.         } 
  25.     } 
  26. } 

 

4、COBAR



本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/396922,如需转载请自行联系原作者

相关文章
|
存储 缓存 监控
|
算法 机器学习/深度学习
决策树算法原理(下)
决策树算法原理(上)这篇里,我们讲到了决策树里ID3算法,和ID3算法的改进版C4.5算法。对于C4.5算法,我们也提到了它的不足,比如模型是用较为复杂的熵来度量,使用了相对较为复杂的多叉树,只能处理分类不能处理回归等。
1232 0
|
9天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
10093 28
|
9天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
2816 14
|
4天前
|
人工智能 安全 应用服务中间件
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
NGINX曝出两个高危漏洞(CVE-2026-123633和CVE-2026-123007),CVSS 9.2分,均属HTTP/2内存安全缺陷,无需认证即可远程执行代码。影响自2015年起的多个版本,静态站点亦受威胁。修复版本为开源版1.30.7/1.31.9。(239字)
1379 1
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
|
7天前
|
传感器 安全 区块链
Windows 电脑运行安卓应用教程:开源工具 ApkStart 双击 APK 即装、独立窗口运行
ApkStart 是一款开源Windows工具,让安卓APP像原生软件一样运行:双击APK即安装、生成真实图标、独立窗口启动、开始菜单可搜索。无需传统模拟器,内置QEMU运行时,支持数据互通、ARM兼容与命令行操作。(239字)
|
1天前
|
存储 安全 网络协议
模型列表里看不到新上线的模型?分清内置目录、端点探测与能力声明
在 DSH 中配置模型提供商时,最容易混淆的是三类不同的「模型信息来源」:**已安装目录**、**端点实时探测**和**使用者自行声明的能力**。内置提供商的探测结果来自已安装目录,即使 API 地址指向网关,也不会变成网关的实时列表;列表中出现某个模型 ID,也不代表端点确实支持相应的上下文窗口或图片输入。本文依次梳理三个配置路径、列表看似过时的原因、探测与真实请求的区别、凭据和网络的排查方式,以及安全边界。
424 95
|
11天前
|
人工智能 前端开发 API
阿里云Qwen4-Max 大模型详解:定位、特征、能力、适用场景与相对Qwen3-Max差异解析
2026年11月11日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问AI平台Token Plan并接入千问办公、Qoder等入口。该版本聚焦知识理解、世界感知、编程与Agentic四项升级,主打复杂编程、专业办公、前端设计与专业软件操作等真实任务。预览期为11月11日10:00至11月25日24:00,仅支持Token Plan调用,到期后模型标识下线;限时2折、新用户再享5折,最低1折可体验。
|
11天前
|
JSON 前端开发 API
阿里云最新Qwen4-Max大模型介绍:模型定位、特征、能力、适用场景解析
2026 年 11 月 11 日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问 AI 平台 Token Plan,并同步接入千问办公、Qoder 等入口。该版本聚焦知识理解、世界感知、编程与 Agentic 四项升级,主打复杂编程、专业办公、前端设计与专业软件操作。预览期至 11 月 25 日 24:00,到期后模型标识下线;限时 2 折、新用户再享 5 折,最低 1 折可体验。
|
7天前
|
人工智能 弹性计算 开发工具
零成本玩各大模型!DeepSeek Harness dsh-our-free-model 插件,13 个厂商模型直接用
总结一下:装这个插件,你不用注册、不用绑卡、不用填一个 Key,就能在 DeepSeek Harness 里用上 DeepSeek V4.1 Flash、Kimi K3 这一批前沿模型,而且是白嫖的、还不限量。三步搞定:下载插件,复制包含 `package.json` 的插件目录路径,在「添加插件」里粘贴路径安装并重启。无论是在阿里云 ECS 上通过命令行部署的 Web 版,还是本地的 Electron 桌面端,都能顺利跑通,快速搭建一套零成本的免费模型使用环境。
1460 1