# remoting **Repository Path**: javacoo/remoting ## Basic Information - **Project Name**: remoting - **Description**: 远程服务调用组件 1.远程服务调用组件是基于Java SPI(Service Provider Interface)机制实现,具有插件式,高度可扩展,集成使用简单等特点。 2.独创的XML配置格式及配套解析方法,让远程接口配置更为简单,处理更为灵活, 3.组件提供统一的远程服务调用API,解决了远程服务调用代码分散,实现各异,配置硬编码等问题。 4.同时统一抽象的调用器接口使得每个服务的具体通信方式和格式对使用者都是透明的,可通过配置不同的调用器类来支持相应的调用策略。 5.远程服务调用组件,屏蔽了具体的通信方式和数据格式转换细节,程序员只需要传入参数对象和服务ID即可。 - **Primary Language**: Java - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2019-05-09 - **Last Updated**: 2024-04-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # remoting #### 介绍 远程服务调用组件 1.远程服务调用组件是基于Java SPI(Service Provider Interface)机制实现,具有插件式,高度可扩展,集成使用简单等特点。 2.独创的XML配置格式及配套解析方法,让远程接口配置更为简单,处理更为灵活, 3.组件提供统一的远程服务调用API,解决了远程服务调用代码分散,实现各异,配置硬编码等问题。 4.同时统一抽象的调用器接口使得每个服务的具体通信方式和格式对使用者都是透明的,可通过配置不同的调用器类来支持相应的调用策略。 5.远程服务调用组件,屏蔽了具体的通信方式和数据格式转换细节,程序员只需要传入参数对象和服务ID即可。 #### 软件架构 类关系模型 ![](https://gitee.com/javacoo/remoting/raw/master/doc/img.jpg) 基于XML配置文件,配置远程接口,提供统一远程调用API,解决了远程服务调用代码分散,配置硬编码在代码中,开发效率低下的问题。与现在使用的大多数方法相比,本方法简化了远程服务调用流程,配置灵活,开发效率大大提高,具体内容如下: 1. 独创XML配置格式: ``` #accessToken #appKey #livemode ``` 2. XML解析模块:系统启动时,读取XML配置文件,经过XML文件格式校验,节点完整性校验,节点数据格式合法性校验,解析等过程,最终将XML信息转换为系统内部接口服务元数据信息,保存在内存中,供下一步使用。 3. 服务注册模块:完成XML解析之后,服务注册模块,读取接口服务元数据信息,将原数据信息转换为服务调用模块需要的接口服务信息格式,保存在内存中,供服务调用模块使用。 4. 接口服务调用模块:提供了统一的远程服务调用接口,程序根据服务ID定位到已经注册的远程服务接口,传入请求参数,即可完成远程服务的调用,屏蔽了不同服务接口,请求方式,参数格式,协议类型等差异,让使用更为简单,开发效率更高。 5. 服务响应处理模块:接口服务调用模块完成服务调用后,返回信息将由此模块进行统一处理,将不同接口服务响应数据格式转换为统一的接口响应格式,也可根据不同接口配置不同的响应数据处理(扩展开发),屏蔽了响应数据格式差异。 #### 安装教程 1. Maven工程导入方式:pom.xml 文件添加如下依赖 ``` com.remoting remoting-core 1.0.0 ``` 2. jar包引入方式:导入以下包到工程 ``` remoting-api-1.0.0.jar remoting-core-1.0.0.jar remoting-tools-1.0.0.jar ``` #### 使用说明 1. 配置:以jar包引入方式,需要配置remoting.xml ``` ``` 2. 远程服务注册配置 ``` ``` 3. 配置参数说明 #### 注意:accessToken,livemode,appKey 为系统变量,以#开头使用,使用后节点的值将会被替换为系统生成的值 ###### 对应配置文件说明: http-endpoint 节点 属性 id:服务地址节点ID,全局唯一。 Service-addr:路径。 Type:类型。 Host子点: 属性 address:地址。 port:端口。 appKey:应用的唯一标示 secretKey:安全码 livemode:模式 scope:作用域 http-header 节点 属性 id:服务地址节点ID,全局唯一。 charset:编码类型。 Header子点: 属性 name:header键。 Import 节点:要导入的接口 属性 Service-id:服务ID,全局唯一。 Api: 即积分系统发布的接口名称。 Endpoint-ref:服务地址配置引用ID。 Header-ref:http请求头信息配置引用ID。 request-type:请求方式(post,get),默认post。 子节点 url-param:需要添加到URL中的参数配置。 data-param:需要添加到请求参数中的参数配置。 注意:取值支持SpingEL表达式,需在请求参数中配置该值,如 ``` #accessToken #appKey #livemode ``` Key 也支持SpingEL表达式:根据配置的EL表达式找到对应的节点并添加对应的字段(要添加的节点必须存在)如 ``` #appKey #secretKey THIRD 1.0 0 2 AND ``` 特别说明: 1. url-param:需要添加到URL中的参数配置。 如上例中的: ``` #accessToken ``` ​ 对应积分系统发布的接口规范中,url部分: ``` https://www.xxx.com?accesstoken=xxx ``` 其中url-name 节点中的 name属性对应的值为url中的accesstoken(注意大小写与url中保持一致)。 accessToken 则表示引用系统变量accessToken,系统自动识别,根据endpoint-ref引用配置中host配置(appKey, secretKey, scope),以及当前import节点属性api的值,生成accessToken。 2. data-param:需要添加到请求参数中的参数配置。 如上例中的: ``` #appKey #livemode ``` 对应积分系统发布的接口规范中,正文部分相应的字段。 像这种会根据服务器环境变化而变化,且在请求参数中的参数,可以用此节点配置,配置后,调用程序中就不需要再设置。 其中url-name 节点中的 name属性对应的值为接口规范请求参数中的key。 3. url-param ,data-param: 取值支持SpingEL表达式,需在请求参数中配置该值。 如: ``` ['token'] ``` Key EL表达式 ``` 0 ``` 4. Header-ref: http请求头信息引用ID,如果不设置则使用组件默认值: 即:Content-Type:application/json,编码为:UTF-8 4. 程序调用 ``` package com.remoting; import com.remoting.api.RemoteServiceExecutor; import com.remoting.api.RemoteSetting; import com.remoting.api.spi.executor.RemoteServiceExecutorFactory; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; /** * Unit test for simple App. */ public class RemotingTest extends TestCase { private final Logger logger = LoggerFactory.getLogger(RemotingTest.class); private RemoteServiceExecutor remoteExecutor; protected void setUp() throws Exception { remoteExecutor = remoteExecutor(); } private RemoteServiceExecutor remoteExecutor(){ return RemoteServiceExecutorFactory.create(); } /** * 构建远程服务配置 *

