PHPRPC,它的商业版本是Hprose。这里仅记录其使用方法。其它相关内容可自行搜索。
对于开源的东西,建议大家看看其源码。
1、需要引入的jar包:phprpc_spring.jar,http://pan.baidu.com/s/1eQhbUwU。
2、web.xml中添加的内容:
1
2
3 phprpc
4 org.springframework.web.servlet.DispatcherServlet
5
6 contextConfigLocation
7 classpath:phprpc-servlet.xml
8
9
10 2
11
12
13 phprpc
14 /phprpc/*
15
3、新建phprpc-servlet.xml,内容如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2
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/beans6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2和3决定了可以使用 项目路径例如:http://localhost:8080/project/phprpc/phpRpcService.htm 访问接口。
4、分别新建以下内容
接口:PhprpcController.java
public interfacePhprpcController {publicString test(String type,Object firstorders,Object secondorders,Object thirdorders);
}
接口实现类:PhprpcControllerImpl.java
@Controllerpublic class PhprpcControllerImpl extends BaseController implementsPhprpcController{
@AutowiredprivatePhprpcService phprpcService;publicPhprpcService getPhprpcService() {returnphprpcService;
}public voidsetPhprpcService(PhprpcService phprpcService) {this.phprpcService =phprpcService;
}publicString test(String type,Object firstorders,Object secondorders,Object thirdorders) {
log.info("执行rpc。。。 type:"+type+" firstorders:"+firstorders+" secondorders:"+secondorders+" thirdorders"+thirdorders);
log.info("参数类型。。。 type:"+type+" firstorders:"+firstorders.getClass().getName()+
" secondorders:"+secondorders.getClass().getName()+" thirdorders"+thirdorders.getClass().getName());
String msg= "Hello PHPRPC!";returnmsg;
}
}
--------------------分界线--------------------------上面是构建phpRpc,下面是使用----------------------------------------分界线-------------------------------------------
5、在知道了类似 【http://localhost:8080/project/phprpc/phpRpcService.htm】这样的地址后,我们可以在另外一个程序中这样使用该rpc提供的方法。
以下代码和前面4段没有什么关系,仅随意展示了其一些使用方法,方法名【pushOrderData】并无实际意义,请勿自行关联,以防被误导。
A、invoke的参数:第一个为方法名,第二个为传入方法的参数(对象数组,必须按照顺序存放参数)
B、org.phprpc.util.Cast 是一个PHPRPC封装好的数据类型转换工具类。
public classPhpRpcDemo {public static void main(String[] args) throwsUnsupportedEncodingException {
String url3= "http://localhost:8080/XXXXXX/phprpc/phpRpcService.htm";
PHPRPC_Client client= newPHPRPC_Client(url3);//PhprpcClient.setEncryptMode(3);//1获取公钥
Object tempRet = client.invoke("getRsaPulbicKey", newObject[] {});
String tempJsonStr=Cast.toString(tempRet);
System.out.println(tempJsonStr);//回调//test1(client);//Map 和 List 类型//test2(client);//String类型
String jsons2 = "[{'id':'872'}]";
Object ret = client.invoke("pushOrderData", new Object[] {"1",null,jsons2,null,null});
String s = Cast.toString(ret);
System.out.println(s);*/
//也可以以 callback形式调用.
/*client.invoke("say", new Object[] {}, new PHPRPC_Callback() {
public void handler(String result, Object[] args) {
System.out.println("handler:");
System.out.println(result);
System.out.println("args:"+args[0]);
System.out.println();
}
});*/}private static voidtest2(PHPRPC_Client client) {
AssocArray ret1= (AssocArray)client.invoke("pushOrderData", new Object[] {"1","","",""});
Map m=ret1.toHashMap();//Cast.toString(null);
System.out.println(m);
}private static voidtest1(PHPRPC_Client client) {
Object[] d= new Object[4];
client.invoke("pushOrderData", d,newPHPRPC_Callback() {public voidhandler(String result, Object[] args) {
System.out.println("handler:");
System.out.println(result);
System.out.println("args:"+args[0]);
System.out.println();
}
});
System.out.println(d);
}
}