最近公司的项目用到了PhpRpc
1、需要用到的jar包为:phprpc_client.jar(选用,如果客户端是其他则不用,如js等)、phprpc.jar、phprpc_spring.jar
下载地址:http://phprpc.org/zh_CN/download/
2、web.xml配置
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/appServlet/mvc-dispatcher-servlet.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
3、mvc-dispatcher-servlet.xml配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean name="/userService" class="org.phprpc.spring.remoting.PHPRPC_Exporter"> <property name="service" ref="userService" /> <property name="serviceInterface" value="com.xxx.service.UserService" /> </bean> <bean name="userService" class="com.xxx.impl.UserServiceImpl"></bean> </beans>
userService是需要向外暴露的业务逻辑接口
以上服务器端的配置就完成了,下面配置客户端,我用的js作为客户端
var client = new PHPRPC_Client('http://172.16.9.194:8080/project/userService', ['login']); client.setKeyLength(256); client.setEncryptMode(2); client.login("username", "pwd", "param3", "param4", function (result, args, output, warning) { alert(result); });
这里相当调用了userService的login方法,该方法有4个参数。JS中的最后一个参数为回调函数,将服务器端返回的返回值alert一下
大概就是这样