说明:

*
  • * @author DuanYong * @return * @since 2017年7月31日下午4:45:39 */ private RemoteSetting createRemoteSetting() { RemoteSetting remoteSetting = new RemoteSetting(); remoteSetting.setConfigLocation("remoting/service-import*.xml"); remoteSetting.setConnectionRequestTimeout(5 * 1000); remoteSetting.setConnectionTimeout(3 * 1000); remoteSetting.setDefaultMaxPerRoute(50); remoteSetting.setSocketTimeout(4 * 1000); remoteSetting.setMaxTotal(300); return remoteSetting; } /** * Create the test case * * @param testName * name of the test case */ public RemotingTest(String testName) { super(testName); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite(RemotingTest.class); } /** * Rigourous Test :-) */ public void testApp() { // Map param = new HashMap(); // param.put("mac", "18-99-f5-5d-71-c7"); // param.put("sversion", "ZLM65HiS2_0.00094"); // param.put("img_size", "300,564"); // param.put("keyword", "tv"); // Object resultObj = remoteExecutor.execute("together", param); // assertNotNull(resultObj); Map param = new HashMap(); param.put("billUserId", "2088102971016535"); param.put("startTime", "2019-02-18"); param.put("endTime", "2019-02-19"); param.put("ctoken", "u6vanf_DNIG55OWM"); Object resultObj = remoteExecutor.execute("tradeListQuery", param); logger.info(resultObj.toString()); System.out.println(resultObj.toString()); assertNotNull(resultObj); } public void testTogether() { Map param = new HashMap(); param.put("mac", "18-99-f5-5d-71-c7"); param.put("sversion", "ZLM65HiS2_0.00094"); param.put("img_size", "300,564"); param.put("keyword", "tv"); Object resultObj = remoteExecutor.execute("together", param); assertNotNull(resultObj); } } ``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)